InsaneCoder
InsaneCoder

Reputation: 8288

BufferedWriter not writing anything to a file

I am reading a file in.txt and writing the numbers to a file out.txt until 42 is found.But in out.txt I am getting blank file.Instead if I write System.out.println(num) instead of out.write(num) I get correct result.It means that the problem is with the statement of BufferedReader.Where I am wrong?

import java.io.*;

class Numbers
{
    public static void main(String args[])
    {
        try{
                    String num;
                    BufferedReader in=new BufferedReader(new FileReader("in.txt"));
                    BufferedWriter out=new BufferedWriter(new FileWriter("out.txt"));

                    while((num=in.readLine())!=null)
                    {
                        if(Integer.parseInt(num)==42)
                            break;
                        else
                            out.write(num);
                    }
        }catch(Exception e)
        {
            System.out.println("File not found");
        }
    }
}

Upvotes: 0

Views: 636

Answers (4)

Dhaval
Dhaval

Reputation: 1076

make sure you have out.close() at the end of try block.

if you have in.txt as a very big file, then you will see some data in out.txt.

BufferedWriter writes only when it has enough data to flush, which is approximately equal to one block size.

Upvotes: 0

Jatin
Jatin

Reputation: 31754

The problem is the you are not closing the out stream. Change it to:

 BufferedReader in = null;
 BufferedReader out = null;
 try{
        String num;
        in = new BufferedReader(new FileReader("in.txt"));
        out = new BufferedWriter(new FileWriter("out.txt"));

         while((num=in.readLine())!=null)
                  {
                      if(Integer.parseInt(num)==42)
                          break;
                      else
                          out.write(num);
                  }
               out.close()
    }catch(Exception e)
    {
        System.out.println("File not found");
    }finally{
       try{
        if(in!=null) in.close();
        if(out!=null) out.close();
        }catch(Exception ex) {ex.printStackTrace();}
    }

This is because, your OutputStream buffers your data and periodically flushes it. Closing the stream not only flushes it but also makes it safe for other applications to use the file.

In your case you might expect a weird behavior (with sometimes complete write and sometimes not). This is due to the fact that BufferedWriter() tries closing it in its finalize method (which may or may not be called)

Upvotes: 1

NValchev
NValchev

Reputation: 3015

You should close the stream after stop using it. Closing it will, firstly, flush the stream (all buffered data will be printed) and secondly, will release all resources the stream is using.

Upvotes: 0

Bucket
Bucket

Reputation: 7521

You need to close your FileWriter:

while((num=in.readLine())!=null)
{
    if(Integer.parseInt(num)==42)
        break;
    else{
        out.write(num);
        out.flush();
    }
}
out.close();

Contents always need to be flushed. close() by itself will flush the stream for you, but it's good practice to flush() anyway.

Upvotes: 0

Related Questions