繁体   English   中英

如何编写和读取dat文件

[英]How to write and read dat file

编写一个程序,要求用户输入整数(他们输入–1以完成数据输入)。 这些数字将被写入二进制文件。 这可以通过称为writeBinaryFile()的方法来完成。 然后,程序应从二进制文件中读取数字并将其显示在控制台中。 这应该在称为readBinaryFile()的方法中完成。 main()方法应该只调用writeBinaryFile()然后再调用readBinaryFile()

到目前为止,我的代码:

public static void main(String[] args) throws FileNotFoundException, IOException {
    System.out.println("Please enter whole numbers then enter -1 to data entry.");
    writeBinaryFile();
    readBinaryFile();
}

/**
 * This method opens a binary file and writes the contents
 * of an int array to the file.
 */
private static void writeBinaryFile() throws IOException, FileNotFoundException
{
    Scanner input = new Scanner(System.in);
    ArrayList<Integer> number = new ArrayList<Integer>();
    try
    {
        FileOutputStream output = new FileOutputStream("Files//Numbers.dat");
        int numbers = 0;

        while(numbers != -1)
        {
            number.add(numbers = input.nextInt());

            output.write(numbers);
        }
        output.close();
    }
    catch(IOException ex)
    {
        ex.getMessage();
    }
}

private static void readBinaryFile() throws IOException
{
    FileInputStream input = new FileInputStream("Files//Numbers.dat");
    int value;
    value = input.read();
    System.out.print("The numbers in the file are:  ");
    try
    {
        while(value != -1)
        {
            System.out.print(value +" ");
            value = input.read();
        }
        input.close();
    }
    catch(IOException ex)
    {
        ex.getMessage();
    }
}

问题是当我输入此数据时:

5 2 4 6 8 4 -1

我的输出是:

5 2 4 6 8 4 255

如何阻止255出现?

您应该避免在输入中加上-1。 问题出在以下行: number.add(numbers = input.nextInt()); 循环中。

编辑:要写入二进制数据,您应该使用DataInputStream/DataOutputStream 您不能将其与Scanner混合使用,因为它主要用于文本数据。 一个示例示例是:

public static void main(String[] args) throws IOException {
    writeNumbers();
    readNumbers();
}

private static void writeNumbers() throws IOException {
        DataOutputStream output = new DataOutputStream(new FileOutputStream("C://Numbers.dat"));
        for (int i = 0; i < 10; i++) {
            output.writeInt(i);
            System.out.println(i);
        }
        output.close();
    }

private static void readNumbers() throws IOException{
        DataInputStream input = new DataInputStream(new FileInputStream("C://Numbers.dat"));
        while (input.available() > 0) {
            int x = input.readInt();
            System.out.println(x);
        }
        input.close();
    }

尝试像这样...

 while(numbers = input.nextInt())!=-1)
        {
            number.add(numbers);

            output.write(numbers);
        }

只需更新@akhul_mittal答案即可包括使用扫描仪从标准输入中读取的内容。

public static void main(String[] args) throws IOException {
    writeNumbers();
    readNumbers();
}

private static void writeNumbers() throws IOException {
    DataOutputStream output = new DataOutputStream(new FileOutputStream("C://Numbers.dat"));
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt();
    while (n != -1) {
        output.writeInt(n);
        n = scanner.nextInt();
    }
    output.close();
}

private static void readNumbers() throws IOException {
    DataInputStream input = new DataInputStream(new FileInputStream("C://Numbers.dat"));
    while (input.available() > 0) {
        int x = input.readInt();
        System.out.println(x);
    }
    input.close();
}

暂无
暂无

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

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