title: Reading Integer values from a file (Java)
body: <p>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.</p>
  
  <p>My file consists of something like the following (this is just an example):</p>
  
  <p><img src="https://i.stack.imgur.com/YEszB.png" alt="enter image description here"></p>
  
  <p>I use the asterisks to determine the level number being read and the hyphen to tell the reader to stop reading.</p>
  
  <p>My file reading <strong>code</strong> is below, Selecting which part to read works OK - for example. if I pass in 2 by doing the following:</p>
  
  <pre><code>readFile(2);
  </code></pre>
  
  <p>Then it prints all of the characters in the 2nd section</p>
  
  <p>What I can't figure out is, once I've got to the 'start' point, how do I actually read the numbers as <em>integers</em> and <strong>not</strong> individual characters?</p>
  
  <p><strong>Code</strong></p>
  
  <pre><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&lt;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");
                  }
  
  
      }
  </code></pre>