Zippy
Zippy

Reputation: 3887

Reading Integer values from a file (Java)

I'm working on a simple level editor for my Android game. I've written the GUI (which draws a grid) using swing. You click on the squares where you want to position a tile and it changes colour. Once you're done, you write everything to a file.

My file consists of something like the following (this is just an example):

enter image description here

I use the asterisks to determine the level number being read and the hyphen to tell the reader to stop reading.

My file reading code is below, Selecting which part to read works OK - for example. if I pass in 2 by doing the following:

readFile(2);

Then it prints all of the characters in the 2nd section

What I can't figure out is, once I've got to the 'start' point, how do I actually read the numbers as integers and not individual characters?

Code

    public void readFile(int level){

        try {
                    //What ever the file path is.
                    File levelFile = new File("C:/Temp/levels.txt");
                    FileInputStream fis = new FileInputStream(levelFile);
                    InputStreamReader isr = new InputStreamReader(fis);    
                    Reader r = new BufferedReader(isr);
                    int charTest;

                    //Position the reader to the relevant level (Levels are separated by asterisks)
                    for (int x =0;x<level;x++){
                    //Get to the relevant asterisk
                    while ((charTest = fis.read()) != 42){
                     }
                    }
                    //Now we are at the correct read position, keep reading until we hit a '-' char
                    //Which indicates 'end of level information'
                    while ((charTest = fis.read()) != 45){

                    System.out.print((char)charTest);   
                    }

                    //All done - so close the file
                    r.close();
                } catch (IOException e) {

                    System.err.println("Problem reading the file levels.txt");
                }


    }

Upvotes: 1

Views: 2265

Answers (4)

Lee Meador
Lee Meador

Reputation: 12985

Perhaps you should consider using readLine which gets all the chars up the the end of line.

This part:

for (int x =0;x<level;x++){
    //Get to the relevant asterisk
    while ((charTest = fis.read()) != 42){
    }
}

Can change to this:

for (int x =0;x<level;x++){
    //Get to the relevant asterisk
    while ((strTest = fis.readLine()) != null) {
        if (strTest.startsWith('*')) {
             break;
        }
    }
}

Then, to read the values another loop:

for (;;) {
    strTest = fls.readLine();
    if (strTest != null && !strTest.startsWith('-')) {
        int value = Integer.parseInt(strTest);
        // ... you have to store it somewhere
    } else {
        break;
    }
}

You also need some code in there to handle errors including a premature end of file.

Upvotes: 1

user1676075
user1676075

Reputation: 3086

Scanner's a good answer. To remain closer to what you have, use the BufferedReader to read whole lines (instead of reading one character at a time) and Integer.parseInt to convert from String to Integer:

// get to starting position
BufferedReader r = new BufferedReader(isr);
...
String line = null;
while (!(line = reader.readLine()).equals("-"))
{
  int number = Integer.parseInt(line);
}

Upvotes: 2

joe776
joe776

Reputation: 1116

If you use the BufferedReader and not the Reader interface, you can call r.readLine(). Then you can simply use Integer.valueOf(String) or Integer.parseInt(String).

Upvotes: 1

Saurabh
Saurabh

Reputation: 7964

I think you should have look at the Scanner API in Java. You can have a look at their tutorial

Upvotes: 0

Related Questions