tags:

views:

122

answers:

4

In the following program, loop is iterating 1000 times, and I am writing all the entries in a file using a FileWriter, but unfortunately programs ends up writing only 510(sometimes 415, sometimes 692, always less then 1000) entries in the file, but loop is iterating 1000 times.

import java.io.* ;
import java.util.*;

public class DemoWriter {

    public static void main(String[] args) throws Exception {

     List<String> receiverList = new ArrayList<String>() ;
     receiverList.add("[email protected]") ;
     receiverList.add("[email protected]") ;
     receiverList.add("[email protected]") ;

     FileWriter fw = new FileWriter("a.txt") ;
     BufferedWriter bw = new BufferedWriter(fw) ;

     int size = receiverList.size() ;

     String str ;
     int count = 0 ;
     for(int i = 1 ; i <= 1000 ; ++i){
      str = receiverList.get( (int) (Math.random() * size) ) + "\n" ;
      bw.write(++count + ".> " + str) ;
      System.out.print(count + ".> " + str) ;
     }
    }
}

Is this because of file size or something else???


Thnx for the quick response to all the nice people here. I have corrected my code (I just forgot to close the stream and now code working perfectly). As all pointed that i need to close the stream, but i am accepting BalusC as he was the first one who replied.

Nice to c u BalusC here. Cheers :)

+9  A: 

You aren't closing and flushing your writers. If you call the close() method on your writers at the end of your code, the buffers will be flushed and the writers closed.

As Hank Gay points out in the comments, the close() methods will possibly throw an exception (an IOException, I believe). This means you'll have to wrap the calls to close in a try/catch block. However, I see your main method throws Exception - this isn't the best practice, but it will prevent you from needing a try/catch block in this particular instance.

Thomas Owens
Also, the call(s) to `close()` should be in a `finally` block.
Hank Gay
A: 

Do you need to close the BufferedWriter and FileWriter objects? That can leave incomplete files hanging around.

Aric TenEyck
bw.close() should pass close() through.
Xepoch
A: 

Just add

    bw.flush();
    bw.close();

to the end of the method

ALOR
The method names are lowercase and I believe (would need to double check the docs), but calling close also flushes the buffer...
Thomas Owens
I'm not sure about it. It's certainly so in C#.Docs say "public void close()throws IOException \\Close the stream."
ALOR
This is Java, not C#. It could be dependent on the type of writer, however.
Thomas Owens
I understand that "this is java", thx. It's JAVA docs about this specific writer, ok?
ALOR
+5  A: 

Indeed, you should always close the resources to flush everything and free up the resources. Explicitly flushing is not necessary as it's usually implicitly done during closing. Just invoke Closeable#close() inside the finally block of a try-catch-finally block and you're fine.

Learn more at the Java IO tutorial.

BalusC
You don't want to invoke close() in the finally part of a try-catch block - it throws an exception.
Thomas Owens
So? Just catch it and ignore further or do e.printStackTrace() of it for logging purposes. It certainly needs to be done in the finally block because you would otherwise leak resources.
BalusC