将 .dat 文件读入 Java 中的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23141851/
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-08-13 21:02:07  来源:igfitidea点击:

Reading a .dat file into an array in Java

javaarraysdatainputstreamdataoutputstream

提问by Ben

The code that I'm writing has two classes: writeInts and readInts. I wrote writeInts to randomly generate 100 numbers between 0 and 1000 and output them to a data.dat file.

我正在编写的代码有两个类:writeInts 和 readInts。我写了 writeInts 来随机生成 0 到 1000 之间的 100 个数字并将它们输出到 data.dat 文件。

readInts is supposed to open a DataInputStream object and read in the "raw" data from the data.dat file and store the 100 integers in an array. My problem is that I can't seem to read the data correctly. Any help with this would be greatly appreciated. Thanks!

readInts 应该打开一个 DataInputStream 对象并从 data.dat 文件中读取“原始”数据并将 100 个整数存储在一个数组中。我的问题是我似乎无法正确读取数据。对此的任何帮助将不胜感激。谢谢!

writeInts:

写整数:

import java.io.*;


public class WriteInts {
    public static void main(String[] args) throws IOException {
        DataOutputStream output = new DataOutputStream(new FileOutputStream("data.dat"));
        int num = 0 + (int)(Math.random());
        int[] counts = new int[100];
        for(int i=0; i<100; i++) {
            output.writeInt(num);
            counts[i] += num;

            System.out.println(num);
        }
        output.close();
    }

}

readInts:

读入:

import java.io.*;
import java.util.*;

public class ReadInts {
    public static void main(String[] args) throws IOException {

        // call the file to read
        Scanner scanner = new Scanner(new File("data.dat"));
        int[] data = new int[100];
        int i = 0;
        while (scanner.hasNextInt()) {
            data[i++] = scanner.nextInt();

            System.out.println(data[i]);

            scanner.close();
        }

    }

}

采纳答案by Denis Kulagin

If you want to write binary data, use DataInputStream/DataOutputStream. Scanneris for text dataand you can't mix it.

如果要写入二进制数据,请使用DataInputStream/DataOutputStream扫描仪用于文本数据,您不能混合使用。

WriteInts:

写入整数:

import java.io.*;

public class WriteInts {
    public static void main(String[] args) throws IOException {
        DataOutputStream output = new DataOutputStream(new FileOutputStream(
                "data.dat"));

        for (int i = 0; i < 100; i++) {
            output.writeInt(i);
            System.out.println(i);
        }

        output.close();
    }

}

ReadInts:

读取数据:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadInts {
    public static void main(String[] args) throws IOException {
        DataInputStream input = new DataInputStream(new FileInputStream(
                "data.dat"));

        while (input.available() > 0) {
            int x = input.readInt();
            System.out.println(x);
        }

        input.close();
    }

}

回答by duffymo

I'd recommend that you abandon DataInputStreamand DataOutputStream.

我建议你放弃DataInputStreamDataOutputStream

Write the ints one to a line using FileWriterand read them using a BufferedReader, one per line. This is an easy problem.

将整数写入一行FileWriter并使用 a 读取它们BufferedReader,每行一个。这是一个简单的问题。

回答by Denis Kulagin

More. If you want to generate a random number in range from 0 to 1000 (both inclusive), you use this statement:

更多的。如果要生成 0 到 1000(包括两者)范围内的随机数,请使用以下语句:

int rndNum = (int) (Math.random() * 1001);

It works that way: Math.random()generates a double in range from 0 to 1 (exclusive), which you then should map to integer range and floor. If you want you maximal value to be 1000, you multiply it by 1001 - 1001 itself is excluded.

它是这样工作的:Math.random()生成一个范围从 0 到 1(不包括)的双精度值,然后您应该将其映射到整数范围和下限。如果您希望最大值为 1000,则将其乘以 1001 - 1001 本身被排除在外。

Yep, like that:

是的,就像这样:

import java.io.*;

public class WriteRandomInts {
    public static void main(String[] args) throws IOException {
        DataOutputStream output = new DataOutputStream(new FileOutputStream(
                "data.dat"));

        for (int i = 0; i < 100; i++) {
            int rndNum = (int) (Math.random() * 1001);
            output.writeInt(rndNum);
            System.out.println(rndNum);
        }

        output.close();
    }
}