Hi,
I'm still sorta new to Java, and so don't have a great understanding of it
so sorry if the answer to this is painfully obvious.
What I'm trying to do is to open a txt file, but only have the first line
of it read into a text area I have.
I can successfully read in the whole document, whoever I can't work out
how to just get the first line read it.
Here is the coding for the section that I'm pretty sure needs to be
adjusted.
------------------
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == openItem) {
String fileName;
fileName = fileField.getText();
try {
inFile = new BufferedReader(
new FileReader(fileName));
documentArea.setText("");
while( ( line = inFile.readLine() ) != null) {
documentArea.append(line + "\n");
}
inFile.close();
}
catch (IOException e) {
System.err.println("Error in file " + fileName + ": "
+ e.toString() );
System.exit(1);
}
}
----------------------
I'm pretty sure its the While statement that reads in the whole file. So
i'm pretty sure I need to edit that bit.
All help is welcome!
Thank you very much in advance!
Gordon Beaton - 18 Apr 2005 12:04 GMT
> I can successfully read in the whole document, whoever I can't work out
> how to just get the first line read it.
> while( ( line = inFile.readLine() ) != null) {
> documentArea.append(line + "\n");
> }
Simply invoke readline() exactly once, instead of using a loop to
invoke it until EOF is reached.

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
BladeZ - 18 Apr 2005 12:10 GMT
Thats the problem though, I'm not sure how to envoke it only once.
Gordon Beaton - 18 Apr 2005 12:39 GMT
> Thats the problem though, I'm not sure how to envoke it only once.
Didn't you write the original code? Just remove the while loop from
around it:
line = inFile.readLine();
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
BladeZ - 18 Apr 2005 13:00 GMT
Thanks alot Gordon.
I tried earlier to remove the While loop but obviusly I took some of the
other code needed along with it. :/
I'm taking a course and we where given that while loop as an example.
I need to apply this coding to a FileDialog now so that when i LOAD a file
it only displays the first line.
I think I should have said that before as they're are some different
elements involed that will probably confuse me.
I'll post if I need any more help.
Thank you again Gordon!
/Constantin