gpt4 book ai didi

java - 二进制文件IOException

转载 作者:行者123 更新时间:2023-12-01 11:36:39 25 4
gpt4 key购买 nike

所以,我的家庭作业问题已经持续了大约三周,而且我不知道如何解决它。

我必须编写一个程序来搜索 int 类型数字的二进制文件,并让程序从小到大写入它们。该文件只能包含 int 类型的数字。

这是我的程序:

    import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.EOFException;
import java.io.IOException;
import java.util.Arrays;

public class BinaryFile
{
public static void main(String[] args) throws IOException
{
int[] numArray = new int[10];
try
{
DataInputStream inputStream = new DataInputStream(new FileInputStream("numbers.dat")); //creates an object that reads from numbers.dat
System.out.println("Reading the integers from numbers.dat");
int i;
for(i = 0;i < 10;i++) //takes the numbers from number.dat and puts them in numArray
{
numArray[i] = inputStream.readInt();
}
inputStream.close();
}
catch(EOFException e)
{
System.out.println("End of file reached");
}
catch(FileNotFoundException e)
{
System.out.println("numbes.dat not found");
System.exit(0);
}
catch(IOException e)
{
System.out.println("IOException found");
e.printStackTrace;
System.exit(0);
}
catch(Exception e)
{
System.out.println("Other exception found.");
System.exit(0);
}
System.out.println("Re-ordering numbers.");
Arrays.sort(numArray); //reorders the numbers in the array
for(int j = 0; j < 10; j++) //prints out the numbers in the array
{
System.out.println(numArray[j]);
}
}
}

输出如下:

    Reading the integers from numbers.dat
End of file reached
java.io.EOFException
at java.io.DataInputStream.readInt(Unknown Source)
at BinaryFile.main(BinaryFile.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:362)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleMethodCall(ExpressionEvaluator.java:92)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84)
at koala.dynamicjava.tree.StaticMethodCall.acceptVisitor(StaticMethodCall.java:121)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$240(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/15621596.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Re-ordering numbers.
0
0
0
0
540090425
540483633
807416628
825368627
891304224
941634610

基本上,它拒绝从numbers.dat 中读取数字。我不太确定问题是什么。

我在numbers.dat中使用的数字是:5 9 12 3 7 10 34 1 98 42

最佳答案

所以对象输入流并不是您真正想要的。

使用 DataInputStream 会做得更好。

DataInputStream inputStream = new DataInputStream(new FileInputStream("numbers.dat"));

关于java - 二进制文件IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29909712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com