文件不存在?线程“main”中的异常 java.io.FileNotFoundException: - Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22340683/
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
File does not exist? Exception in thread "main" java.io.FileNotFoundException: - Java
提问by COLEplusTEN
I'm working on a project for class and I'm attempting to create a method that will read a file and fill an array. Inside the method I'm also using another method called from a class I've created. I prompt the user to enter the file to be read and no matter what I seem to do I keep getting an error that the file does not exist.
我正在研究一个类项目,我正在尝试创建一个方法来读取文件并填充数组。在该方法中,我还使用了从我创建的类中调用的另一个方法。我提示用户输入要读取的文件,无论我似乎做什么,我都会收到文件不存在的错误消息。
Here is the beginning code I'm working with:
这是我正在使用的开始代码:
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("Enter the file to read from:");
String readFile = input.nextLine();
System.out.print("Enter the number of accounts in the file: ");
int size = input.nextInt();
Account[] Accounts = readFile(readFile, size);
Account[] invalidAccounts = new Account[34];
for (int i = 0; i < Accounts.length; i++) {
String password = Accounts[i].getPassword();
if (isValidPassword(password) == false) {
invalidAccounts[i] = Accounts[i];
}
}
createDistributionList(invalidAccounts);
}
public static Account[] readFile(String readFile, int size) throws Exception {
Account[] Accounts = new Account[size];
File myFile = new File(readFile);
Scanner fileInput = new Scanner(myFile);
for (int i = 0; i < Accounts.length; i++) {
int stuID = fileInput.nextInt();
String name = fileInput.next();
String username = fileInput.next();
String password = fileInput.next();
Accounts[i] = new Account(stuID, name, username, password);
}
return Accounts;
}
Here's the trace I'm getting if I use quotes:
如果我使用引号,这是我得到的跟踪:
Enter the file to read from:"studentdata.txt"
Enter the number of accounts in the file: 34
Exception in thread "main" java.io.FileNotFoundException: "studentdata.txt" (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.util.Scanner.<init>(Scanner.java:656)
at accountdriver.AccountDriver.readFile(AccountDriver.java:38)
at accountdriver.AccountDriver.main(AccountDriver.java:25)
Java Result: 1
And here's the trace without quotes:
这是没有引号的跟踪:
Enter the file to read from:studentdata.txt
Enter the number of accounts in the file: 34
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at accountdriver.AccountDriver.readFile(AccountDriver.java:40)
at accountdriver.AccountDriver.main(AccountDriver.java:25)
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)
And here is the properly working code:
这是正常工作的代码:
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("Enter the file to read from:");
String readFile = input.nextLine();
System.out.print("Enter the number of accounts in the file: ");
int size = input.nextInt();
Account[] Accounts = readFile(readFile, size);
Account[] invalidAccounts = new Account[34];
for (int i = 0; i < Accounts.length; i++) {
String password = Accounts[i].getPassword();
if (isValidPassword(password) == false) {
invalidAccounts[i] = Accounts[i];
}
}
createDistributionList(invalidAccounts);
}
public static Account[] readFile(String readFile, int size) throws Exception {
Account[] Accounts = new Account[size];
File myFile = new File(readFile);
Scanner fileInput = new Scanner(myFile);
for (int i = 0; i < Accounts.length; i++) {
int stuID = fileInput.nextInt();
String name = fileInput.nextLine();
String username = fileInput.nextLine();
String password = fileInput.nextLine();
Accounts[i] = new Account(stuID, name, username, password);
}
return Accounts;
}
I just needed to add Line to the end of each fileInput.next when I was attempting to read in strings.
当我尝试读取字符串时,我只需要将 Line 添加到每个 fileInput.next 的末尾。
采纳答案by slipperyseal
The stacktrace contains
堆栈跟踪包含
"The filename, directory name, or volume label syntax is incorrect"
“文件名、目录名或卷标语法不正确”
suggesting there are special characters in the name. It looks like you are putting " quotation marks around the name.
建议名称中有特殊字符。看起来您在名称周围加上了 " 引号。
Enter the file to read from:"studentdata.txt"
You would use quotation marks if the filename was a Java string in the source code, but when inputting text via the console, these are not required.
如果文件名是源代码中的 Java 字符串,您将使用引号,但当通过控制台输入文本时,这些不是必需的。
回答by Paul Samsotha
FileNotFoundException
will either mean your file (name) doesn't exist, or the file is in the wrong location.
FileNotFoundException
要么意味着您的文件(名称)不存在,要么该文件位于错误的位置。
If you're going to read the file path as "studentdata.txt"
, what you need to understand is that, if working from an IDE, normally it tries to look for the file in the current working directory, which is the project root. So your file would need to be there.
如果您要将文件路径读取为"studentdata.txt"
,您需要了解的是,如果从 IDE 工作,通常它会尝试在当前工作目录(即项目根目录)中查找文件。所以你的文件需要在那里。
You may instead just want to use the absolute path instead like C:/path/to/file.txt
您可能只想使用绝对路径,而不是像 C:/path/to/file.txt
In cases where the file will be an embedded resources, you may want to use MyClass.class.getResourceAsStream(fileName)
where the file would be in the class path. So you could do
在文件将是嵌入资源的情况下,您可能希望使用MyClass.class.getResourceAsStream(fileName)
文件在类路径中的位置。所以你可以这样做
InputStream is = MyClass.class.getResourceAsStream(fileName);
Scanner scanner = new Scanner(is);
Where your file would be in the same package as your class, if you're just going to use the file name as the path.
如果您只想使用文件名作为路径,那么您的文件将与您的类位于同一个包中。
So you have some things to consider.
所以你需要考虑一些事情。
回答by Kipruto Barno
You need to copy and paste the text file "studentdata.txt" into the project file(navigate to the specific project name)inside the location of NetbeansProjects. t should work just fine!
您需要将文本文件“studentdata.txt”复制并粘贴到 NetbeansProjects 位置内的项目文件中(导航到特定的项目名称)。应该可以正常工作!