Reading a .dat file into an array in Java

2019-05-30 10:57发布

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.

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!

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

    }

}

3条回答
聊天终结者
2楼-- · 2019-05-30 11:21

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

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

}
查看更多
一夜七次
3楼-- · 2019-05-30 11:35

I'd recommend that you abandon DataInputStream and DataOutputStream.

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

查看更多
戒情不戒烟
4楼-- · 2019-05-30 11:47

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

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.

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();
    }
}
查看更多
登录 后发表回答