Reputation: 377
Ok, so I am reading a 40 mb file 1000 bytes at a time. Each time I empty my buffer I check to make sure that the FileReader still has data to be read using the ready() method. However, it is returning false when there are bits that still haven't been read but the stream isn't ready. How would I circumvent this?
if( !fileInput.ready() )
{
System.out.println(!fileInput.ready());
//empty the rest of the buffer into the output file
fileOutput.write( buffer.toString() );
fileOutput.flush();
doneProcessing = true;
}
Upvotes: 0
Views: 1818
Reputation: 419
Try doing in this manner:
Reader in = new FileReader(args[0]);
Writer out = new FileWriter("output.doc");
BufferedReader br = new BufferedReader(in);
String str;
while((str=br.readLine())!=null){
out.write(str);
System.out.println(str.getBytes());
}
out.close();
Upvotes: 0
Reputation: 109007
I don't see where you are reading from fileInput and filling up buffer. Try this instead
FileOutputStream out = ...
InputStream in = ..
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) >= 0)
{
out.write(buffer, 0, len);
}
in.close();
out.close();
Upvotes: 2
Reputation: 29139
There is an easier way to read than using the ready() method. The trick is that the read method returns -1 when read fully.
char[] buf = new char[ 1024 ];
for( int count = reader.read( buf ); count != -1; count = reader.read( buf ) )
{
output.write( buf, 0, count );
}
You can do a similar thing with InputStream if you are reading in binary. Just convert buf to byte[].
Upvotes: 1