java – buffered readed readline() gives null as end of file but no way to use that null-Collection of common programming errors

Is there a way to check whether a file was correctly written, I mean if there is an EOF at the end? I’m asking that because I have a program that takes some file, merge them in a very big file and then use it to get statistics from it. The point is that the second part never ends because it doesn’t recognize the end of file.

The relevant parts of the code are the following: (please do not ask for the whole code as I cannot post for important reasons)

    FileWriter file=null;
    PrintWriter pw = null;
    String pathToRead=null;
    InputStreamReader isr = null;
    BufferedReader br = null ;
    FileInputStream fis = null ;
    TestJFileChooser d=new TestJFileChooser();
    int c=1;
            String line=null;

    ....

    //here i select the files

    selectedFile=new File(pathToRead);

    //here I get one buffer reader for each file got with listFiles()

    for(File file_sel:app){
        if (file_sel.getName().startsWith("gtou")){
            System.out.println(file_sel.getName());
            fis = null;
            try {
                fis = new FileInputStream(file_sel);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            isr=new InputStreamReader(fis);
            br=new BufferedReader(isr);
            map.put(i, br);
            num_file++;
            i++; 
        }
    }

    //then I select the output file and open a print writer for it

    fileToWrite=new File(pathToRead);
        try {
            file = new FileWriter(fileToWrite);
            pw= new PrintWriter(file);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        //merging part
        ....
        line=br.readLine();
        while(line!=null){
        System.out.println("line is:"+line);
        ....
        line=br.readLine();
        }   


       //end of merging ....

        pw.flush();
        pw.close();

        try {
            if (file!=null) file.close();
            fis.close();
            isr.close();
            br.close();
            for(int fi=0;fi 0) {
                    os.write(buffer, 0, readBytes);
                }
                fos.flush();
                is.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

If you read files with different encodings, you will have to modify at least the reading of course.

If it doesn’t work, I’d suggest you build a “summarized” and runable sample program.

  • The core of your question is this code:

        BufferedReader br = ...
        String line = br.readLine();
        while (line != null) {
            System.out.println("line is:" + line);
            ...
            line = br.readLine();
        }
    

    You say that this repeatedly outputs this:

    line is: null ;
    line is: null ;
    

    (Notice the ” ;” on the end!!!)

    The only way that can happen is if the file you are reading contains at least one line that look like this:

    null ;
    

    Indeed, unless the “…” code includes a continue statement, there must must be lots of those lines in the input file.

    Is there a way to check whether a file was correctly written?

    Yea. Look at it using a text editor and/or check its file size.

    I mean if there is an EOF at the end?

    In modern file systems, EOF is a position not a marker. Specifically it is the position after the last byte of the file. So it is logically impossible for a file to not have an EOF. (You’d have to have a file that is infinite in length for there to be no EOF.)

  • Originally posted 2013-11-19 13:18:57.