0

When doing this, I am trying to make it "dummy proof" for my coworkers, so if they put in a file path instead of a file - the program doesn't stop so that they can just keep going in the application - it'll just ask them again. However currently

FileNotFoundException: C:\Users\usersname\Desktop\userImports\current (Access is denied)
java.io.FileNotFoundException: C:\Users\usersname\Desktop\userImports\current (Access is denied)

How do I work for this? I don't want it to crash like this, I'd rather it just say, "thats not a file - please try again"

How do I better handle file not found exceptions?

 File f;
            do {
                System.out.print("Please give me the " + type + "file: " );
                String file = console.nextLine();
                f = new File(file);
            } while (!f.exists());
            Scanner readMe = null;
            try {
                readMe = new Scanner(f);
            } catch (FileNotFoundException e) {
                System.err.println("FileNotFoundException: " + e.getMessage());
                e.printStackTrace();
            }
            return readMe;
        }
5
  • 1
    You're printing the stack trace inside your catch block. Instead of that, you could print whatever message you want. Commented Aug 17, 2017 at 14:43
  • You could work with the File class. it has several options you might be looking for, like File#exists, File#isFile or File#canWrite Commented Aug 17, 2017 at 14:44
  • "Access is denied" may comes if we are giving any file which is not allowed to be accessed from outside program. Make sure ani-one can access that file Commented Aug 17, 2017 at 14:47
  • use the isFile() and isDirectory() methods to distinguish between files and folders. are you sure you are giving exact file path/name in input Commented Aug 17, 2017 at 14:49
  • Class File has isDirectory() and exists() methods Commented Aug 17, 2017 at 15:05

3 Answers 3

2

I'm not sure I understood what you exactly want but this is my answer to what I understood : Just loop while u don't find the file and you can also add a counter like after 5 times u exit the program.

File f;
boolean found = false ; 

  while (!found) {

        do {
            System.out.print("Please give me the " + type + "file: " );
            String file = console.nextLine();
            f = new File(file);
        } while (!f.exists());

        Scanner readMe = null;

        try {

            readMe = new Scanner(f);
            found = true ; 

        } catch (FileNotFoundException e) {
            found = false ; 
            System.err.println("FileNotFoundException: " + e.getMessage());

            system.out.printl  ( "A problem occured while loading the file please try again ");
            //e.printStackTrace();
        }
  }

  return readMe;

}
Sign up to request clarification or add additional context in comments.

Comments

0

"Access is denied" means that the file does exist but the user who ran the program is not allowed to access - in your case read - it. It could be, i.a., that the user does not have the necessary authority, or that the file is being held by another program.

Comments

0

Try to use canRead() instead of exists()

do {
  ...
} while (!f.canRead());

Comments

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.