我有作业问题我无法得到最终答案。
问题是: 编写一个随机生成100个的程序 使用writeInt(int)方法将二进制文件整数到 DataOutputStream类。关闭文件。使用a打开文件 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();
}
}
答案 0 :(得分: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++)
答案 1 :(得分: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();
}
答案 2 :(得分:0)
尝试将问题分成几部分来组织代码,不要忘记在关闭之前刷新输入流。
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());
}
}
}
}
答案 3 :(得分: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 Class:
import java.io. *; import java.util.Random;
public class CreateBinaryIO {//创建二进制文件public static void main(String [] args)抛出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(); } }