Below are my codes, I’m having this weird warning: Resource leak: 'br' is never closed . Could anyone help me with this warning. I do not want my app to crash or cause any problems in the future.
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"/St/"+ textToPass);
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
TextView output=(TextView) findViewById(R.id.st);
// Assuming that 'output' is the id of your TextView
output.setText(text);
You don’t close your BufferedReader.
Will close both of your streams.