本文介绍了读.dat文件到Java中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code,我正在写有两类:writeInts和readInts。我写writeInts随机生成0和1000,并将其输出之间100号到Data.dat文件。

readInts应该打开一个DataInputStream的对象,并在从Data.dat文件的原始数据读和100的整数存储在数组中。我的问题是,我似乎无法正确读取数据。任何帮助,这将大大AP preciated。谢谢!

writeInts:

 进口java.io. *;
公共类WriteInts {
    公共静态无效的主要(字串[] args)抛出IOException
        DataOutputStream类输出=新的DataOutputStream类(新的FileOutputStream(data.dat文件));
        INT NUM = 0 +(int)的(的Math.random());
        INT [] =计数INT新[100];
        的for(int i = 0; I< 100;我++){
            output.writeInt(NUM);
            计数[I] + = NUM​​;            的System.out.println(NUM);
        }
        output.close();
    }}

readInts:

 进口java.io. *;
进口的java.util。*;公共类ReadInts {
    公共静态无效的主要(字串[] args)抛出IOException        //调用文件阅读
        扫描仪扫描=新的扫描仪(新的文件(data.dat文件));
        INT []数据= INT新[100];
        INT I = 0;
        而(scanner.hasNextInt()){
            数据[我++] = scanner.nextInt();            的System.out.println(数据由[i]);            scanner.close();
        }    }}


解决方案

如果你想要写的二进制数据,使用的 DataInputStream所/ DataOutputStream类的。 扫描仪的是文字数据并不能混用。

WriteInts:

 进口java.io. *;公共类WriteInts {
    公共静态无效的主要(字串[] args)抛出IOException
        DataOutputStream类输出=新的DataOutputStream类(新的FileOutputStream(
                data.dat文件));        的for(int i = 0; I< 100;我++){
            output.writeInt(ⅰ);
            的System.out.println(ⅰ);
        }        output.close();
    }}

ReadInts:

 进口java.io.DataInputStream中;
进口java.io.FileInputStream中;
进口java.io.IOException异常;公共类ReadInts {
    公共静态无效的主要(字串[] args)抛出IOException
        DataInputStream以输入=新DataInputStream所(新的FileInputStream(
                data.dat文件));        而(input.available()大于0){
            INT X = input.readInt();
            的System.out.println(X);
        }        input.close();
    }}

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

    }

}
解决方案

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

}

这篇关于读.dat文件到Java中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 20:50