简体   繁体   English

尝试读取二进制保存的整数时,readInt()不起作用

[英]readInt() not work when try read binary saved integer

i trying save some integers and doubles to txt file in binary format. 我试图保存一些整数,并以二进制格式将其加倍到txt文件。 Numbers are saved correctly i think, but problem is when i try read integer with readInt(). 我认为数字可以正确保存,但是问题是当我尝试使用readInt()读取整数时。 For example, when integer number have value = 12, program output for this integer is 208797696, and program with more integers always end with EOF exception. 例如,当整数值为12时,该整数的程序输出为208797696,并且具有更多整数的程序始终以EOF异常结束。 There´s not problem with readDouble(), it works correct. 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());
}

} }

Your write_int method writes using RandomAccessFile.write() , which writes a byte to the file. 您的write_int方法使用RandomAccessFile.write()进行写入,这会将字节写入文件。 When you use readInt() , you read that single byte plus three bytes of other arbitrary data (because you didn't write them). 当使用readInt() ,您将读取该单个字节以及三个字节的其他任意数据(因为您没有写它们)。

You should use RandomAccessFile.writeInt() in write_int instead. 您应该在write_int使用RandomAccessFile.writeInt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM