二进制文件IOException

时间:2015-04-28 03:22:13

标签: java binaryfiles ioexception

所以,我现在已经有三个星期的家庭作业问题了,我无法弄清楚如何修复它。

我必须编写一个程序来搜索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

2 个答案:

答案 0 :(得分:1)

所以对象输入流并不是你想要的。

使用DataInputStream可以做得更好。

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

答案 1 :(得分:0)

解析文件使用扫描程序。

Enter the number of the file you want to select:
1) someDir/somefile1.txt
2) someDir/somefile2.txt
3) someDir/somefile3.txt
#? 2
You picked someDir/somefile2.txt (2)