0

I was experimenting with BufferedReader to read 1st line file to a string. How do I do this? Also how can I read an entire file to a string? How to read a particular line like readline(int line) without iterating through the previous lines?

File namefile = new File(root, ".name");
FileReader namereader = new FileReader(namefile);
BufferedReader in = new BufferedReader(namereader);
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134
  • http://stackoverflow.com/questions/7175161/how-to-get-file-read-line-by-line see if it can be of help it is something similar you wish to achieve – Devjosh Feb 24 '12 at 14:02
  • If (from your comments to answers) you want to know how access arbitrary lines in a file without reading the whole file up to that point (random access) you should clarify the wording of your question. – Dev Feb 24 '12 at 14:54
  • @dev Sorry, I speak English but it's a bit difficult for me to word this properly as I don't have much of a programming background. Thank you for understanding. – Binoy Babu Feb 24 '12 at 15:41

5 Answers5

5

You can use BufferedReader.readLine() to get the first line.

Note that the next call to readLine() will get you the 2nd line, and the next the 3rd line....

EDIT:
If you want to specify a specific line, as your comment suggest - you might want to use Apache Commons FileUtils, and use: FileUtils.readLines(). It will get you a List<String> which you can handle like any list, including getting a specific line. Note that it has more overhead because it reads the entire file, and populates a List<String> with its lines.

amit
  • 175,853
  • 27
  • 231
  • 333
  • Is it possible to specify a particular line? – Binoy Babu Feb 24 '12 at 14:06
  • @BinoyBabu: Look at my edit, if you want a specific line - you might want to use apache commons - `FileUtils` – amit Feb 24 '12 at 14:11
  • 1
    @Binoy you should edit your question to include that criteria if that is what your looking for. – Dev Feb 24 '12 at 14:59
  • @amit: can you give me example to write specific line on file? - let say that i have file.txt with three lines. line number1. a, number2. b, number3. c. then i want to rewrite line number 3 to d. how to do that? thanks – Gery Aug 24 '14 at 11:17
4

Um, what's wrong with BufferedReader.readLine()?

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

(I don't see any sign of a readFile() method though - what documentation were you looking at?)

Personally I prefer to use FileInputStream wrapped in InputStreamReader instead of FileReader by the way, as otherwise it will always use the platform default encoding - are you sure what's what you want?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It reads the file as a whole right? I can't specify a line like BufferedReader.readLine(1); – Binoy Babu Feb 24 '12 at 14:02
  • @BinoyBabu: No, it reads a line, as documented. Look at the name of the method **readLine**, not **readFile**. – Jon Skeet Feb 24 '12 at 14:03
  • 1
    Since the reader is buffered, BufferedReader#readLine will most probably read more than the first line from the underlying source, even if it's only invoked once. From a performance point of view, this is probably irrelevant in most cases, but there are situations where the functionality difference is relevant. – jarnbjo Feb 24 '12 at 14:06
  • 1
    @jarnbjo: Yes, quite possibly - but that's not the same thing as reading the whole *file* which is what the OP seems to have been concerned about. – Jon Skeet Feb 24 '12 at 14:08
3
final File namefile = new File(root, ".name");
final FileReader namereader = new FileReader(namefile);
final BufferedReader in = new BufferedReader(namereader);
in.readLine();
Triode
  • 11,309
  • 2
  • 38
  • 48
1

If you use the BufferedReader to read the File there should be a Method called

readLine()

wich reads exactly one Line.

http://developer.android.com/reference/java/io/BufferedReader.html

NotANormalNerd
  • 415
  • 3
  • 14
0

See the readline() method of the BufferedReader.

http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html#readLine%28%29

Sly
  • 2,105
  • 5
  • 22
  • 28