Results 1 to 10 of 10

Thread: Reading Text

  1. #1
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default Reading Text

    Hi all,

    This applies to Java Gaming but there it wouldn't get any attention. I'm trying to read level information from a text file that can be edited easily.

    Here is an example of the text file
    Java Code:
    // Format for levels
    // Level Name
    // Difficulty
    // X Coord, Y Coord, Enemy Type
    
    Level:
    Level1
    
    name:
    Level1_Easy
    50, 50, 1
    80, 50, 1
    110, 50, 1
    
    name:
    Level1_Medium
    50, 50, 1
    80, 50, 1
    110, 50, 1
    
    name:
    Level1_Hard
    50, 50, 1
    80, 50, 1
    110, 50, 1
    I really don't know how to use a BufferedReader to read a specific part.

    I.E. Read all of the Level1_Medium info (The 3 lines after Level1_Medium).

    I tried this but it reads the whole thing.

    Java Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    public class LevelReader
    {
        private BufferedReader level;
        public int[] curLevel;
        public LevelReader()
        {
            try{
                level = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream("level.txt"))));
            }
            catch(Exception e)
            {
                System.out.println("Level file not found (level.txt)");
            }
        }
        /** Read a level starting at the name (ex. Level1_Easy)
        public void readLevel()
        {
            String line; //original line
            String other; //line after removing commas
            try{
            while((line = level.readLine()) != null && line != "name:") //before you get to Level1_Medium from easy or Level1_Hard from medium
            {
                other = line.replace(",", ""); //get rid of the commas
                System.out.println(other); //print edited line for troubleshooting
            }
            } catch(Exception e) { }
        }
        /** Get to the first level (skip comments)
        public void initReader()
        {
            String line;
            try{
            while((line = level.readLine()) != null && line != "name:")
                level.readLine();
            } catch(Exception e) {
                System.out.println("Error reading level file");
            }
        }
    }
    I'm going to end up reading the int values:
    50, 50, 1
    80, 50, 1
    etc into an array for x coordinates, y coordinates, and enemy types.

    I'm really new to reading/writing text files so thanks for your help. If I didn't explain this well please ask for an elaboration.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default

    There's got to be a better way, right? It's going to be a really long file. Reading it all at once would take a considerable amount of time wouldn't it?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by David M. View Post
    There's got to be a better way, right?
    If your file contains records of uniform length, then you can use a RandomAccessFile. If you have a very large amount of data (or even a moderate amount), then you'll probably want to use a data base.


    It's going to be a really long file. Reading it all at once would take a considerable amount of time wouldn't it?
    You can always try it and find out. Also, you don't have to continue reading to the end of the file once you've found and extracted the information that you need.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    How long is really long: mega bytes?
    If the file is a normal sequential text file, there is no way to find data in it without reading it byte by byte and looking at the bytes for what you want.
    If the file were fixed format, then the RandomAccessFile could be used to read the nth byte thru the mth bytes directly.

  6. #6
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default

    Well if I wanted to read this portion to get level 1 easy difficulty stats

    Java Code:
    // Format for levels
    // Level Name
    // Difficulty
    // X Coord, Y Coord, Enemy Type
    
    Level:
    Level1
    
    name:
    Level1_Easy
    50, 50, 1
    80, 50, 1
    110, 50, 1
    How would I do that? BufferedReader doesn't seem to want to read part of it for me.

    Norm: I don't know how big the file is yet. Right now I'm just trying this as an option. I'm sure it's better than hard-coding the values in an array.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    BufferedReader doesn't seem to want to read part of it for me.
    Please explain what is happening when you use it and what the problem you are having.
    You can use a BufferedReader to read some of the file and the stop reading when you have the contents that you want. No need to read past the point where you have found your data. The file can be closed at that point.

    You must always start at the beginning, looking at the contents read until you get to the data you want.
    Unless you are using the RandomAccessFile and you know that your data is at byte 12312 for example.

  8. #8
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

    Default

    Well I call it to stop reading at name:

    Java Code:
    while((line = level.readLine()) != null && line != "name:")
        System.out.println(line);
    I tried that to see if it would stop at name: but it reads the whole file.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default

    line != "name:"
    Use the equals() method when comparing Strings. The == operator is for primitives.

    Did the printout every show: line=name:<
    System.out.println("line=" + line + "<"); // uses delimiters to know if there are any spaces

  10. #10
    David M. is offline Member
    Join Date
    Apr 2011
    Location
    Kansas
    Posts
    26
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. Reading raw text files
    By DrSaturn in forum Android
    Replies: 0
    Last Post: 02-24-2011, 07:07 PM
  3. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 4
    Last Post: 05-17-2010, 08:22 AM
  4. Reading in Text Files
    By blueduiker in forum New To Java
    Replies: 9
    Last Post: 05-17-2010, 05:45 AM
  5. Help reading a text file.
    By Kristoba in forum New To Java
    Replies: 5
    Last Post: 03-11-2010, 08:42 AM

Posting Permissions

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