1

I would like to know how to put it in a stringbuffer after reading it using charsequence?

Here is my code:

StringBuffer data=new StringBuffer();

File f= new File(Environment.getExternalSTorageDirector+"/file.pdf");
FileInputStream fileIS= new FileInputStream(f);
BufferReader buf= new BufferReader(new InputSreamReader(fileIS));
String readString = new String();
while((readString = buf.readLine())!=null)
{data.append(readString);
}

THE FILE IS 10MB

Doing this I have error: OutOfMemory

user1222905
  • 533
  • 2
  • 17
  • 36
  • What is the file size? If the file size is X MB, then you need at least 2*X MB of memory in Android. – Pau Kiat Wee Mar 30 '12 at 12:09
  • 1
    readLine reads all line. You read `file.pdf` pdf does not is text file. You should read file to byte array. Use [FileUtils](http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#readFileToByteArray(java.io.File)) or check how they wrote this method (source code is available in [svn](http://commons.apache.org/io/source-repository.html)) – Andrzej Jozwik Mar 30 '12 at 12:13
  • Are PDF files meant to be read as 'lines'? The `readLine` method will try to read till it finds '\n'. I think you're better off reading in chunks of a fixed size, instead of 'lines'. – ArjunShankar Mar 30 '12 at 12:13
  • 1
    If the file is indeed a PDF, then reading Strings is nonsensical: PDF is (primarily) a binary format, so you should consider using bytes and Streams instead of readers. – nd. Mar 30 '12 at 12:22
  • the file could be pdf, a txt...it doesn't matter – user1222905 Mar 30 '12 at 12:35
  • can soneone help me solve this out? – user1222905 Mar 30 '12 at 12:40
  • @Pau where do you know that the size of memory in andoid needs to be at least 2*X? – user1222905 Mar 30 '12 at 15:33

0 Answers0