1
내가 만들고있는 프로그램에서 특정 값을 바이트로 파일에 저장하고 파일을로드하고 각 바이트를 읽으려고합니다. dis.read();
하지만 그럴 때마다, 값이 잘못 나온다. 여기Java에서 파일 쓰기/읽기 바이트
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();
}
및 로딩 코드 :
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();
}
'dis.read();'는 무엇을하나요? –
@SotiriosDelimanolis 나는 파일의 다음 바이트를 읽었을 것이라고 확신한다. 그러나 thats 나만, 나는 바이트에 올 때 아주 좋지 않다. –
[여기] (http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html) javadoc. 예, 다음 바이트를 읽습니다. 당신은'x' 위치에'int'를 쓰지만'byte' 만 읽습니다. 그것들이 어떻게 동등하지 않은지 보시겠습니까? –