无法从文件中获取数据

Cannot get data from file

使用JFileChooser保存文件,一切正常:

private void saveFileDialog(){

    fileChooser = new JFileChooser(System.getProperty("employees.dat"));
    disableTextField(fileChooser.getComponents());

    fileChooser.setSelectedFile(new File("employees.dat"));
    fileChooser.setDialogTitle("File to save");

    int userSelection = fileChooser.showSaveDialog(this);
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        File fileToSave = fileChooser.getSelectedFile();
        save();
        System.out.println("Save as file: " + fileToSave.getAbsolutePath());
    }
}

调用方式save():

public void save(){
    RandomAccessFile file = null;
    try {
        file = new  RandomAccessFile("employees.dat", "rw");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     try{
         for(Employee em: list){
      if(list != null){

        file.writeUTF(em.getPps());
        file.writeUTF(em.getName());
        file.writeUTF(em.getSurname());
        file.writeUTF(em.getGender());
        file.writeUTF(em.getDep());
        file.writeInt(em.getSalary());
        file.writeUTF(em.getFullTime());
             }  
         }  

       file.close();

     }catch(IOException e1){
         System.out.println("Cant save");
     }  
}

在我的电脑上打开文件,所有数据都被记录和保存。

问题是:我无法从文件中获取数据

这是我的 getFromFile 方法:

   public void getFromFile(){
    RandomAccessFile file = null;
    try {
        file = new  RandomAccessFile("employees.dat", "rw");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try{
    while ( file.getFilePointer() < file.length() ){ 

          Employee em = new Employee();

                System.out.println("Saving");
                em.setPps(file.readUTF());
                em.setName(file.readUTF());
                em.setSurname(file.readUTF());
                em.setGender(file.readBoolean());
                em.setDep(file.readUTF());
                em.setSalary(file.readInt());
                em.setFulltime(file.readBoolean());  
                list.add( em );

    }
         file.close();
         }catch(IOException e1){
             System.out.println("Cant save");
             e1.printStackTrace();
         }  
}

我从文件中获取的 JFileChooser:

    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        fileChooser = new JFileChooser(".");

        fileChooser.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            System.out.println("Action");

          }
        });

        int status = fileChooser.showOpenDialog(null);

        if (status == JFileChooser.APPROVE_OPTION) {

          File selectedFile = fileChooser.getSelectedFile();
          getFromFile();

          System.out.println(selectedFile.getParent());
          System.out.println(selectedFile.getName());
        } else if (status == JFileChooser.CANCEL_OPTION) {
          System.out.println("calceled");

        }

      }
});

控制台显示 System.out.println("Saving");,然后是 System.out.println("Cant save"); .在我看来,一切都应该有效,但是文件是空的,我的程序中没有出现任何新内容。

这是以下异常:

 java.io.EOFException
        at java.io.RandomAccessFile.readFully(RandomAccessFile.java:399)
        at java.io.DataInputStream.readUTF(DataInputStream.java:592)
        at java.io.RandomAccessFile.readUTF(RandomAccessFile.java:921)

您的 "core" 问题可能出在这里...

System.out.println("Saving");
em.setPps(file.readUTF());
em.setPps(file.readUTF());
em.setName(file.readUTF());
em.setSurname(file.readUTF());
em.setGender(file.readBoolean());
em.setDep(file.readUTF());
em.setSalary(file.readInt());
em.setFulltime(file.readBoolean()); 

您正在呼叫 setPps 两次。

此外,您 select 使用 JFileChooser 的内容与文件的写入位置 to/read 来自

之间没有校对

如果我们快速查看 read/write 操作,我们可以看到双读操作

+----------------------------------+-------------------------------------+
| Write                            | Read                                |
+----------------------------------+-------------------------------------+
| file.writeUTF(em.getPps());      | em.setPps(file.readUTF());          |
+----------------------------------+-------------------------------------+
| file.writeUTF(em.getName());     | em.setPps(file.readUTF());          |
+----------------------------------+-------------------------------------+
| file.writeUTF(em.getSurname());  | em.setName(file.readUTF());         |
+----------------------------------+-------------------------------------+
| file.writeUTF(em.getGender());   | em.setSurname(file.readUTF());      |
+----------------------------------+-------------------------------------+
| file.writeUTF(em.getDep());      | em.setGender(file.readBoolean());   |
+----------------------------------+-------------------------------------+
| file.writeInt(em.getSalary());   | em.setDep(file.readUTF());          |
+----------------------------------+-------------------------------------+
| file.writeUTF(em.getFullTime()); | em.setSalary(file.readInt());       |
+----------------------------------+-------------------------------------+
|                                  | em.setFulltime(file.readBoolean()); |
+----------------------------------+-------------------------------------+

你写的和读的也有差距

例如,您使用 file.writeUTF(em.getGender()); 编写性别,但使用 em.setGender(file.readBoolean());

读取它

您可能应该使用 file.writeBoolean(em.getGender());

If it saving to the file properly via my JFileChooser, does it mean that there is collation between them?

我们来看看你的saveFileDialog方法

private void saveFileDialog(){

    fileChooser = new JFileChooser(System.getProperty("employees.dat"));
    disableTextField(fileChooser.getComponents());

    fileChooser.setSelectedFile(new File("employees.dat"));
    fileChooser.setDialogTitle("File to save");

    int userSelection = fileChooser.showSaveDialog(this);
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        File fileToSave = fileChooser.getSelectedFile();
        save();
        System.out.println("Save as file: " + fileToSave.getAbsolutePath());
    }
}

您创建了一个名为 fileToSave 的局部变量,它包含(我假设您希望文件保存在的目录),但它只有此方法的上下文,其他人无法使用

所以,您的方法应该是...

private void saveFileDialog(){
    save();
}

基本是一样的结果

我也会考虑使用 JAXB 这样的东西,这可能会让你的生活更轻松