英文:
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.<init>(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("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);
}
}
}
}
}
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 )
"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”
MY TXT FILE IS LOCATED HERE WITH THIS INFO
WITH THIS INFO:
"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”
答案1
得分: 2
你正在尝试使用 ObjectInputStream
来读取一个不是使用 ObjectOutputStream
创建的文件。这种方法行不通。
提示:数字 22427275
是十六进制表示法。当你通过十六进制转ASCII查找来解码它时,你会得到字符 '"'
、'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 '"'
, 'B'
, 'r'
, and 'u'
. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论