读取和写入文件时出错(Java)[关闭]

Posted

技术标签:

【中文标题】读取和写入文件时出错(Java)[关闭]【英文标题】:Error Reading and writing files (Java) [closed] 【发布时间】:2012-12-20 19:55:18 【问题描述】:

大家好,我刚刚在我的程序中实现了目标文件,并且我不断收到错误(读​​取文件错误和写入文件问题)这是我的 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");
        
    



【问题讨论】:

我没有看到 try catch 块 ...或文件读/写 您的代码似乎与您的问题无关。你得到什么例外?你在哪里读/写文件? @MatthewCassar 将来尝试发布一个更小的示例来重现该问题。在一个问题中倾倒大量代码往往会让人们失望,而且你得到的帮助也更少。基本上越少越好,当被问到或认为有必要时添加更多。 你应该在你的 catch 块中添加e.printStackTrace(),这样你就可以看到堆栈跟踪是什么。基本上你正在捕捉错误,并说“嘿,它坏了”,但没有给自己足够的信息来知道它为什么坏了。 【参考方案1】:

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

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

根据您更新后的问题中的代码,您似乎对哪些类需要实现 Serializable 感到困惑。需要实现的类是您实际写入文件的类(即SubjectTeacher 等)。

检查这两个,然后告诉我你发现了什么。

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

【讨论】:

谢谢你,再次对误会深表歉意,很遗憾没有奏效 测试它是否有效 :D 但它仍然显示有关读取文件的错误(尽管它实际上是红色的) @MatthewCassar 异常说明了什么?查看它的属性以获取描述(我忘记了该属性在 java 中实际调用的内容——我一直在编写 c# 代码)

以上是关于读取和写入文件时出错(Java)[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

“无法识别的类型'员工'。忽略。C:/ .....”从.xml读取输入并将输出写入.xls文件+ perl时出错

写入控制文件时出错 ORA-00221 oracle

你能推荐一个 Java 库来读取(并且可能写入)CSV 文件吗? [关闭]

在写入文本文件时添加分隔符

Python文件操作:文件的打开关闭读取写入

文件的写入,读取和关闭