Reputation: 419
I have a BufferedReader that's reading a file. The file can easily have an empty line between 2 paragraphs ex:
Lorem ipsum dolor sit amet, mel in docendi eleifend sapientem, mei cu natum possit salutandi. Quaeque legimus appareat cum in, tamquam cotidieque duo in. Justo ubique efficiantur ei sit, idque dicat sadipscing ad sit. Amet consul disputationi ei sea. At reque lorem quo, animal singulis per cu, usu euripidis reprimique inciderint eu.
Ocurreret dissentias ei per. Copiosae vulputate vel ei, ne est dissentias adversarium, ad quo graecis nostrum volutpat. Vix ut sale persequeris, id est volumus noluisse, mel et ridens molestiae. Ut qui iriure volutpat contentiones, unum propriae inciderint ut duo. Ei singulis delicata per. Usu verterem nominati in.
The BufferedReader stops reading when it reaches the end of the first paragraph. I know why it stops as it thinks as long as an empty line is found, it thinks it's the end of the file, which is my problem.
I've been using the while loop way to iterate through the file.
I've scrambled around this problem so much I scrapped my original bufferedreader method and editted it a lot of times, but it was something like: (pseudo code, this code is not correct. notice the while loop mainly)
public static String currenttxt() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
try {
while (br.readLine() != null) {
return br.readline();
}
} catch (Exception e) {
e.PrintStackTrace();
}
}
How can I solve this issue?
Upvotes: 3
Views: 6204
Reputation: 5603
As in the comments already mentioned your function is discarding the first line because of the while (br.readLine())
and leave the loop after reading the second line.
You should keep the line already read within some local variable.
public static String currenttxt() throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
StringBuilder b = new StringBuilder();
try {
String line = br.readLine();
while (line != null) {
b.append(line);
b.append(System.getProperty("line.separator"));
line = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
// remove the last line separator at the end using trim()
return b.toString().trim();
}
With java 7 it could be simplified to
public static String currenttxt() throws IOException{
List<String> lines;
lines = Files.readAllLines(FileSystems.getDefault()
.getPath(file.getAbsolutePath()));
StringBuilder b = new StringBuilder();
boolean insertNewLine = false;
for (String line : lines) {
if (insertNewLine)
b.append(System.getProperty("line.separator"));
b.append(line);
insertNewLine = true;
}
return b.toString();
}
The second example shows one way how a trim()
at the end could be avoided.
Another way would be to remove the last line separator from the StringBuilder b
.
Upvotes: 1
Reputation: 1667
You should initialize a varible and assign it a value every time you reading a line to avoid the problem:
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
return sb.toString();
If you want to preserve the empty line between paragraphs it is better to read per char:
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
int c = br.read();
while (c != -1) {
sb.append(String.valueOf((char)c));
c = br.read();
}
return sb.toString();
Upvotes: 1