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

How to sum numbers that are in columns from a text file in Java?

I am trying to read from a file with multiple lines and then add the numbers in columns. However, I struggle to separate numbers into int variables from same lines so that I could add them eventually. Basically, I would like to know how to read from…
kuk
  • 127
  • 1
  • 8
0
votes
2 answers

Java - Multithreading and Files Question

I have one text file that needs to be read by two threads, but I need to make the reading sequentially. Example: Thread 1 gets the lock and read first line, lock is free. Thread 2 gets the lock and read line 2, and so goes on. I was thinking in…
Samth
  • 113
  • 1
  • 1
  • 5
0
votes
0 answers

How to search for a string and display of the particular string stored in txt file?

The project that I am working on is an income and expense system where there will be three options: Add income and expense of a month Search income and expense by month Display the report of a year For the first one, I asked the user to input the…
0
votes
1 answer

FileReader has also buffered method

I just found this FileReader reader = new FileReader("SomePath"); char[] buf = new char[100]; reader.read(buf); So why should I use BufferedReader if FileReader itself has a method to read more than 1 byte.
Alicia24
  • 21
  • 2
0
votes
0 answers

Error while entering input using bufferedreader in java

import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) throws java.lang.Exception { //code BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); …
0
votes
2 answers

How does a BufferedReader's read(char[]) work?

I was searching on the internet and came across this code to read files from a file and convert it into a string. But I don't understand how in.read(arr) is reading all the contents of a file at once. import java.util.Scanner; import…
Robin
  • 3
  • 2
0
votes
1 answer

How to read the content of all of the text files of a directory using Java?

My problem is that I want to iterate all the text files in a directory using Java but whenever I start for loop over files, it always results in half processing, like for example:- I have 80 files and I want to read content of each file then I start…
0
votes
0 answers

Buffer reader adding "[?1034h" as last line when executing Linux commands in Java

I'm executing a linux command in java using processBuilder but it adds [?1034h as the last line. It is printing all lines but after my expected last line, it adds another line with those characters. My code: ProcessBuilder processBuilder = new…
jasvik
  • 75
  • 7
0
votes
2 answers

NumberFormatException when I try to read text file of integers into a int[]

I am very confused. I am trying to get 10 integers on 10 lines of a text file into an int[]. I have tried a few different ways, but my latest attempt is to use a for loop and parseInt on the BufferedReader.readLine() for each line of the file. This…
Jackson
  • 31
  • 5
0
votes
1 answer

Read lines from text file into console using BufferedReader and FileReader and applying try-with-resources in Kotlin

Here, I am reading into console lines from a text file. I am using BufferedReader and FileReader classes for the purpose. I am also using the try-with-resources feature of Java 7 To accomplish the same I have the following Java code - try…
0
votes
1 answer

Is there any parsing when reading 'clean' text file using Scanner?

I know that: Parsing is the process of turning some kind of data into another kind of data. But then I also came across this difference between Scanner and BufferedReader: BufferedReader is faster than Scanner because BufferedReader does not need…
Stefan
  • 969
  • 6
  • 9
0
votes
2 answers

Why is parsing CSV spreadsheet in Java throwing a NumberFormatException?

I am trying to read a comma-separated value file but I can't seem to get my code to work. Any clue where my code is incorrect? Notes: Trying to read only the Price column and make sure the values are not NULL, N/A, less than 80 and greater than…
BigO
  • 334
  • 1
  • 3
  • 16
0
votes
1 answer

Parsing Excel in Java - index out of bounds

I am trying to read an Excel file but I cant seem to get my code to work. The error it returns has to with Array out of bounds, but whatever number It doesnt work. Any clue where my code is incorrect? Notes: Trying to read only the Price column and…
BigO
  • 334
  • 1
  • 3
  • 16
0
votes
5 answers

How Can I Read 2 lines From txt File

Hey Guys My I am Dealing With This Issue I Want To merge to line from txt file and show them in Arraylist So My Code Is That And I Want To Skip The -- Line To Show in Arraylist.. private List getQuotes(){ List quotes = new…
0
votes
2 answers

How to read large .txt file in chunks of 1000 lines

I want to read and process 1000 line chunks from a file repeatedly till file end. Path pp = FileSystems.getDefault().getPath("logs", "access.log"); final int BUFFER_SIZE = 1024*1024; //this is actually bytes FileInputStream fis = new…
Rohit
  • 9
  • 2
1 2 3
99
100