Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The first character is cut off when BufferedReader is used

Tags:

java

file-io

This is the code :

File file = new File("Hello.txt");
file.createNewFile();
FileWriter write = new FileWriter(file);
BufferedWriter bufWrite = new BufferedWriter(write);
bufWrite.write("HelloWorld");
bufWrite.flush();
bufWrite.close();

FileReader read = new FileReader(file);
BufferedReader bufRead = new BufferedReader(read);
while(bufRead.read()!=-1){
 System.out.println(bufRead.readLine());
}
bufRead.close();

Here, the output is elloWorld. 'H'is not there. Why is it so? Not sure if I'm doing anything wrong here!

like image 749
Mercenary Avatar asked Jul 30 '13 15:07

Mercenary


People also ask

What is BufferedReader used for?

Class BufferedReader. Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used.

What is the default size of BufferedReader in Java?

maybe it was other way for some years, or will be other way in future, but now it is stricctly specified to 8192 in the java.

What is BufferedReader buffer size?

Initializing a BufferedReader. Wrapping the FileReader like this is a nice way to add buffering as an aspect to other readers. This will set the buffer size to 16384 bytes (16 KB). The optimal buffer size depends on factors like the type of the input stream and the hardware on which the code is running.

What happens if BufferedReader is not closed?

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect. So, if you don't close(), system resources may be still associated with the reader which may cause memory leak.


4 Answers

It's a surprising common question.

When you do

bufRead.read()

you actually read a character, it doesn't put it back and let you read it again later.

The simplest solution is to not do this.

File file = new File("Hello.txt");
try (PrintWriter pw = new PrintWriter(new FileWriter(file))) {
    pw.println("HelloWorld"); // should have a new line
}

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    for (String line; (line = br.readLine()) != null; ) {
        System.out.println(line);
    }
}
like image 134
Peter Lawrey Avatar answered Sep 29 '22 02:09

Peter Lawrey


Look at your loop:

while(bufRead.read()!=-1){
 System.out.println(bufRead.readLine());
}

You're using read in the loop - which will consume the first character of the next line.

You should use:

String line;
while ((line = bufRead.readLine()) != null) {
    System.out.println(line);
}
like image 27
Jon Skeet Avatar answered Sep 29 '22 02:09

Jon Skeet


That has a very simple reason: The line

while(bufRead.read()!=-1){

consumes one character from the input stream. From the documentation:

Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.
like image 31
f1sh Avatar answered Sep 29 '22 02:09

f1sh


You already read a character at

while(bufRead.read()!=-1)

If there are more than one lines then it will vanish first character of every line!

so use

String line;
while ((line = bufRead.readLine()) != null) {
    System.out.println(line);
}

See read() readLine()

like image 33
a question Avatar answered Sep 29 '22 02:09

a question