java 如何写入和读取dat文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32345371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:03:49  来源:igfitidea点击:

How to write and read dat file

javafile

提问by Jason2020

Write a program that asks the user to enter whole numbers (they enter –1 to finish data entry). The numbers are to be written to a binary file. This is to be done in a method called writeBinaryFile(). Then the program should read the numbers from the binary file and display them to the console. This should be done in a method called readBinaryFile(). The main()method should just call writeBinaryFile()then readBinaryFile().

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

My code so far:

到目前为止我的代码:

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();
    }
}

The problem is that when I enter this data:

问题是当我输入这些数据时:

5 2 4 6 8 4 -1

5 2 4 6 8 4 -1

My output is:

我的输出是:

5 2 4 6 8 4 255

5 2 4 6 8 4 255

How can I stop 255 from coming up?

我怎样才能阻止 255 出现?

回答by akhil_mittal

You should avoid adding -1 to input. The problem is at line: number.add(numbers = input.nextInt());in loop.

您应该避免在输入中添加 -1。问题出在 line: number.add(numbers = input.nextInt());in loop。

EDIT:To write binary data you should use DataInputStream/DataOutputStream. You cannot mix it with Scanneras that is primarily for text data. A sample example is:

编辑:要写入二进制数据,您应该使用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();
    }

回答by RajSharma

try doing like this...

尝试这样做...

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

            output.write(numbers);
        }

回答by Dakshinamurthy Karra

Just update @akhul_mittal answer to include reading from standard input using Scanner.

只需更新@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();
}