读取和写入文件时出错(Java)
嗨,伙计们,我刚刚在我的程序中实现了目标文件,我经常会遇到读取文件时出错和写入文件时出错这是我的try-catch块中的两个错误,当我尝试读取文件时,它不会加载,保存也不会起作用读取和写入文件时出错(Java),java,file,object,load,save,Java,File,Object,Load,Save,嗨,伙计们,我刚刚在我的程序中实现了目标文件,我经常会遇到读取文件时出错和写入文件时出错这是我的try-catch块中的两个错误,当我尝试读取文件时,它不会加载,保存也不会起作用 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; public class Stores implements Serializable { publ
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Stores implements Serializable
{
public static ArrayList<Student> stud1 = new ArrayList<Student>();
public static ArrayList<SubjectTeacher> sTeach1 = new ArrayList<SubjectTeacher>();
private static int iT = 0;
private static int iS = 0;
public static void savet (ArrayList<SubjectTeacher> teachIn, int count)
{
iT = count;
sTeach1 = teachIn;
saveTeachToFile();
}
public static void saves (ArrayList<Student> studIn, int count)
{
iS = count;
stud1 = studIn;
saveStudToFile();
}
public static ArrayList<Student> getStud ()
{
return stud1;
}
public static ArrayList<SubjectTeacher> getTeach ()
{
return sTeach1;
}
public static int getStudSize()
{
return stud1.size();
}
public static int getTeachSize()
{
return sTeach1.size();
}
private static void saveStudToFile()
{
try
{
// create a FileOutputStream object which will handles the writing of the sudent list of objects to the file.
FileOutputStream studentFile = new FileOutputStream("Students.obf");
// the OutputObjectStream object will allow us to write whole objects to and from files
ObjectOutputStream studentStream = new ObjectOutputStream(studentFile);
for(Student item: stud1) // enhanced for loop
// Loop through the list of studentsListIn and for each of these objects, wite them to the file
{
studentStream.writeObject(item);
}
//close the file so that it is no longer accessible to the program
studentStream.close();
}
catch(IOException e)
{
System.out.println("There was a problem writing the File");
}
}
private static void saveTeachToFile()
{
try
{
FileOutputStream teacherFile = new FileOutputStream("Teacher.obf");
ObjectOutputStream teacherStream = new ObjectOutputStream(teacherFile);
for(SubjectTeacher item1: sTeach1) // enhanced for loop
{
teacherStream.writeObject(item1);
}
//close the file so that it is no longer accessible to the program
teacherStream.close();
}
catch(IOException e)
{
System.out.println("There was a problem writing the File");
}
}
public static void loadStudentList()
{
boolean endOfFile = false;
Student tempStudent;
try
{
// create a FileInputStream object, studentFile
FileInputStream studentFile = new FileInputStream("Students.obf");
// create am ObjectImnputStream object to wrap around studentStream
ObjectInputStream studentStream = new ObjectInputStream(studentFile) ;
// read the first (whole) object with the readObject method
tempStudent = (Student) studentStream.readObject();
while (endOfFile != true)
{
try
{
stud1.add(tempStudent);
// read the next (whole) object
tempStudent = (Student) studentStream.readObject();
}
//use the fact that the readObject throws an EOFException to check whether the end of eth file has been reached
catch(EOFException e)
{
endOfFile = true;
}
studentStream.close();
}
}
catch(FileNotFoundException e)
{
System.out.println("File not found");
}
catch(ClassNotFoundException e) // thrown by readObject
/* which indicates that the object just read does not correspond to any class
known to the program */
{
System.out.println("Trying to read an object of an unkonown class");
}
catch(StreamCorruptedException e) //thrown by constructor
// which indicates that the input stream given to it was not produced by an ObjectOutputStream object {
{
System.out.println("Unreadable File Format");
}
catch(IOException e)
{
System.out.println("There was a problem reading the file");
}
}
public static void loadTeacherList()
{
boolean endOfFile = false;
SubjectTeacher tempTeacher;
try
{
FileInputStream teacherFile = new FileInputStream("Teacher.obf");
ObjectInputStream teacherStream = new ObjectInputStream(teacherFile) ;
tempTeacher = (SubjectTeacher) teacherStream.readObject();
while (endOfFile != true)
{
try
{
sTeach1.add(tempTeacher);
// read the next (whole) object
tempTeacher = (SubjectTeacher) teacherStream.readObject();
}
//use the fact that the readObject throws an EOFException to check whether the end of eth file has been reached
catch(EOFException e)
{
endOfFile = true;
}
teacherStream.close();
}
}
catch(FileNotFoundException e)
{
System.out.println("File not found");
}
catch(ClassNotFoundException e) // thrown by readObject
/* which indicates that the object just read does not correspond to any class
known to the program */
{
System.out.println("Trying to read an object of an unkonown class");
}
catch(StreamCorruptedException e) //thrown by constructor
// which indicates that the input stream given to it was not produced by an ObjectOutputStream object {
{
System.out.println("Unreadable File Format");
}
catch(IOException e)
{
System.out.println("There was a problem reading the file");
}
}
}
首先,你应该用正确的代码编辑问题,这样它就不会被关闭。其次,可能会发生一些事情
正在写入文件的类不可序列化
这些文件是只读或写保护的
根据您更新的问题中的代码,您可能会混淆哪些类需要实现可序列化。需要实现的类是您实际编写到文件ie SubjectTeacher等的类
检查一下那两个,告诉我你发现了什么
此外,我还建议逐步编写代码,并查看运行时的异常情况。你会对发生的事情有一个更好的了解。我看不到一个试一试的拦网。。。或者文件读/写您的代码似乎与您的问题无关。你有什么例外?你在哪里读/写文件?@matthewcasar在未来尝试发布一个小的例子来重现这个问题。在一个问题中转储大量代码往往会让人反感,而你得到的帮助也会减少。基本上,越少越好,在被要求或认为必要时添加越多。您应该在catch块中添加e.printStackTrace,以便查看stacktrace是什么。基本上,你发现了错误,说“嘿,它坏了”,但没有给自己足够的信息知道它为什么坏了。谢谢你,再次为误解道歉,不幸的是,这并没有起作用,测试结果是:D,但它仍然显示了读取文件的错误,尽管它实际上正在运行red@MatthewCassar例外说明了什么?查看它的属性以获取描述,我忘记了该属性在java中的实际调用-我已经编写c代码一段时间了