简体   繁体   English

Java Scanner(System.in)在用户输入后不打开文件

[英]Java Scanner(System.in) does not open file after user input

Hello there I am facing an issue which I cannot find a solution.您好,我遇到了一个找不到解决方案的问题。 I am asking the user to input the name of a file but the output i get is always ''unable to open file''.我要求用户输入文件名,但我得到的输出总是“无法打开文件”。 Any advice would be much appreciated.任何建议将不胜感激。

    Scanner reader = new Scanner(System.in); 
    System.out.println("Enter the name of textfile to be read ( add .txt): ");

    String fileName = reader.next();
    String line = null;
    BufferedReader bufferedReader =   new BufferedReader(fileReader);

    while((line = bufferedReader.readLine()) != null) {
        System.out.println(line); 

        catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
       }

        catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                  
        // Or we could just do this: 
        // ex.printStackTrace();
    }
    }

The FileNotFoundException is always executed but why? FileNotFoundException 总是被执行,但为什么?

PS if I change the path to a specific location such as "C:\\etc" it reads the file. PS,如果我将路径更改为特定位置,例如“C:\\etc”,它会读取文件。

If you only give the file name and not the path, Java doesn't know where to look.如果您只提供文件名而不提供路径,Java 不知道在哪里查找。 If you are certain that the file will be in the project directory, just prepend C:/etc to the user input.如果您确定该文件将位于项目目录中,只需在用户输入前添加 C:/etc。

If you don't specify the absolute file path, ie, C:/dir/... , java will look in the same directory as the project root (the same directory as your src and bin folders).如果不指定绝对文件路径,即C:/dir/... ,java 将在与项目根目录相同的目录(与srcbin文件夹相同的目录)中查找。 If the file is there, it will find it with just the file name, or if you make a directory in that folder, you would need that directory in the path.如果该文件在那里,它将仅通过文件名找到它,或者如果您在该文件夹中创建一个目录,则路径中将需要该目录。 The same is true if you have an executable JAR, it will look in the same directory that the JAR is located.如果您有一个可执行 JAR,情况也是如此,它将在 JAR 所在的同一目录中查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scanner input = new Scanner(System.in) 到底是什么意思? - What does Scanner input = new Scanner(System.in) actually mean? 如何使用 java.util.Scanner 从 System.in 正确读取用户输入并对其采取行动? - How to use java.util.Scanner to correctly read user input from System.in and act on it? Java不会使用Scanner和System.in分解具有多个double和char变量的用户输入 - java won't break up a user input with multiple double and char variables using Scanner and System.in Java Scanner分隔符和System.in - Java Scanner delimiter and System.in Java 扫描器类 (System.in) - Java Scanner Class ( System.in) JAR文件未在crontab执行时从Scanner(System.in)读取输入 - JAR file not reading input from Scanner(System.in) on crontab execution 我如何限制 Java 中的“新扫描仪(System.in)”系统,不进一步处理用户输入数据,除非它是正确的输入值? - How can I restrict the "new Scanner(System.in)" system in Java, to not go further with the user input data, unless it's the correct inputted value? 使用Scanner和System.in(Java)进行方法的Junit测试 - Junit test of method with Scanner and System.in (Java) 使用Java中的System.in在Scanner上进行缓冲 - Buffering with Scanner upon System.in in Java Java - Scanner(System.in)和“阻塞线程” - Java - Scanner(System.in) and “Blocking a thread”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM