Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

J2ME FileConnection

 
Ashley Swinson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program opens a file and reads it using the following code:

fc = (FileConnection)Connector.open(file.getString());
fis = fc.openInputStream();
fdi = new DataInputStream(fis);

int inputChar2;
String strTar = new String();
while ( (inputChar2 = fdi.read()) != -1) {
strTar+=((char)inputChar2);
}
fdi.close();
fis.close();
fc.close();

After the file is read, I store it in a String and open a second file, then I replace the previous String with the second file, and open a third file, etc. So, I keep updating a String with files that are all about 40 kilobytes. When I am trying to read the second file that I open (in the loop), I get an out of memory exception. How can I fix this? It doesn't make sense to me because I can always open the first file, no matter how big it is. But, after I store the first file into a String and then open and try to read the second file, I get the following exception. Please help! How can I fix this and why is it doing it?

java.lang.OutOfMemoryError
at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+309)
at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)
at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)
at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic