0

The bufferwriter is not writing to a file. Please can anyone tell me what could be the issue

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;


public class main {
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        Writer output = null;
        File file = new File("D:/junk/CI_CSSOIDs sql_query/orphans.log");
        output = new BufferedWriter(new FileWriter(file));

        java.io.FileReader fr = new FileReader( "D:/junk/CI_CSSOIDs ql_query/SQL_CSSO.log" ) ;
        java.io.BufferedReader reader = new BufferedReader( fr ) ;
        int orphancount=0;
        String line = null ;
        int count =1;
        while( ( line = reader.readLine() ) != null )
        {         
            String words[]=line.split(" ");
            for (int i =0;i<words.length;i++){
                if (words[i].length()==32){
                    String CIline=null;
                    java.io.FileReader CIfr = new FileReader( "D:/junk/CI_CSSOIDs sql_query/CI.log" ) ;
                    java.io.BufferedReader CIreader = new BufferedReader( CIfr ) ;
                    boolean orphan = true;
                    while((CIline=CIreader.readLine())!=null){
                        if (CIline.contains(words[i])){
                            orphan=false;
                            break;
                            }                       
                    }
                    if(orphan){
                        orphancount++;
                        output.write("####"+words[i]+"*****\n");
                        System.out.println(words[i]+" : is an orphan CSSOID");
                    }                   
                }
            }       
            count++;        
        }
        System.out.println("Orphan count is :"+orphancount);
    }
}
mujeeb
  • 799
  • 5
  • 18
  • 29

4 Answers4

3

Try to flush() and then close() your BufferedWriter after writing the data to it.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
2

A BufferedWriter buffers writes. That means it keeps the data in memory until the buffer is exhausted or you flush/close the writer. It does this to improve performance.

I suggest you close all your stream/reader/writers and that will fix your problem too.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks Peter. Your answer was helpful. I added output.flush(); after the write statement. – mujeeb Jul 18 '11 at 19:41
  • 1
    It will hurt performance to call flush too often (defeating the purpose of using a BufferedWriter) I suggest you not flush and close all the resources you should closing. – Peter Lawrey Jul 18 '11 at 19:46
2

Close your BufferedWriter after you're finished writing.

output.close();
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
2

You neither flush() nor close() the writer. Since it's a buffered writer, all the stuff is stored up in the buffer until you do one of those.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199