aspose file tools*
The moose likes Beginning Java and the fly likes readLine() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "readLine()" Watch "readLine()" New topic
Author

readLine()

sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
i had a text file of 4 sentences,when i use this readLine() method i can read only one line from the file what about the next line ? do ihave to write any logic over there.. plzzz help
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

you can use, readLine()method again for read the next line.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
Do you know how to write a while loop? That is the easiest way. Bertrand Meyer curses this bit of syntax as awkward and hard to read:See, when you get beyond the last line, you get a null line; then the loop will terminate. You are doing two things inside the (). The first thing you are doing is reading the line, then you are checking it isn't null. So reads the line, with the result that your variable line now is equal to the line.
The bit aboutchecks that you haven't got a null line.

Why do you need the extra (), I hear you ask? Because the != operator has a higher precedence than the = operator. The extra pair of () makes the JVM read the line before you test whether it is null.

So now you know how that bit of awkward syntax works, you can use it. It doesn't count 4 lines or anything, but keeps going until it has got the last line.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Does it matter what the end-of-line character(s) are?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

My guess would be that Sandeep is opening a new BufferedReader to read each line; I've seen folks make that mistake before. The trick is to create just one BufferedReader, then call readLine() on that one object as many times as you need. So the "new BufferedReader()" line, you do that just once.


[Jess in Action][AskingGoodQuestions]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
This is how I tend to code this. Warning: I'm just typing this, it's not tested.



SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Steve De Costa
Ranch Hand

Joined: Jun 04, 2007
Posts: 33
When you say

"String sentence = buffer.readLine();"

Whatever context is in the current line of the file, is stored in the string variable 'sentence' as a string. Thus, you could have numbers or any characters in that line.



In Sandeep's case, if there are 4 lines the doing

buffer.readline() 4 times will read the entire file. NOte that readLine() please the cursor for the buffer to read at the beginning of the next line.

Try checking the Reader Class here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40041
    
  28
Originally posted by Kaydell Leavitt:
Does it matter what the end-of-line character(s) are?
I don't think so, no. Probably best to keep them the same as everything else in line with your OS, though:
  • Windows/DOS = \r\n
  • Mac = \r
  • Unix and Linux, and I think Solaris too = \n
  • Roger Chung-Wee
    Ranch Hand

    Joined: Sep 29, 2002
    Posts: 1683
    If files need to be transferred between OSs, I've sometimes used the dos2unix and unix2dos utilities to translate the CR/LF characters.

    Some files have no newlines characters. This will date me horrendously - I remember working on mainframe systems where the files were of fixed, 80-character length, thus mimicking the old punched cards. In the unlikely event of having to process such a file, you would have to append the LF character to each record.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: readLine()