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
22
votes
8 answers

Can I peek on a BufferedReader?

Is there a way to check if in BufferedReader object is something to read? Something like C++ cin.peek(). Thanks.
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
22
votes
6 answers

Fastest Way To Read and Write Large Files Line By Line in Java

I have been searching a lot for the fastest way to read and write again a large files (0.5 - 1 GB) in java with limited memory (about 64MB). Each line in the file represents a record, so I need to get them line by line. The file is a normal text…
user1785771
  • 487
  • 2
  • 7
  • 18
22
votes
6 answers

Run .exe file in Java from file location

I have to open a .exe file from my Java program. So I tried following code First. Process process = runtime.exec("c:\\program files\\test\\test.exe"); But I was getting some error. Then I found out that the exe has to be launched from that…
Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
21
votes
4 answers

Socket, BufferedReader hangs at readLine()

I have a server which initially does this:- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); for (;;) { String cmdLine = br.readLine(); if (cmdLine == null || cmdLine.length() == 0) break; ... } later…
ashim
  • 231
  • 1
  • 2
  • 5
21
votes
2 answers

Why is the performance of BufferedReader so much worse than BufferedInputStream?

I understand that using a BufferedReader (wrapping a FileReader) is going to be significantly slower than using a BufferedInputStream (wrapping a FileInputStream), because the raw bytes have to be converted to characters. But I don't understand why…
Andy King
  • 1,632
  • 2
  • 20
  • 29
20
votes
7 answers

Java - Read all .txt files in folder

Let's say, I have a folder called maps and inside maps I have map1.txt, map2.txt, and map3.txt. How can I use Java and the BufferReader to read all of the .txt files in folder maps (if it is at all possible)?
test
  • 17,706
  • 64
  • 171
  • 244
20
votes
2 answers

How to read until end of file (EOF) using BufferedReader in Java?

I have problem with reading the input until EOF in Java. In here, there are single input and the output consider the input each line. Example: input: 1 2 3 4 5 output: 0 1 0 1 0 But, I have coded using Java, the single output will printed when I…
meisyal
  • 811
  • 1
  • 11
  • 18
19
votes
1 answer

Auto-Detect Character Encoding in Java

Seems to be a fairly hit issue, but I've not yet been able to find a solution; perhaps because it comes in so many flavors. Here it is though. I'm trying to read some comma delimited files (occasionally the delimiters can be a little bit more unique…
Kirk
  • 618
  • 4
  • 7
  • 21
18
votes
2 answers

Reading file from assets directory throws FileNotFoundException

I'm trying to read in a text file of a bunch of words that I want to use for a word game I am writing. This list is stored in the assets directory and is a txt file. But, whenever I attempt to open it, it throws an exception. ListwordList =…
18
votes
3 answers

Parquet Writer to buffer or byte stream

I have a java application that converts json messages to parquet format. Is there any parquet writer which writes to buffer or byte stream in java? Most of the examples, I have seen write to files.
vijju
  • 415
  • 1
  • 5
  • 9
18
votes
8 answers

Reading lines with BufferedReader and checking for end of file

If I have something like this in my code: String line = r.readLine(); //Where r is a bufferedReader How can I avoid a crash if the next line is the end of the file? (i.e. null) I need to read the next line because there may be something there that…
Zippy
  • 3,826
  • 5
  • 43
  • 96
18
votes
6 answers

Reading a resource with BufferedReader

I am trying to read a resource that will be included into a .JAR, but I get a nullPointer for the following: bReader = new BufferedReader(new InputStreamReader( this.getClass().getResourceAsStream("resources/" + fileName))); Using…
Skogen
  • 721
  • 1
  • 11
  • 30
17
votes
4 answers

How do you set a timeout on BufferedReader and PrintWriter in Java 1.4?

How does one set a timeout on a BufferedReader and a PrintWriter created using a socket connection? Here is the code I have for the server right now, which works until either the server or the client crashes: while(isReceiving){ str = null; …
codewario
  • 19,553
  • 20
  • 90
  • 159
17
votes
3 answers

Fastest way to read a file line by line with 2 sets of Strings on each line?

What is the fastest way I can read line by line with each line containing two Strings. An example input file would be: Fastest, Way To, Read One, File Line, By Line .... can be a large file There are always two sets of strings on each line that I…
xiao
  • 1,718
  • 4
  • 23
  • 31
17
votes
3 answers

How do an InputStream, InputStreamReader and BufferedReader work together in Java?

I am studying Android development (I'm a beginner in programming in general) and learning about HTTP networking and saw this code in the lesson: private String readFromStream(InputStream inputStream) throws IOException { StringBuilder output = new…
schv09
  • 995
  • 1
  • 9
  • 15