0

我有作业问题,我无法得到最终答案。

问题是: 编写一个程序,使用 DataOutputStream 中的 writeInt(int) 方法将 100 个随机生成的整数写入二进制文件。关闭文件。使用 DataInputStream 和 BufferedInputStream 打开文件。读取整数值,就像文件包含未指定的数字一样(忽略您编写文件的事实)并报告数字的总和和平均值。

我相信我完成了问题的第一部分(写入文件),但我不知道如何报告总和。

到目前为止,我所拥有的

import java.io.*;

public class CreateBinaryIO {
public static void main(String [] args)throws IOException {
    DataOutputStream output = new DataOutputStream(new FileOutputStream("myData.dat"));
    int numOfRec = 0 + (int)(Math.random()* (100 - 0 +1));
    int[] counts = new int[100];
    for(int i=0;i<=100;i++){
        output.writeInt(numOfRec);      
        counts[i] += numOfRec;
    }// Loop i closed
    output.close();
}

}

这个 ReadBinaryIO 类:

import java.io.*;


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

        int value = input.readInt();

        System.out.println(value + " ");
        input.close();

    }
}
4

4 回答 4

0

尝试以下代码,您尝试读取一个整数:

   DataInputStream input = new DataInputStream (new BufferedInputStream(new FileInputStream("myData.dat")));

   int sum = 0;
   for(int i =0; i<=100; i++){
        int value = input.readInt();
        sum += value;
    }

    System.out.println(value + " ");
    input.close();

或者,如果您想动态设置 for 循环的长度,那么

在 myData.dat 文件上创建一个 File 对象,然后将文件大小除以 32 位

   File file = new File("myData.dat");
   int length = file.length() / 32;

   for(int i =0; i <= length;i++)
于 2013-03-24T23:16:09.727 回答
0
int numOfRec = 0 + (int)(Math.random()* (100 - 0 +1));

那不是生成随机数。调查java.util.Random.nextInt()

int[] counts = new int[100];
for(int i=0;i<=100;i++){
   output.writeInt(numOfRec);      
   counts[i] += numOfRec;
}// Loop i closed

这实际上会中断,因为您正在使用i<=100而不是仅使用i<100但我不确定您为什么要填充该数组开始?此外,该代码只写了 101 次相同的数字。该随机数的生成需要在循环内,因此每次都会生成一个新的。

至于回读它,您可以使用这样的循环来循环您的文件:

long total = 0;
while (dataInput.available() > 0) {
  total += dataInput.readInt();
}
于 2013-03-24T23:31:51.277 回答
0

尝试将问题分成几部分以使您的代码有机化,不要忘记在关闭之前刷新 OutputStream。

package javarandomio;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Random;

public class JavaRandomIO {

public static void main(String[] args) {

    writeFile();
    readFile();
}

private static void writeFile() {
    DataOutputStream output=null;
    try {
        output = new DataOutputStream(new FileOutputStream("myData.txt"));
        Random rn = new Random();
        for (int i = 0; i <= 100; i++) {
            output.writeInt(rn.nextInt(100));
        }
        output.flush();
        output.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally{
        try{
            output.close();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}

private static void readFile() {
    DataInputStream input=null;
    try {
        input = new DataInputStream(new FileInputStream("myData.txt"));
        int cont = 0;
        int number = input.readInt();
        while (true) {
            System.out.println("cont =" + cont + " number =" + number);
            if (input.available() == 4) {
                break;
            }
            number = input.readInt();
            cont++;
        }
        input.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally{
        try{
            input.close();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}
}
于 2013-03-25T02:34:36.013 回答
0

到目前为止,我提交了作业,我想我得到了。

/** Munti ... Sha
course code (1047W13), assignment 5 , question 1 , 25/03/2013, 

 This file read the integer values as if the file contained an unspecified number (ignore 
the fact that you wrote the file) and report the sum and average of the numbers.
*/

import java.io.*;
public class ReadBinaryIO {
    public static void main(String [] args)throws ClassNotFoundException, IOException {
        //call the file to read 
        DataInputStream input = new DataInputStream (new BufferedInputStream(new FileInputStream("myData.dat")));
        // total to count the numbers, count to count loops process
        long total = 0;
        int count = 0;
        System.out.println("generator 100 numbers are ");
        while (input.available() > 0) {
          total += input.readInt();
          count ++;
            System.out.println(input.readInt());

        }
        //print the sum and the average
        System.out.println("The sum is " + total);
        System.out.println("The average is " + total/count);

        input.close();

    }

}

CreateBinaryIO 类:

导入java.io.*;导入 java.util.Random;

public class CreateBinaryIO { //创建二进制文件 public static void main(String [] args)throws ClassNotFoundException, IOException { DataOutputStream output = new DataOutputStream(new FileOutputStream("myData.dat")); 随机 randomno = new Random();

for(int i=0;i<100;i++){       output.writeInt(randomno.nextInt(100));     }// Loop i closed   output.close(); }    }
于 2013-03-27T04:12:29.440 回答