简体   繁体   English

尝试读取时找不到文件

[英]File not found when trying to read

Code: 码:

public class problem_7_4 {

    public static void main(String[] args) throws FileNotFoundException {
       Scanner console = new Scanner(System.in);
       System.out.print("Input file: ");
       String inputFileName = console.next();

       File inputFile = new File("\"" + inputFileName + "\"");
       Scanner in = new Scanner(inputFile);
    }
}

I am trying to prompt the user to enter a file to read. 我试图提示用户输入要读取的文件。 When the file is entered, it brings up an error saying the file is not found. 输入文件后,将显示一条错误消息,指出找不到文件。 I have the file saved on my program so it should be there. 我已将文件保存在程序中,因此该文件应该在那里。 Any ideas? 有任何想法吗?

You're code is wrong. 您的代码错误。 Why are you adding quotes to the file name? 为什么要在文件名中加上引号? Should be: 应该:

File inputFile = new File(inputFileName);

Add the following line right after creating inputFile and make sure it is the correct path. 在创建inputFile之后立即添加以下行,并确保它是正确的路径。 If not, modify it to match the location on the disc 如果不是,请对其进行修改以匹配光盘上的位置

System.out.println(inputFile.getAbsolutePath());

Try this; 尝试这个;

File inputFile = new File(inputFileName); 

inputFileName is String already so you don't need to put it in quotes inputFileName已经是String,因此您无需将其放在引号中

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM