Questions tagged [bufferedreader]

Java class that reads text from a character-input stream, buffering characters so as to provide for an efficient reading of characters, arrays, and lines.

BufferedReader is a class that 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. The default is large enough for most purposes.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

BufferedReader in = new BufferedReader(new FileReader("foo.in"));

will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.

Frequently asked questions

3275 questions
332
votes
12 answers

Scanner vs. BufferedReader

As far I know, the two most common methods of reading character-based data from a file in Java is using Scanner or BufferedReader. I also know that the BufferedReader reads files efficiently by using a buffer to avoid physical disk operations. My…
Mads Mobæk
  • 34,762
  • 20
  • 71
  • 78
217
votes
10 answers

Do I need to close() both FileReader and BufferedReader?

I'm reading a local file using a BufferedReader wrapped around a FileReader: BufferedReader reader = new BufferedReader(new FileReader(fileName)); // read the file // (error handling snipped) reader.close(); Do I need to close() the FileReader as…
Zilk
  • 8,917
  • 7
  • 36
  • 44
168
votes
3 answers

Convert InputStream to BufferedReader

I'm trying to read a text file line by line using InputStream from the assets directory in Android. I want to convert the InputStream to a BufferedReader to be able to use the readLine(). I have the following code: InputStream is; is =…
karse23
  • 4,055
  • 6
  • 29
  • 33
161
votes
12 answers

Android Reading from an Input stream efficiently

I am making an HTTP get request to a website for an android application I am making. I am using a DefaultHttpClient and using HttpGet to issue the request. I get the entity response and from this obtain an InputStream object for getting the html of…
RenegadeAndy
  • 5,440
  • 18
  • 70
  • 130
107
votes
8 answers

Difference between java.io.PrintWriter and java.io.BufferedWriter?

Please look through code below: // A.class File file = new File("blah.txt"); FileWriter fileWriter = new FileWriter(file); PrintWriter printWriter = new PrintWriter(fileWriter); // B.class File file = new File("blah.txt"); FileWriter fileWriter =…
i2ijeya
  • 15,952
  • 18
  • 63
  • 72
102
votes
5 answers

What exactly does "Stream" and "Buffer" mean in Java I/O?

I just learned about input/output using BufferedReader. I wanted to know what exactly are the meanings of the term Stream and Buffer? Also what does this line of code serves us: BufferedReader br=new BufferedReader(new…
user122345656
  • 1,523
  • 5
  • 15
  • 20
68
votes
6 answers

Specific difference between bufferedreader and filereader

I would like to know the specific difference between BufferedReader and FileReader. I do know that BufferedReader is much more efficient as opposed to FileReader, but can someone please explain why (specifically and in detail)? Thanks.
Outlier
  • 811
  • 1
  • 7
  • 14
60
votes
13 answers

SSLHandShakeException No Appropriate Protocol

I recently added SSL to my website and it can be accessed over https. Now when my java application tries to make requests to my website and read from it with a buffered reader it produces this stack trace Im not using a self signed certificate the…
ChrisianBartram
  • 733
  • 1
  • 5
  • 9
59
votes
7 answers

What is the difference between Java's BufferedReader and InputStreamReader classes?

What is the difference between Java's BufferedReader and InputStreamReader classes?
Ajay Yadav
  • 1,625
  • 4
  • 19
  • 29
53
votes
9 answers

Best way to use BufferedReader in Kotlin

So I've just started using Kotlin for Android, and converted my Android Java codes into Kotlin. In one of the conversions, I stumbled upon a BufferedReader, which I would usually write in Java as the following: String result = ""; String line =…
Akira Kido
  • 629
  • 1
  • 6
  • 11
44
votes
6 answers

Why is BufferedReader read() much slower than readLine()?

I need to read a file one character at a time and I'm using the read() method from BufferedReader. * I found that read() is about 10x slower than readLine(). Is this expected? Or am I doing something wrong? Here's a benchmark with Java 7. The input…
dariober
  • 8,240
  • 3
  • 30
  • 47
43
votes
8 answers

Read all lines with BufferedReader

I want to type a multiple line text into the console using a BufferedReader and when I hit "Enter" to find the sum of the length of the whole text. The problem is that it seems I'm getting into an infinite loop and when I press "Enter" the program…
deadpixels
  • 769
  • 1
  • 12
  • 21
43
votes
2 answers

How to use BufferedReader in Java

Sorry if this is an obvious question, but I can't seem to get it. I'm working on an assignment for a Data Structures course. It involves pulling data from a simple .dat file. We had never used any of the file-accessing options in Java before, so the…
43
votes
7 answers

Reading specific number of bytes from a buffered reader in golang

I am aware of the specific function in golang from the bufio package. func (b *Reader) Peek(n int) ([]byte, error) Peek returns the next n bytes without advancing the reader. The bytes stop being valid at the next read call. If Peek returns…
Kirk Backus
  • 4,776
  • 4
  • 32
  • 52
38
votes
6 answers

How to read plain text file in kotlin?

There may be various way to read plain text file in kotlin. I want know what are the possible ways and how I can use them.
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53
1
2 3
99 100