title: Java writing and reading bytes to/from a file
body: <p>In a program I'm making, i save certain values as bytes to a file, and i want to load that file and read each byte <code>dis.read();</code> but each time I do that, the values come out incorrect. Here is my saving code:</p>
  
  <pre><code>file2 = new File(newComputer.file1.toString() + "\\saves\\" + name);
  try {
      FileOutputStream fos = new FileOutputStream(file2 + ".dat");
      DataOutputStream dos = new DataOutputStream(fos);
      dos.writeInt(character.pos.x);
      dos.writeInt(character.pos.y);
      dos.writeInt((int)Minecraft.sx);
      dos.writeInt((int)Minecraft.sy);
      dos.writeInt((int)Minecraft.dir);
      dos.flush();
      dos.writeInt(sky.r);
      dos.writeInt(sky.g);
      dos.writeInt(sky.b);
      dos.writeInt(sky.dayFrame);
      dos.writeInt(sky.changeFrame);
      dos.writeInt(sky.time);
      dos.flush();
      dos.close();
  } catch(Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p>and here is the loading code:</p>
  
  <pre><code>file2 = new File(newComputer.file1.toString() + "\\saves\\" + name);
      try {
          FileInputStream fis = new FileInputStream(file2);
          DataInputStream dis = new DataInputStream(fis);
          int tmp = 0;
          //first get the character's x position
          tmp = dis.read();
          System.out.println("x: " + tmp);
          character.x = tmp;
          //then get the character's y position
          tmp = dis.read();
          System.out.println("y: " + tmp);
          character.y = tmp;
          //then get the camera's x position
          tmp = dis.read();
          System.out.println("sx: " + tmp);
          Minecraft.sx = tmp;
          //then get the camera's y position
          tmp = dis.read();
          System.out.println("sy: " + tmp);
          Minecraft.sy = tmp;
          //then get the character's facing position
          tmp = dis.read();
          System.out.println("facing: " + tmp);
          Minecraft.dir = tmp;
          //then get the sky's RGB colors
          tmp = dis.read();
          System.out.println("r: " + tmp);
          sky.r = tmp;
          tmp = dis.read();
          System.out.println("g: " + tmp);
          sky.g = tmp;
          tmp = dis.read();
          System.out.println("b: " + tmp);
          sky.b = tmp;
          //render the world
          Minecraft.hasStarted = true;
          Minecraft.played++;
      } catch (Exception ex) {
      ex.printStackTrace();
  }
  </code></pre>