Saurabh Kumar
Saurabh Kumar

Reputation: 16669

Is there a way to tell java BufferedReader to read upto certain point

I am using Java BufferedReader and reading file line by line using bufferedReader.readline().

Is there a way by which I can tell that the line has this much text and Don't read it beyond that.

In other words is there already existing something which sees the line content size and then start reading. Then it would be reading upto the size which is provided?

Upvotes: 3

Views: 1551

Answers (3)

dku.rajkumar
dku.rajkumar

Reputation: 18588

You can use read(char[] buffer,int offet,int length) for this purpose.

Upvotes: 1

GETah
GETah

Reputation: 21449

If you know the size of a line than you can use BufferedReader.read(char[] cbuf, int off, int len) to read what you want and use BufferedReader.skip(long n) to skip the rest.

OR You can read your line and then truncate it to the length you are expecting.

String line = bufferedReader.readLine();
String truncatedLine = line.substring(0, expectedLength);

Upvotes: 0

rfmodulator
rfmodulator

Reputation: 3738

Is this what you mean?

public int read(char[] buf, int offset, int count)

where:

buf - The array into which the chars read should be stored, offset - The offset into the array to start storing chars, count - The requested number of chars to read

Upvotes: 1

Related Questions