Daniele Buonadonna
Daniele Buonadonna

Reputation: 77

BufferedReader do not read the entire text file

I read about someone having troubles with BufferedReader: the reader simply do not read the first lines. I have instead the opposite problem. For example, in a text file with 300 lines, it arrives at 200, read it half of it and then the following string is given null, so it stops.

private void readerMethod(File fileList) throws IOException {
    BigInteger steps = BigInteger.ZERO;
    BufferedReader br = new BufferedReader(new FileReader(fileList)); 

    String st;
    //reading file line by line
    try{
        while (true){
            st = br.readLine();
            if(st == null){
                System.out.println("Null string at line " + steps);
                break;
            }
            System.out.println(steps + " - " + st);
            steps = steps.add(BigInteger.ONE);
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try{
            br.close();
        }catch(Exception e){}
    }
}

The output of the previous slice of code is as expected until it reaches line 199 (starting from 0). Consider a file with 300 lines.

... 198 - 3B02D5D572B66A82F9D21EE809320DB3E250C6C9 199 - 6E2C69795CB712C27C4097119CE2C5765 Null string at line 200

Notice that, all lines have the same length, so in this output line 199 is not even complete. I checked the file text, and it's correct: it contains all 300 lines and they are all of the same length. Also, in the text there are only capitals letters and numbers, as you can see.

My question is: how can i fix this? I need that the BufferedReader read all the text, not just a part of it.

As someone asked i add here the remaining part of the code. Please notice that all capital names are constant of various type (int, string etc). This is the method that is called by the main thread:

public void init(){
    BufferedWriter bw = null;
    List<String> allLines = createRandomStringLines(LINES);
    try{
        String fileName = "SHA1_encode_text.txt";
        File logFile = new File(fileName);
        System.out.println(logFile.getCanonicalPath());
        bw = new BufferedWriter(new FileWriter(logFile));

        for(int i = 0; i < allLines.size(); i++){
            //write file
            String o = sha1FromString(allLines.get(i));
            //sha1FromString is a method that change the aspect of the string,
            //replacing char by char. Is not important at the moment.
            bw.write(o + "\n");
        }

    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try{
            bw.close();
        }catch(Exception e){}
    }

}

The method that create the list of random string is the following. "SYMBOLS" is just a String contains all avaiable chars.

private List<String> createRandomStringLines(int i) {
    List<String> list = new ArrayList<String>();
    while(i!=0){
        StringBuilder builder = new StringBuilder();
        int count = 64;
        while (count-- != 0) {
            int character = (int)(Math.random()*SYMBOLS.length());
            builder.append(SYMBOLS.charAt(character));
        }
        String generatedString = builder.toString();
        list.add(generatedString);
        i--;
    }
    return list;
}

Note that, the file written is totally correct.

Upvotes: 2

Views: 1403

Answers (1)

Daniele Buonadonna
Daniele Buonadonna

Reputation: 77

Okay, thanks to the user ygor, i manage to resolve it. The problem was that the BufferReader stars his job when the BufferWriter isn't closed yet. It was sufficient to move the command line that require the reader to work, after the bufferWriter.close() command.

Upvotes: 1

Related Questions