readInt()在尝试读取二进制保存的整数时不起作用
readInt() not work when try read binary saved integer
我正在尝试将一些整数和双精度数以二进制格式保存到 txt 文件中。我认为数字保存正确,但问题是当我尝试使用 readInt() 读取整数时。例如,当整数的值为 12 时,该整数的程序输出为 208797696,而具有更多整数的程序总是以 EOF 异常结束。 readDouble() 没有问题,它工作正常。
import java.util.Scanner;
import java.io.*;
public class CandyFruit {
public static void main(String[] args) throws IOException {
int i;
int int_sc ;
double double_sc;
String string_sc;
int seek_w = 0; //seek for writing
String map = "";
Scanner scan = new Scanner(System.in);
try (RandomAccessFile write = new RandomAccessFile("fgh.txt", "rw")) {
for (;;) {
System.out.println("1 - write int, 2 - write double, 0 - break");
i = scan.nextInt();
if (i == 0) break;
System.out.println("Your choice:");
if (i == 1) {
int_sc = scan.nextInt();
write_int(write, int_sc, seek_w);
map = map + "1";
seek_w += 4; //seek growth for int
}
else if (i == 2) {
double_sc = scan.nextDouble();
write_double(write, double_sc, seek_w);
map = map + "2";
seek_w += 8; //seek growth for double
}
}
}
//catch
try (RandomAccessFile read = new RandomAccessFile("fgh.txt", "rw")) {
int seek_r = 0;
for (int o = 0; o < map.length(); o++) {
char c = map.charAt(o);
if (c == '1') {
read_int(read, seek_r);
seek_r += 4; //seek growth for int
}
else if (c == '2') {
read_double(read, seek_r);
seek_r += 8; //seek growth for double
}
}
}
//catch
}
public static void write_int (RandomAccessFile write, int scanned, int s) throws IOException {
write.seek(s);
write.write(scanned);
}
public static void write_double(RandomAccessFile write, double scanned, int s) throws IOException {
write.seek(s);
write.writeDouble(scanned);
}
public static void read_int(RandomAccessFile read, int s) throws IOException {
read.seek(s);
System.out.println("Int is: "+read.readInt());
}
public static void read_double(RandomAccessFile read, int s) throws IOException {
read.seek(s);
System.out.println("Double is: "+read.readDouble());
}
}
您的 write_int
方法使用 RandomAccessFile.write()
写入,它将 字节 写入文件。当您使用 readInt()
时,您读取的是该单个字节加上其他任意数据的三个字节(因为您没有写入它们)。
您应该在 write_int
中使用 RandomAccessFile.writeInt()
。
我正在尝试将一些整数和双精度数以二进制格式保存到 txt 文件中。我认为数字保存正确,但问题是当我尝试使用 readInt() 读取整数时。例如,当整数的值为 12 时,该整数的程序输出为 208797696,而具有更多整数的程序总是以 EOF 异常结束。 readDouble() 没有问题,它工作正常。
import java.util.Scanner;
import java.io.*;
public class CandyFruit {
public static void main(String[] args) throws IOException {
int i;
int int_sc ;
double double_sc;
String string_sc;
int seek_w = 0; //seek for writing
String map = "";
Scanner scan = new Scanner(System.in);
try (RandomAccessFile write = new RandomAccessFile("fgh.txt", "rw")) {
for (;;) {
System.out.println("1 - write int, 2 - write double, 0 - break");
i = scan.nextInt();
if (i == 0) break;
System.out.println("Your choice:");
if (i == 1) {
int_sc = scan.nextInt();
write_int(write, int_sc, seek_w);
map = map + "1";
seek_w += 4; //seek growth for int
}
else if (i == 2) {
double_sc = scan.nextDouble();
write_double(write, double_sc, seek_w);
map = map + "2";
seek_w += 8; //seek growth for double
}
}
}
//catch
try (RandomAccessFile read = new RandomAccessFile("fgh.txt", "rw")) {
int seek_r = 0;
for (int o = 0; o < map.length(); o++) {
char c = map.charAt(o);
if (c == '1') {
read_int(read, seek_r);
seek_r += 4; //seek growth for int
}
else if (c == '2') {
read_double(read, seek_r);
seek_r += 8; //seek growth for double
}
}
}
//catch
}
public static void write_int (RandomAccessFile write, int scanned, int s) throws IOException {
write.seek(s);
write.write(scanned);
}
public static void write_double(RandomAccessFile write, double scanned, int s) throws IOException {
write.seek(s);
write.writeDouble(scanned);
}
public static void read_int(RandomAccessFile read, int s) throws IOException {
read.seek(s);
System.out.println("Int is: "+read.readInt());
}
public static void read_double(RandomAccessFile read, int s) throws IOException {
read.seek(s);
System.out.println("Double is: "+read.readDouble());
}
}
您的 write_int
方法使用 RandomAccessFile.write()
写入,它将 字节 写入文件。当您使用 readInt()
时,您读取的是该单个字节加上其他任意数据的三个字节(因为您没有写入它们)。
您应该在 write_int
中使用 RandomAccessFile.writeInt()
。