• 0

Java File I/O


Question

I'm attempting to read a fairly large (~28MB) file that contains only a single line of text. I've tried using a BufferedReader, but readLine() crashes with an Out of Memory error. Is there a better way of doing this? Would reading it in a character at a time with read() work?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Try:

try
{
         File theFile = new File( "textFile.txt");
         BufferedInputStream in = new BufferedInputStream(new FileInputStream(theFile));
         byte[] b = new byte[in.available()];
         in.read(b, 0 , b.length ); 
         String s = new String(b,0,b.length);
         in.close();
}
catch (IOException ex )
{
         System.out.println("Error opening file");
}

This reads the whole file at once.

Link to comment
Share on other sites

This topic is now closed to further replies.