找不到Java I/O文件

找不到Java I/O文件,java,file,io,filenotfoundexception,Java,File,Io,Filenotfoundexception,当前正在尝试编写一个程序,从文件中获取输入并将其存储在数组中。但是,每当我尝试运行该程序时,都找不到该文件(尽管file.exists()和file.canRead()返回true) 这是我的密码: public void getData (String fileName) throws FileNotFoundException { File file = new File (fileName); System.out.println(file.exists());

当前正在尝试编写一个程序,从文件中获取输入并将其存储在数组中。但是,每当我尝试运行该程序时,都找不到该文件(尽管file.exists()和file.canRead()返回true)

这是我的密码:

    public void getData (String fileName) throws FileNotFoundException
{
    File file = new File (fileName);
    System.out.println(file.exists());
    System.out.println(file.canRead());
    System.out.println(file.getPath());
    Scanner fileScanner = new Scanner (new FileReader (file));
    int entryCount = 0;     // Store number of entries in file

    // Count number of entries in file
    while (fileScanner.nextLine() != null)
    {
        entryCount++;
    }

    dirArray = new Entry[entryCount];   //Create array large enough for entries
    System.out.println(entryCount);

}

public static void main(String[] args)
{
    ArrayDirectory testDirectory = new ArrayDirectory();


    try
    {
        testDirectory.getData("c://example.txt");
    }

    catch (Exception ex)
    {
        ex.printStackTrace();
    }

}
(在当前状态下,该方法仅用于计算行数和创建数组)

控制台输出如下:true:/c:/example.txt

程序似乎在扫描程序实例化的行上抛出“FileNotFoundException”

在调试时检查'file'对象时,我注意到的一点是,尽管它的'path'变量的值为“c:\example.txt”,但它的'filePath'值为null。不确定这是否与问题相关

编辑:在Brendan Long的回答之后,我更新了“catch”块。堆栈跟踪如下所示:

java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at assignment2.ArrayDirectory.getData(ArrayDirectory.java:138)
    at assignment2.ArrayDirectory.main(ArrayDirectory.java:193)

扫描仪似乎无法识别文件,因此无法找到行

此代码可能无法满足您的要求:

try
{
    testDirectory.getData("c://example.txt");
}

catch (Exception ex)
{
    new FileNotFoundException("File not found");
}
如果捕获任何异常,则运行FileNotFoundException的构造函数,然后将其丢弃。尝试这样做:

try
{
    testDirectory.getData("c://example.txt");
}
catch (Exception ex)
{
    ex.printStackTrace();
}

此代码可能不符合您的要求:

try
{
    testDirectory.getData("c://example.txt");
}

catch (Exception ex)
{
    new FileNotFoundException("File not found");
}
如果捕获任何异常,则运行FileNotFoundException的构造函数,然后将其丢弃。尝试这样做:

try
{
    testDirectory.getData("c://example.txt");
}
catch (Exception ex)
{
    ex.printStackTrace();
}

根据,
nextLine()
在没有更多输入时抛出此异常。您的程序似乎期望它返回
null
,但现在它就是这样工作的(不像,比如说,
BufferedReader
在输入结束时返回
null
)。用于确保在使用
nextLine

之前有另一行,根据,
nextLine()
在没有更多输入时引发此异常。您的程序似乎期望它返回
null
,但现在它就是这样工作的(不像,比如说,
BufferedReader
在输入结束时返回
null
)。使用以确保在使用
nextLine

之前有另一行@BrendanLong的可能重复项我在发布此内容之前访问了该页面,但没有解决方案有效,因此这个问题:为什么路径名中有两个斜杠?@ajb这正是我被教导的方法。不管是哪种方式,它仍然将地址识别为file.getPath()返回“c:\example.txt”@AJCol,我想有人把它弄糊涂了。在Java中,必须将反斜杠(\)加倍才能在字符串文本中使用,但不需要使用常规斜杠(
/
)。Windows文件系统会将其视为两个斜杠,可能无害,也可能无害——我不确定。可能是@BrendanLong的副本我在发布此内容之前访问过该页面,但没有解决方案有效,因此这个问题是:为什么路径名中有两个斜杠?@ajb这正是我被教导的方法。不管是哪种方式,它仍然将地址识别为file.getPath()返回“c:\example.txt”@AJCol,我想有人把它弄糊涂了。在Java中,必须将反斜杠(\)加倍才能在字符串文本中使用,但不需要使用常规斜杠(
/
)。Windows文件系统会将此视为两个斜杠,可能无害,也可能无害--我不确定。谢谢帮助,更新了相应的问题谢谢帮助,更新了相应的问题HastLine()返回true,尽管我希望它返回false,这很奇怪…@AJCol您在循环中使用了
hasNextLine
,对吗?而不是一次?我希望
hasNextLine()
返回前N次
true
,然后返回
false
,如果您的文件中有N行。我刚刚打印了.next(),它实际上打印了文件中的第一个字,所以我猜它与文件没有任何关系。谢谢你给我指引了正确的方向!hasNextLine()返回true,尽管我希望它返回false,这很奇怪…@AJCol您在循环中使用了
hasNextLine
,对吗?而不是一次?我希望
hasNextLine()
返回前N次
true
,然后返回
false
,如果您的文件中有N行。我刚刚打印了.next(),它实际上打印了文件中的第一个字,所以我猜它与文件没有任何关系。谢谢你给我指引了正确的方向!