如何使用扫描仪提示用户输入 java 中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18995177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prompt a user for a file in java using Scanner?
提问by user2209865
I need to know how to prompt user for a file and then be able to use the file in my program. Here is my unsuccessful attempt.
我需要知道如何提示用户输入文件,然后才能在我的程序中使用该文件。这是我失败的尝试。
public static void main(String[] args) {
Scanner Scanscan = new Scanner(System.in);
System.out.print("Input filename:");
String filename = Scanscan.nextLine();
File inputFile = new File(filename);
Scanner reader = new Scanner(inputFile);
I get a "File not found exception" when I try it.
我在尝试时收到“找不到文件异常”。
回答by Jakob Weisblat
You get a FileNotFoundException
because the filename the user entered doesn't exist in the system. Enter a filename that does exist, and it should work. Make sure the file you're getting is in the directory you're running the program from. Try entering the name of the java file you're working on amd see if it works.
你得到一个FileNotFoundException
因为用户输入的文件名在系统中不存在。输入一个确实存在的文件名,它应该可以工作。确保您获得的文件位于您运行程序的目录中。尝试输入您正在处理的 java 文件的名称,看看它是否有效。
回答by user2813412
File does not exist. You should,
文件不存在。你应该,
1.Check that your file is existed corresponding to your executing application.
1.检查您的文件是否存在与您正在执行的应用程序相对应。
2.Try to provide full hard path of file in your system like "C:\\sample.txt"
2.尝试在您的系统中提供文件的完整硬路径,例如 "C:\\sample.txt"