I’m using the next method to read content from a file. The problem here is that i’m limited to ht unmber of characters specified for inputBuffer (in this case 1024).
First of all, if the content is less than 1024 chars long, i get a lot of whitespace chars, and i need to use trim to remove them.
Second of all, and this is more important, I’d like to read the entire content of the file, even if it is more than 1024 characters and insert it into a String object. I’ve understood that I should not use the .available method to determine if there is more data in the file, because it’s not accurate or something like that.
Any ideas on how I should go about doing this?
public String getContent( String sFileName )
{
//Stop in case the file does not exists
if ( !this.exists( sFileName ) )
return null;
FileInputStream fIn = null;
InputStreamReader isr = null;
String data = null;
try{
char[] inputBuffer = new char[1024];
fIn = _context.openFileInput(sFileName);
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
isr.close();
fIn.close();
}catch(IOException e){
e.printStackTrace(System.err);
return null;
}
return data.trim();
}
You can read the #/bytes before you allocate your buffer:
Another solution is to read the input as strings, a line at a time.