gpt4 book ai didi

java - 文件不存在?线程 "main"java.io.FileNotFoundException : - Java 中出现异常

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:19 24 4
gpt4 key购买 nike

我正在开发一个类(class)项目,我正在尝试创建一个读取文件并填充数组的方法。在该方法内部,我还使用了从我创建的类中调用的另一个方法。我提示用户输入要读取的文件,无论我做什么,我都会收到文件不存在的错误。

这是我正在使用的开始代码:

 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;
}

这是我使用引号时得到的跟踪:

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

这是不带引号的跟踪:

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)

这是正常工作的代码:

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;
}

当我尝试读取字符串时,我只需要在每个 fileInput.next 的末尾添加 Line 即可。

最佳答案

堆栈跟踪包含

"The filename, directory name, or volume label syntax is incorrect"

表明名称中存在特殊字符。看起来您在名称两边加上了 "引号。

Enter the file to read from:"studentdata.txt"

如果文件名是源代码中的 Java 字符串,则需要使用引号,但通过控制台输入文本时,不需要使用引号。

关于java - 文件不存在?线程 "main"java.io.FileNotFoundException : - Java 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22340683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com