Home > OS >  Ask a user to input fileName that I later read
Ask a user to input fileName that I later read

Time:12-18

My task that I am working on is to ask a User to input a fileName they wish to encrypt. Right now I am focusing on getting it to read out, but I receive the fileNotFound exception. The file is located in the same folder as the app and is spelled correctly with a .txt. This is my code that I have so can anyone pick up the mistake?

case '1': {
    System.out.println("Enter The name of the File you wish to Encrypt: ");
    String inputFile = scanner.next();
    
    try {
        File file = new File(inputFile);
        Scanner myReader = new Scanner(file);
        while (myReader.hasNextLine()) {
            String msg = myReader.nextLine();
            System.out.println(msg);
        }
        myReader.close();
    } catch (FileNotFoundException e) {
        System.out.println("An error occurred.");
        e.printStackTrace();
        break;
    }
}

CodePudding user response:

Either supply the full path to the file you want loaded at the prompt, or if you're running it from IntelliJ you can put the file in the root of your project directory and just enter the filename at the prompt and it will be loaded.

CodePudding user response:

There are a couple of issues. It would be go to brief yourself on this article Path vs File in Java

You might have the path incorrect and that is cause for concern. Navigate to the absolute directory of both the file and the Java program would be a start

if not that keep in mind that Java is a VM language and the file might not be on the VM

  • Related