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
38
votes
1 answer

Read and Write Text in ANSI format

Please have a look at the following code import java.io.*; public class CSVConverter { private File csvFile; private BufferedReader reader; private StringBuffer strBuffer; private BufferedWriter writer; int startNumber = 0; …
PeakGen
  • 21,894
  • 86
  • 261
  • 463
36
votes
6 answers

How to read BufferedReader faster

I want to optimize this code: InputStream is = rp.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String text = ""; String aux = ""; while ((aux = reader.readLine()) != null) { text…
Cata
  • 11,133
  • 11
  • 65
  • 86
31
votes
2 answers

Difference between BufferedReader and BufferedInputStream

What are the differences between BufferedReader , BufferedInputStream and Scanner in java? BufferedReader reads the text and BufferedInputStream reads byte. Is there any difference other than this?
user1357722
  • 7,088
  • 13
  • 34
  • 43
29
votes
2 answers

Java using scanner enter key pressed

I am programming using Java. I am trying write code which can recognize if the user presses the enter key in a console based program. How can I do this using java. I have been told that this can be done using either Scanner or, buffered input…
H J
  • 369
  • 2
  • 6
  • 10
28
votes
6 answers

What are mark and reset in BufferedReader?

I would like to know what are the mark() and reset() methods of BufferedReader? How do I use them? I read the Javadoc but as a beginner I was unable to understand it.
saurabh ranu
  • 1,351
  • 6
  • 18
  • 24
28
votes
3 answers

What is the buffer size in BufferedReader?

What is the sense of buffer size in the constructor? BufferedReader(Reader in, int size) As i have written the program: import java.io.*; class bufferedReaderEx{ public static void main(String args[]){ InputStreamReader isr = null; …
codeomnitrix
  • 4,179
  • 19
  • 66
  • 102
28
votes
4 answers

Should I buffer the InputStream or the InputStreamReader?

What are the differences (if any) between the following two buffering approaches? Reader r1 = new BufferedReader(new InputStreamReader(in, "UTF-8"), bufferSize); Reader r2 = new InputStreamReader(new BufferedInputStream(in, bufferSize), "UTF-8");
bdkosher
  • 5,753
  • 2
  • 33
  • 40
27
votes
6 answers

Maximum line length for BufferedReader.readLine() in Java?

I use BufferedReader's readLine() method to read lines of text from a socket. There is no obvious way to limit the length of the line read. I am worried that the source of the data can (maliciously or by mistake) write a lot of data without any…
daphshez
  • 9,272
  • 11
  • 47
  • 65
27
votes
1 answer

Read All Lines of BufferedReader in Scala into a String

How can I read all of a BufferedReader's lines and store into a String? val br = new BufferedReader(...) val str: String = getAllLines(br) // getAllLines() -- is where I need help Similar to this question.
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
27
votes
7 answers

Using BufferedReader.readLine() in a while loop properly

So I'm having an issue reading a text file into my program. Here is the code: try { InputStream fis = new FileInputStream(targetsFile); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); //while(br.readLine()!=null){ …
billg118
  • 561
  • 1
  • 5
  • 13
26
votes
6 answers

BufferedReader to skip first line

I am using the following bufferedreader to read the lines of a file, BufferedReader reader = new BufferedReader(new FileReader(somepath)); while ((line1 = reader.readLine()) != null) { //some code } Now, I want to skip reading the first line…
Code
  • 785
  • 3
  • 8
  • 19
25
votes
8 answers

Most Robust way of reading a file or stream using Java (to prevent DoS attacks)

Currently I have the below code for reading an InputStream. I am storing the whole file into a StringBuilder variable and processing this string afterwards. public static String getContentFromInputStream(InputStream inputStream) // public static…
Unni Kris
  • 3,081
  • 4
  • 35
  • 57
24
votes
3 answers

Reset buffer with BufferedReader in Java?

I am using class BufferedReader to read line by line in the buffer. When reading the last line in the buffer, I want to start reading from the beginning of the buffer again. I have read about the mark() and reset(), I am not sure its usage but I…
ipkiss
  • 13,311
  • 33
  • 88
  • 123
22
votes
1 answer

Making io.BufferedReader from sys.stdin in Python2

How can I make a BufferedReader object from a standard file object, like sys.stdin or what you get from 'open'? (Background: I need a peek() method, which the standard file objects fail at having. Any suggestions to solve this issue are also…
EvanED
  • 947
  • 6
  • 22
22
votes
7 answers

BufferedReader: read multiple lines into a single string

I'm reading numbers from a txt file using BufferedReader for analysis. The way I'm going about this now is- reading a line using .readline, splitting this string into an array of strings using .split public InputFile () { fileIn = null; …
S_Wheelan
  • 267
  • 2
  • 5
  • 8