简体   繁体   English

为什么在Java中出现有关Windows文件路径的错误?

[英]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.

相关问题 为什么我在使用OSX而不是Windows的Java中遇到找不到文件的错误? - Why am I getting a file not found error in java using OSX, but not Windows? 为什么我会收到有关 Java 中实用程序类的警告 - Why am I getting this warning about utility classes in Java 为什么我会收到有关Java中“可能丢失精度”的警告? - Why am I getting a warning about “Possible loss of precision” in Java? 为什么我在Java中收到Undefinedlabel错误? - why i am getting Undefinedlabel error in java? 为什么我收到关于compareTo方法的此错误? - Why am I getting this error about the compareTo method? 我为什么会收到“ UnsatisfiedLinkError:java.library.path中没有SolarisSerialParallel” - Why am I getting “UnsatisfiedLinkError: no SolarisSerialParallel in java.library.path” 我在使用Java中的Selenium chrome驱动程序更改文件路径时遇到错误? - I am getting error for changing file path using selenium chrome driver in java? 为什么在远程系统上运行批处理文件时出现“路径不正确”错误? - Why am I getting a “Path not correct” error while running a batch file on a remote system? 为什么在 Windows 7 上尝试安装 Java SDK 时会收到错误消息? - Why am I getting an error message when trying to install the Java SDK on Windows 7? 使用Scanner读取文件:为什么在Java中使用Scanner读取文件时出现错误? - read a file using Scanner: Why am I getting an error when using Scanner for read files in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM