二进制文件ioexception

hs1ihplo  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(504)

所以,我已经有三个星期的家庭作业问题了,我不知道怎么解决它。
我必须写一个程序,将搜索一个二进制文件的数字类型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

nvbavucw

nvbavucw1#

要解析文件,请使用scanner。

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]);
    }
  }
}
u5i3ibmn

u5i3ibmn2#

所以对象输入流并不是您真正想要的。
使用datainputstream会更好。

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

相关问题