有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java FileWriter和FileReader中,是否可以使用对象?

请考虑这些代码:有可能使用文件编写器和FieleRead与类学生对象吗?

Student s1 = new Student();
s1.input();//I want to write this method in a File
FileWriter out2 = new FileWriter("input.txt");
out2.write();
out2.flush();
out2.close();

s1.display();//I want to Read this method in a File
FileReader in2 = new FileReader("input.txt");
in2.read(); 
in2.close(); 

共 (1) 个答案

  1. # 1 楼答案

    是的,您可以使用FileOutputStream保存您喜欢的任何对象,并使用FileInputStream获取它(作为对象,您需要在此之后进行强制转换)。试试看。 例如:

        FileOutputStream fos = null;
        ObjectOutputStream writer = null;
        Student data = new Student();
        try{            
            fos = new FileOutputStream(fileName);
            writer = new ObjectOutputStream(fos);
    
            writer.writeObject(data);
        }
        catch (IOException ex){
            ex.printStackTrace();
        }
    

    请记住关闭它并执行异常练习:-)

    因为我发现了一个旧的完整的工作示例类,所以您可能需要对其进行研究(这是非常难看的代码,但它确实起到了作用):

    import java.io.*;
    
    public class Test {
    
        static boolean checkFile(File file) {
            if (file != null) {
                if(file.isFile()){
                    return true;
                }
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    System.err.println("Error creating " + file.toString());
                }
                if (file.isFile() && file.canWrite() && file.canRead())
                    return true;
            }
            return false;
        }
    
        static boolean writeFile(String dat, Object data){
            FileOutputStream fos = null;
            ObjectOutputStream writer = null;
    
            try{            
                fos = new FileOutputStream(dat);
                writer = new ObjectOutputStream(fos);
    
                writer.writeObject(data);
            }
            catch (IOException ex){
                ex.printStackTrace();
                return false;
            }
            finally{
                try{
                   if(writer!=null){
                       writer.close();
                   }
                }
                catch (IOException ex){
                    ex.printStackTrace();
                }
    
                try{
                    if(fos!=null){
                        fos.close();
                    }
                }
                catch (IOException ex){
                    ex.printStackTrace();
                }
            }   
            return true;
        }
    
        static Student readFile(String dat){
            FileInputStream fis = null;
            ObjectInputStream reader = null;
            Student student = null;
    
            try{            
                fis = new FileInputStream(dat);
                reader = new ObjectInputStream(fis);
    
                student = (Student) reader.readObject();
            }
            catch (Exception ex){
                ex.printStackTrace();
            }
            finally{
                try{
                   if(reader!=null){
                       reader.close();
                   }
                }
                catch (IOException ex){
                    ex.printStackTrace();
                }
    
                try{
                    if(fis!=null){
                        fis.close();
                    }
                }
                catch (IOException ex){
                    ex.printStackTrace();
                }
            }   
            return student;
        }
    
        public static void main(String[] args){
            Student student = new Student("Horst");
            String filename = "input.txt";
            boolean worked = false;
            if(checkFile(new File(filename)))
                worked=writeFile(filename, student);
            student = new Student("not Horst");
            if(worked)
                student= (Student) readFile(filename);
            System.out.println("Student : " + student.name);
        }
    
    }
    
    class Student implements Serializable{
    
        public String name;
    
        public Student(String name){
            this.name=name;
        }
    }