Java文件输入错误 java.io.StreamCorruptedException:无效的流头:22427275

huangapple go评论75阅读模式
英文:

Java File input error java.io.StreamCorruptedException: invalid stream header: 22427275

问题

这是我的错误运行:

欢迎来到英雄大学
教学得当
java.io.StreamCorruptedException: 无效的流头: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)

我的代码:我还有一个文本文件,我正试图从中读取信息,但我无法弄清楚如何做。

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);
     
  
       // 从外部文件检索数据...
       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("文件未找到异常");
       }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("您想要做什么?");
       System.out.println("输入 'hire' 来雇佣新的教职员工。");
       System.out.println("输入 'admit' 来录取新的学生。");
       System.out.println("输入 'find student' 来列出关于学生的信息。");
       System.out.println("输入 'find faculty' 来列出关于教职员工的信息。");
       System.out.println("输入 'list students' 来列出所有学生的姓名。");
       System.out.println("输入 'list faculty' 来列出教职员工的姓名。");
       System.out.println("输入 'quit' 来结束程序并保存数据。");
  
       //-------------------->
  
       Scanner in =new Scanner(System.in);
       boolean run= true;
       while(run){
  
           String s= in.nextLine();
           if(s.equals("quit")){
               run=false;
               // 在外部文件中保存数据
               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("学生的名字是什么?");
               String fn=in.nextLine();
               System.out.println("学生的姓氏是什么?");
               String ln=in.nextLine();
               Student temp=u_city.findStudent(fn, ln);
               if(temp==null){
                   System.out.println("未找到学生");
               }else{
                   temp.Details();
               }
           }
           if(s.equals("find_faculty")){
               System.out.println("教职员工的名字是什么?");
               String fn=in.nextLine();
               System.out.println("教职员工的姓氏是什么?");
               String ln=in.nextLine();
               Faculty temp=u_city.findFaculty(fn, ln);
               if(temp==null){
                   System.out.println("未找到教职员工");
               }
           }
  
           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);
               }     
                 
           }
       }
  
   }
}
英文:

This is my error run:

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.&lt;init&gt;(ObjectInputStream.java:403)
at university_info.UniversityDriver.main(UniversityDriver.java:24)

MY Code: Also I have a txt file that I am trying to read the info from. but I cat figure out how.

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

this is my project prompt:

You will be creating a program that adds, queries, and stores university information on
students and faculty.
You program must have the following classes: UniversityDriver, University, Person,
Student, and Faculty. These classes must follow the specification provided in this
document.
You must submit the java files corresponding to these classes on the Canvas submission
page on or before the due date. Your project submission must compile in order to be a
valid submission. Submissions that do not compile will not receive any credit.

Initial University Data:

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

MY TXT FILE IS LOCATED HERE WITH THIS INFO

Java文件输入错误 java.io.StreamCorruptedException:无效的流头:22427275

WITH THIS INFO:

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

答案1

得分: 2

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

提示:数字 22427275 是十六进制表示法。当你通过十六进制转ASCII查找来解码它时,你会得到字符 '&quot;''B''r''u'。这看起来熟悉吗?

一个有效的对象序列化将以十六进制 aced0005 开头。(其中 0xaced 是对象序列化格式的“魔术数字”,而 0x0005 是当前格式版本号。)

我建议你使用 Scanner 或 CSV 读取库来读取这个文件。

英文:

You are trying to use ObjectInputStream to read a file that was not created using ObjectOutputStream. That approach won't work.

Clue: the number 22427275 is in hexadecimal. When you decode it by doing a hex to ASCII lookup, you get the characters &#39;&quot;&#39;, &#39;B&#39;, &#39;r&#39;, and &#39;u&#39;. Does that look familiar?

A valid Object serialization will start with aced0005 in hexadecimal. (The 0xaced is the object serialization format's "magic number" and 0x0005 is the current format version number.)

I suggest that you use Scanner or a CSV reader library to read the file.

huangapple
  • 本文由 发表于 2020年10月9日 13:57:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64274653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定