二进制文件ioexception

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

所以,我已经有三个星期的家庭作业问题了,我不知道怎么解决它。
我必须写一个程序,将搜索一个二进制文件的数字类型int和程序写他们从最小到最大。文件只能包含int类型的数字。
以下是我的程序:

  1. import java.io.DataInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.EOFException;
  5. import java.io.IOException;
  6. import java.util.Arrays;
  7. public class BinaryFile
  8. {
  9. public static void main(String[] args) throws IOException
  10. {
  11. int[] numArray = new int[10];
  12. try
  13. {
  14. DataInputStream inputStream = new DataInputStream(new FileInputStream("numbers.dat")); //creates an object that reads from numbers.dat
  15. System.out.println("Reading the integers from numbers.dat");
  16. int i;
  17. for(i = 0;i < 10;i++) //takes the numbers from number.dat and puts them in numArray
  18. {
  19. numArray[i] = inputStream.readInt();
  20. }
  21. inputStream.close();
  22. }
  23. catch(EOFException e)
  24. {
  25. System.out.println("End of file reached");
  26. }
  27. catch(FileNotFoundException e)
  28. {
  29. System.out.println("numbes.dat not found");
  30. System.exit(0);
  31. }
  32. catch(IOException e)
  33. {
  34. System.out.println("IOException found");
  35. e.printStackTrace;
  36. System.exit(0);
  37. }
  38. catch(Exception e)
  39. {
  40. System.out.println("Other exception found.");
  41. System.exit(0);
  42. }
  43. System.out.println("Re-ordering numbers.");
  44. Arrays.sort(numArray); //reorders the numbers in the array
  45. for(int j = 0; j < 10; j++) //prints out the numbers in the array
  46. {
  47. System.out.println(numArray[j]);
  48. }
  49. }
  50. }

输出如下:

  1. Reading the integers from numbers.dat
  2. End of file reached
  3. java.io.EOFException
  4. at java.io.DataInputStream.readInt(Unknown Source)
  5. at BinaryFile.main(BinaryFile.java:23)
  6. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  7. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  8. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  9. at java.lang.reflect.Method.invoke(Unknown Source)
  10. at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
  11. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  12. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  13. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  14. at java.lang.reflect.Method.invoke(Unknown Source)
  15. at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:362)
  16. at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleMethodCall(ExpressionEvaluator.java:92)
  17. at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84)
  18. at koala.dynamicjava.tree.StaticMethodCall.acceptVisitor(StaticMethodCall.java:121)
  19. at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
  20. at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
  21. at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
  22. at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
  23. at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
  24. at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
  25. at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
  26. at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
  27. at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:246)
  28. at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:220)
  29. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  30. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  31. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  32. at java.lang.reflect.Method.invoke(Unknown Source)
  33. at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
  34. at sun.rmi.transport.Transport$1.run(Unknown Source)
  35. at sun.rmi.transport.Transport$1.run(Unknown Source)
  36. at java.security.AccessController.doPrivileged(Native Method)
  37. at sun.rmi.transport.Transport.serviceCall(Unknown Source)
  38. at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
  39. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
  40. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$240(Unknown Source)
  41. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/15621596.run(Unknown Source)
  42. at java.security.AccessController.doPrivileged(Native Method)
  43. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
  44. at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  45. at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  46. at java.lang.Thread.run(Unknown Source)
  47. Re-ordering numbers.
  48. 0
  49. 0
  50. 0
  51. 0
  52. 540090425
  53. 540483633
  54. 807416628
  55. 825368627
  56. 891304224
  57. 941634610

基本上,它拒绝从numbers.dat读取数字。我不太确定问题出在哪里。
我在numbers.dat中使用的数字是:5 9 12 3 7 10 34 1 98 42

nvbavucw

nvbavucw1#

要解析文件,请使用scanner。

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. public class BinaryFile
  7. {
  8. public static void main(String[] args) throws IOException
  9. {
  10. Integer[] numArray = new Integer[10];
  11. try
  12. {
  13. Scanner inputStream = new Scanner(new File("D:/abc.dat")); //creates an object that reads from numbers.dat
  14. System.out.println("Reading the integers from numbers.dat");
  15. int i;
  16. for(i = 0;i < 10;i++) //takes the numbers from number.dat and puts them in numArray
  17. {
  18. numArray[i] = inputStream.nextInt();
  19. }
  20. inputStream.close();
  21. }
  22. catch(FileNotFoundException e)
  23. {
  24. System.out.println("numbes.dat not found");
  25. System.exit(0);
  26. }
  27. catch(IOException e)
  28. {
  29. System.out.println("IOException found");
  30. e.printStackTrace();
  31. System.exit(0);
  32. }
  33. catch(Exception e)
  34. {
  35. System.out.println("Other exception found.");
  36. System.exit(0);
  37. }
  38. System.out.println("Re-ordering numbers.");
  39. Arrays.sort(numArray); //reorders the numbers in the array
  40. for(int j = 0; j < 10; j++) //prints out the numbers in the array
  41. {
  42. System.out.println(numArray[j]);
  43. }
  44. }
  45. }
展开查看全部
u5i3ibmn

u5i3ibmn2#

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

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

相关问题