0

I try to open a .txt file (agenda.txt) from the src/resources folder, read objects from it and add them to an ArrayList, but I get this error: "The system can not find the specified path.". This is the code I use:

    public void open(File f) {
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    try {
        fis = new FileInputStream(f);
        ois = new ObjectInputStream(fis);
        Object o;
        try {
            while ((o = ois.readObject()) != null) {
                model.adauga((Abonat) o);
            }
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(
                    this,
                    ex.getMessage(),
                    "Clasa...!",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(
                this,
                ex.getMessage(),
                "Eroare deschidere fisier!",
                JOptionPane.ERROR_MESSAGE);
        return;
    } finally {
        try {
            ois.close();
        } catch (IOException ex) {
            Logger.getLogger(CarteDeTelefonGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}

And in the class constructor:

    private String path ="resources/agenda.txt";
    File f=new File(path);
    open(f);

What is wrong in the code?

1
  • try resources//agenda.txt Commented May 31, 2013 at 9:27

1 Answer 1

1

the file should be located outside src, something like baseproject/resources. Thats because your path is the project base not your sources directory. Or you can change the code to

private String path ="src/resources/agenda.txt";
Sign up to request clarification or add additional context in comments.

4 Comments

Another problem now...It works fine on my laptop, but if i run the .jar on another computer it doesn't find the agenda.txt file. How can I fix this?
you have the exactly same directory structure? what exception are you getting? and please add the exception to your question
Error: src\resources\agenda.txt (The system cannot find the path specified). In the jar is the resources directory that contains the agenda.txt file and the meta inf directory.
easy answer: obviously the file is not located there! go to the location of the jar, create a directory src, then directory resources, and paste there your file agenda.txt. extended answer: as i told you before the project directory is your project path! so, modify your code for searching the file in 'resources/agenda.txt' and move the file to that location

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.