[英]Why am I getting an error about a Windows file path in Java?
I am getting a " java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at parker.MovieLibrary.<init>(MovieLibrary.java:22) at parker.SelectorUserInput.main(SelectorUserInput.java:10)
" error when trying to open a file. 我收到了一个
java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at parker.MovieLibrary.<init>(MovieLibrary.java:22) at parker.SelectorUserInput.main(SelectorUserInput.java:10)
“尝试打开文件时出错。
Below is the code of the MovieLibrary constructor that is giving me trouble: 以下是给我带来麻烦的MovieLibrary构造函数的代码:
public MovieLibrary() {
String FILENAME = "\\Users\\FirstName LastName\\Desktop\\JavaIndividualAssignment\\FinalMovieList1.txt";
Scanner input = new Scanner(FILENAME);
File file = new File(input.nextLine());
String[] split;
try {
File file1 = new File(input.nextLine());
input = new Scanner(file1);
while (input.hasNextLine()) {
String line = input.nextLine();
//code to add movies to an ArrayList
}
//input.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally{
if (input != null){
input.close();
}
}
}
}
I tired all of the suggestions listed here: Java File Path Windows/Linux , but none of them worked.I got the same error each time. 我厌倦了这里列出的所有建议: Java File Path Windows / Linux ,但是它们都不起作用,每次都遇到相同的错误。 I replaced the backslashes with single forward slashes, tried using the Path object, nothing changed the error.
我用单个正斜杠替换了反斜杠,并尝试使用Path对象,但没有改变错误。
Is this an issue with my file path? 我的文件路径有问题吗? I used the same file-opening code on a different computer and it found the file just fine.
我在另一台计算机上使用了相同的文件打开代码,它发现文件很好。
Below is the 下面是
You are constructing a Scanner object of the filename string (\\Users etc). 您正在构造文件名字符串(\\ Users等)的Scanner对象。 Pretty sure you want to create a File object of the string and a Scanner object of that File object.
相当确定您要创建字符串的File对象和该File对象的Scanner对象。
String FILENAME = "C:\\Users\\FirstName LastName\\Desktop\\JavaIndividualAssignment\\FinalMovieList1.txt";
Scanner input=null;
File file = new File(FILENAME);
String[] split;
try {
input = new Scanner(file);
while (input.hasNextLine()) {
String line = input.nextLine();
//code to add movies to an ArrayList
}
//input.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally{
if (input != null){
input.close();
}
}
Try This
尝试这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.