Search code examples
javaiofile-handlingrandomaccessfileeofexception

Cannot get data from file


Using JFileChooser to save file and everything works perfect:

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

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

Opening file on my computer and all data is recorded and saved.

Problem is: I cannot get data from file

Here is my getFromFile method:

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

My JFileChooser for getting from file:

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

        }

      }
});

Console shows me System.out.println("Saving");, and then System.out.println("Cant save"); . Seems to me that everything should work, however file is empty and nothing new appear in my program.

Here is following exception coming:

 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)

Solution

  • Your "core" problem is likely here...

    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()); 
    

    You're calling setPps twice.

    Also, there is no collation between what you select using the JFileChooser and where the file is getting written to/read from

    If we take a quick look at the read/write operations we can see the doubl read operation

    +----------------------------------+-------------------------------------+
    | 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()); |
    +----------------------------------+-------------------------------------+
    

    There's also a disparity between what you're writing and what you're reading

    For example, you write the gender using file.writeUTF(em.getGender());, but you read it using em.setGender(file.readBoolean());

    You should probably be using file.writeBoolean(em.getGender());

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

    Let's take a look at your saveFileDialog method

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

    You create a local variable called fileToSave which holds (I presume the directory you want the file saved in), but it only has context to this method, no one else can use

    So, your method should read...

    private void saveFileDialog(){
        save();
    }
    

    and it would basically achieve the same result

    I'd also consider using something like JAXB which would probably make your life a lot easier