BufferedReader do not read the entire text file
up vote
1
down vote
favorite
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
|
show 17 more comments
up vote
1
down vote
favorite
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
1
When you take a look atBufferedReader
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
|
show 17 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
java string file bufferedreader reader
edited Nov 9 at 9:51
asked Nov 9 at 9:07
Daniele Buonadonna
408
408
1
When you take a look atBufferedReader
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
|
show 17 more comments
1
When you take a look atBufferedReader
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
|
show 17 more comments
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 9 at 10:54
Daniele Buonadonna
408
408
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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