Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Greetings, I'm kind of rusty with my java and I need some help with reading in values from a file. The file being read from is in the format: Code: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2005
    Location
    At a rock house in Oak Cliff...
    Posts
    87

    Java File I/O


    Greetings,
    I'm kind of rusty with my java and I need some help with reading in values from a file. The file being read from is in the format:
    Code:
    string string int string string int
    for each line. I'm lost on how to set this up and any help is always appreciated.

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Code:
    import java.io.*;
    
    BufferedReader bfrRead = new BufferedReader(new FileReader("/home/user/temp/the_file"));
    
    try
    {
         while(bfrRead.open())
         {
              String theLine = bfrRead.readLine();
    
              // Do Stuff
         }
    
         bfrRead.close();
    }
    
    // Now catch the FileNotFoundException and IOException
    Basically, we wrap the BufferedReader around the FileReader. FileReader takes either a String with the file's location or an instance of the File class.

    Then, while lines are still there, we read them in, line by line. After all lines are read, we close that BufferedReader.

    Make sure to catch your Exceptions. I'm 99% sure that only the two I named need to be caught.


    And, of course, be sure to import java.io.*;


    The 1.4.2 API is at:

    http://java.sun.com/j2se/1.4.2/docs/api/

    If it helps

  3. #3
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    You might also want to use the java.util.StringTokenizer class to seperate the tokens on a line. (I'd recommend reading the API for it; it's quite useful)

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Javasnob has a good idea.

    Definitely check out the API. But as a brief overview:

    StringTokenizer takes a String and separates it by whitespace into "Tokens". You can then manipulate the Tokens separately:

    Code:
    String theString = new String("a b c d e f");
    StringTokenizer st = new StringTokenizer(theString);
    
    while(st.hasTokens())
    {
        String theToken = st.nextToken();
        System.out.println(theToken);
    }

    Unrelated to that, but as I just remembered, remember that I/O is done as Strings. So even though you have a string string int string string int format, you will receive them all as Strings.

    To change the Strings to ints, you'll need to do:

    Code:
    int theNum = Integer.parseInt(theStringWithIntValue);
    That can throw a NumberFormatException however, be warned.

  5. #5
    Just Joined!
    Join Date
    Feb 2005
    Location
    At a rock house in Oak Cliff...
    Posts
    87
    Thank you for all your help. I've implemented your suggestions into my code and all is well with the exception of one: for
    Code:
     while(bfrRead.open())
    I get the error:

    Code:
    The method open() is undefined for the tyoe BufferedReader

  6. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Gadzooks!

    It's actually

    Code:
    while(bfrRead.ready())
    Oopsie.

  7. #7
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    I always liked doing this with BufferedReader:

    Code:
    BufferedReader reader = ...;
    String line;
    while((line = reader.readLine()) != null) {
         ...
    }
    But that's just another suggestion to cut down on lines of code.

  8. #8
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    I thought Java was supposed to be a good language. It certainly makes a meal of doing something that's trivial in a proper language like C.

  9. #9
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    J2SE 5.0 makes this easier, actually:

    Code:
    Scanner in = new Scanner(new FileInputStream(file));
    while(in.hasNext()) {
         String str = in.next();
         // Process the string
    }

  10. #10
    Linux Newbie
    Join Date
    Jul 2005
    Location
    Chd,India
    Posts
    135
    how about using perl regular expressions?
    The strong shall live and the weak will die
    In the end,only the fittest survive in this world
    - Shishio Makoto

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •