Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

Reading firstline of a .txt file.

Thread view: 
BladeZ - 18 Apr 2005 11:15 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.