二进制文件 IOException

Binary File 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.run(Unknown Source)
        at sun.rmi.transport.Transport.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$run0(Unknown Source)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda/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"));

使用扫描仪解析文件。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class BinaryFile
{
  public static void main(String[] args) throws IOException
  {
    Integer[] numArray = new Integer[10];
    try 
    {
      Scanner inputStream = new Scanner(new File("D:/abc.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.nextInt();
      }
      inputStream.close();
    }
    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]);
    }
  }
}