首页 > 解决方案 > Java 文件输入错误 java.io.StreamCorruptedException:无效的流标头:22427275

问题描述

这是我的错误运行:

Welcome to Hero University
Teach well
java.io.StreamCorruptedException: invalid stream header: 22427275
    at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:964)
    at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:403)
    at university_info.UniversityDriver.main(UniversityDriver.java:24)

我的代码:我还有一个 txt 文件,我正在尝试从中读取信息。但我想知道怎么做。

package university_info;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class UniversityDriver{

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       University u_city=new University("Hero","Teach well");
       System.out.println("Welcome to "+u_city.universityName+" University");  
   System.out.println(u_city.motto);
     
  
   // retrieving data from external file on startup.....
   ArrayList<Person> al=new ArrayList<Person>();
   try{
       FileInputStream fis = new FileInputStream("fileName.txt");
       ObjectInputStream ois = new ObjectInputStream(fis);
       al = (ArrayList<Person>) ois.readObject();
       ois.close();
       }catch(FileNotFoundException ex){
           System.out.println("File not found exception");
       }catch(IOException ex){
           ex.printStackTrace();
       }catch(ClassNotFoundException ex){
          
       }
  
   Object[] array=new Object[al.size()];
   array=al.toArray(array);
  
   for(int i=0;i<array.length;i++){
       u_city.people[i]=(Person)array[i];
   }
      
   System.out.println("What would you like to do");
   System.out.println("Enter 'hire' to hire a new faculty member. ");
   System.out.println("Enter 'admit' to admit a new student ");
   System.out.println("Enter 'find student' to list information about a student");
   System.out.println("Enter 'find faculty' to list information about a faculty member");
   System.out.println("Enter 'list students' to list the names of all students.");
   System.out.println("Enter 'list faculty' to list the names of faculty members");
   System.out.println("Enter 'quit' to end this program and save data");
  
   //-------------------->
  
  
   Scanner in =new Scanner(System.in);
   boolean run= true;
   while(run){
      
   String s= in.nextLine();
   if(s.equals("quit")){
       run=false;
       //save data in extenal file
   try{
      FileOutputStream fos = new FileOutputStream("fileName.txt");
          ObjectOutputStream oos = new ObjectOutputStream(fos);
          oos.writeObject(u_city.people);
          oos.close();
      }catch(IOException ex){
          ex.printStackTrace();
      }
   }
     
   if(s.equals("hire")){
       u_city.hire();
   }
   if(s.equals("admit")){
       u_city.admit();
   }
   if(s.equals("find_student")){
       System.out.println("What is the student's first name?");
       String fn=in.nextLine();
       System.out.println("What is the student's last name?");
       String ln=in.nextLine();
       Student temp=u_city.findStudent(fn, ln);
       if(temp==null){
           System.out.println("Student not found");
       }else{
           temp.Details();
       }
   }
   if(s.equals("find_faculty")){
       System.out.println("What is the faculty's first name?");
       String fn=in.nextLine();
       System.out.println("What is the faculty's last name?");
       String ln=in.nextLine();
       Faculty temp=u_city.findFaculty(fn, ln);
       if(temp==null){
           System.out.println("faculty not found");
       }
   }
  
   if(s.equals("list_student")){
       Person[] temp= u_city.getStudents();
       for(int i=0;i<=temp.length;i++){
           System.out.println(temp[i].firstName+" "+temp[i].lastName);
       }
         
   }
   if(s.equals("list_faculty")){
       Person[] temp= u_city.getFaculty();
       for(int i=0;i<=temp.length;i++){
           System.out.println(temp[i].firstName+" "+temp[i].lastName);
       }     
         
   }
   }
  
}
}  

这是我的项目提示:

您将创建一个程序来添加、查询和存储有关学生和教师的大学信息。您的程序必须具有以下类:UniversityDriver、大学、人员、学生和教师。这些类必须遵循本文档中提供的规范。您必须在截止日期或之前在 Canvas 提交页面上提交与这些类对应的 java 文件。您的项目提交必须经过编译才能成为有效的提交。未编译的提交将不会获得任何功劳。

初始大学数据:

Faculty: (first name, last name, month-birth, day-birth, year-birth, course 1, course 2, …
course n )
"Bruce", "Wayne", 9, 27, 1995, “Bayesian Logic”, “Artificial Intelligence”, “Hardware 
Design”
“Diana","Prince", 11, 5, 2006, "Hardware Design", “FPGA Programming”, “Embedded
Systems”
“Barbara”, “Gordon”, 5, 23, 1980, “Probability”, “Signal Processing”, “Advance
Algorithms”
"Charles","Xavier", 11, 5, 1966, “Signal Processing”, “Embedded Systems”, “Parallel
Programming”
Students: (first name, last name, month-birth, day-birth, year-birth, major)
"Billy", "Baston", 7, 12, 1990, "Information Analytics"
"Carol", "Danvers", 4, 9, 1992, "Quantum Computing"
"Clark", "Kent", 5, 5, 1994 , "Hardware Architecture"
"Kara", "Zorel", 4, 13, 1989, "Hardware Architecture"
"Peter","Parker", 6, 25, 1997, "Quantum Computing"
"Tony","Stark", 2, 2, 2004, "Hardware Architecture"
"Stephen","Strange", 12, 15, 1976, "Quantum Computing"
"Bruce","Banner", 9, 9, 2000, “Undecided”

我的 TXT 文件位于此处,包含此信息

地点

有了这个信息:

"Bruce", "Wayne", 9, 27, 1995, “Bayesian Logic”, “Artificial Intelligence”, “Hardware Design” 
“Diana","Prince", 11, 5, 2006, "Hardware Design", “FPGA Programming”, “Embedded Systems” 
“Barbara”, “Gordon”, 5, 23, 1980, “Probability”, “Signal Processing”, “Advance Algorithms” 
"Charles","Xavier", 11, 5, 1966, “Signal Processing”, “Embedded Systems”, “Parallel Programming” 

标签: javaarrayseclipsedata-structuresreadfile

解决方案


您正在尝试使用ObjectInputStream来读取不是使用创建的文件ObjectOutputStream。这种方法行不通。

提示:数字22427275是十六进制的。当您通过对 ASCII 进行十六进制查找对其进行解码时,您会得到字符'"''B''r''u'. 是不是很眼熟?

有效的对象序列化将以十六进制开头aced0005。(0xaced是对象序列化格式的“幻数”,0x0005是当前格式的版本号。)

我建议您使用ScannerCSV 阅读器库来读取文件。


推荐阅读