-3

大家好,我刚刚在我的程序中实现了目标文件,并且我不断收到错误(读​​取文件错误和写入文件问题)这些是我的 try catch 块中的 2 个错误,当我尝试读取文件时它没有加载,保存也不行。

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");
        }
    }


}
4

1 回答 1

1

好吧,一方面,您应该使用正确的代码编辑问题,这样它就不会被关闭。其次,可能会发生一些事情。

  1. 您写入文件的类不可序列化
  2. 这些文件以某种方式被只读或写保护

根据您更新的问题中的代码,您可能会混淆哪些类需要实现Serializable。需要实现的类是您实际写入文件的类(即SubjectTeacher,等)。

检查这两个,让我知道你发现了什么。

另外,我建议单步执行代码并查看运行时异常的样子。你会更好地了解正在发生的事情。

于 2012-12-20T20:22:18.113 回答