BufferedReader do not read the entire text file









up vote
1
down vote

favorite
1












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.










share|improve this question



















  • 1




    When you take a look at BufferedReader code, you can see that the size is the number of caracters and not the number of lines. I don't think this is the problem here but you should fix this.
    – Mickael
    Nov 9 at 9:22






  • 2




    Windows 10 is enough information. Please, show us the code, which generates the file. Do you close the writer properly ? Maybe the written file is not flushed properly before you attempt to read it.
    – ygor
    Nov 9 at 9:41







  • 1




    The method that reads the file by chance it running together with the method that writes it? Or does the reader start after the writer has finished?
    – Robert Kock
    Nov 9 at 9:55






  • 1




    Please add "System.out.println("writer closed")" AFTER "bw.close();" and add "System.out.println("reader opening") BEFORE BufferedReader br = new BufferedReader(new FileReader(fileList)); In which order will those two messages appear ?
    – ygor
    Nov 9 at 10:01






  • 1




    BufferedWriter's default buffer size is 8192. You reader stops reading exactly after 8192 bytes ((40+1)*199 + 33. It is almost certain, that your writer did only flush once.
    – ygor
    Nov 9 at 10:07














up vote
1
down vote

favorite
1












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.










share|improve this question



















  • 1




    When you take a look at BufferedReader code, you can see that the size is the number of caracters and not the number of lines. I don't think this is the problem here but you should fix this.
    – Mickael
    Nov 9 at 9:22






  • 2




    Windows 10 is enough information. Please, show us the code, which generates the file. Do you close the writer properly ? Maybe the written file is not flushed properly before you attempt to read it.
    – ygor
    Nov 9 at 9:41







  • 1




    The method that reads the file by chance it running together with the method that writes it? Or does the reader start after the writer has finished?
    – Robert Kock
    Nov 9 at 9:55






  • 1




    Please add "System.out.println("writer closed")" AFTER "bw.close();" and add "System.out.println("reader opening") BEFORE BufferedReader br = new BufferedReader(new FileReader(fileList)); In which order will those two messages appear ?
    – ygor
    Nov 9 at 10:01






  • 1




    BufferedWriter's default buffer size is 8192. You reader stops reading exactly after 8192 bytes ((40+1)*199 + 33. It is almost certain, that your writer did only flush once.
    – ygor
    Nov 9 at 10:07












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.










share|improve this question















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.







java string file bufferedreader reader






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 9:51

























asked Nov 9 at 9:07









Daniele Buonadonna

408




408







  • 1




    When you take a look at BufferedReader code, you can see that the size is the number of caracters and not the number of lines. I don't think this is the problem here but you should fix this.
    – Mickael
    Nov 9 at 9:22






  • 2




    Windows 10 is enough information. Please, show us the code, which generates the file. Do you close the writer properly ? Maybe the written file is not flushed properly before you attempt to read it.
    – ygor
    Nov 9 at 9:41







  • 1




    The method that reads the file by chance it running together with the method that writes it? Or does the reader start after the writer has finished?
    – Robert Kock
    Nov 9 at 9:55






  • 1




    Please add "System.out.println("writer closed")" AFTER "bw.close();" and add "System.out.println("reader opening") BEFORE BufferedReader br = new BufferedReader(new FileReader(fileList)); In which order will those two messages appear ?
    – ygor
    Nov 9 at 10:01






  • 1




    BufferedWriter's default buffer size is 8192. You reader stops reading exactly after 8192 bytes ((40+1)*199 + 33. It is almost certain, that your writer did only flush once.
    – ygor
    Nov 9 at 10:07












  • 1




    When you take a look at BufferedReader code, you can see that the size is the number of caracters and not the number of lines. I don't think this is the problem here but you should fix this.
    – Mickael
    Nov 9 at 9:22






  • 2




    Windows 10 is enough information. Please, show us the code, which generates the file. Do you close the writer properly ? Maybe the written file is not flushed properly before you attempt to read it.
    – ygor
    Nov 9 at 9:41







  • 1




    The method that reads the file by chance it running together with the method that writes it? Or does the reader start after the writer has finished?
    – Robert Kock
    Nov 9 at 9:55






  • 1




    Please add "System.out.println("writer closed")" AFTER "bw.close();" and add "System.out.println("reader opening") BEFORE BufferedReader br = new BufferedReader(new FileReader(fileList)); In which order will those two messages appear ?
    – ygor
    Nov 9 at 10:01






  • 1




    BufferedWriter's default buffer size is 8192. You reader stops reading exactly after 8192 bytes ((40+1)*199 + 33. It is almost certain, that your writer did only flush once.
    – ygor
    Nov 9 at 10:07







1




1




When you take a look at BufferedReader code, you can see that the size is the number of caracters and not the number of lines. I don't think this is the problem here but you should fix this.
– Mickael
Nov 9 at 9:22




When you take a look at BufferedReader code, you can see that the size is the number of caracters and not the number of lines. I don't think this is the problem here but you should fix this.
– Mickael
Nov 9 at 9:22




2




2




Windows 10 is enough information. Please, show us the code, which generates the file. Do you close the writer properly ? Maybe the written file is not flushed properly before you attempt to read it.
– ygor
Nov 9 at 9:41





Windows 10 is enough information. Please, show us the code, which generates the file. Do you close the writer properly ? Maybe the written file is not flushed properly before you attempt to read it.
– ygor
Nov 9 at 9:41





1




1




The method that reads the file by chance it running together with the method that writes it? Or does the reader start after the writer has finished?
– Robert Kock
Nov 9 at 9:55




The method that reads the file by chance it running together with the method that writes it? Or does the reader start after the writer has finished?
– Robert Kock
Nov 9 at 9:55




1




1




Please add "System.out.println("writer closed")" AFTER "bw.close();" and add "System.out.println("reader opening") BEFORE BufferedReader br = new BufferedReader(new FileReader(fileList)); In which order will those two messages appear ?
– ygor
Nov 9 at 10:01




Please add "System.out.println("writer closed")" AFTER "bw.close();" and add "System.out.println("reader opening") BEFORE BufferedReader br = new BufferedReader(new FileReader(fileList)); In which order will those two messages appear ?
– ygor
Nov 9 at 10:01




1




1




BufferedWriter's default buffer size is 8192. You reader stops reading exactly after 8192 bytes ((40+1)*199 + 33. It is almost certain, that your writer did only flush once.
– ygor
Nov 9 at 10:07




BufferedWriter's default buffer size is 8192. You reader stops reading exactly after 8192 bytes ((40+1)*199 + 33. It is almost certain, that your writer did only flush once.
– ygor
Nov 9 at 10:07












1 Answer
1






active

oldest

votes

















up vote
1
down vote













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.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222698%2fbufferedreader-do-not-read-the-entire-text-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    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.






    share|improve this answer
























      up vote
      1
      down vote













      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.






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 10:54









        Daniele Buonadonna

        408




        408



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222698%2fbufferedreader-do-not-read-the-entire-text-file%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)