修复Java中的文件未找到异常

huangapple go评论74阅读模式
英文:

Fixing file not found exception in Java

问题

以下是翻译好的代码部分:

import java.io.FileNotFoundException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class WordSearch {
    private static final int MIN_WORD_LENGTH = 3;

    public static void main(String[] args) throws FileNotFoundException {
        List<String> dictList = new ArrayList<>();
        dictList = dictRead(args[0]);
    }

    public static List<String> dictRead(String filePath) throws FileNotFoundException {
        Scanner dictScan = null;
        dictScan = new Scanner(new File(filePath));
        List<String> dictList = new ArrayList<>();
        int i = 0;
        while (dictScan.hasNext()) {
            dictList.add(i, dictScan.nextLine());
            i += 1;
        }
        return dictList;
    }
}

请注意,这是你提供的代码的中文翻译。如果你对代码中的异常有疑问,请随时提问。

英文:

I have been working on a small program and I'm new to Java. But it keeps raising filenotfound exception.
Here's my code:

import java.io.FileNotFoundException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/* 
 * Don&#39;t forget your file header comment here!
 */

public class WordSearch {
    /*
     * This is how you declare constants in Java. You can now type
     * &#39;MIN_WORD_LENGTH&#39; anywhere you want to refer to it.
     */
    private static final int MIN_WORD_LENGTH = 3;

    public static void main(String[] args) throws FileNotFoundException {
        // Remember, to access command-line arguments, you use args[0],
        // args[1],...
        // See CommandLine.java and Stdin.java in the Class Examples github for examples.
    	List&lt;String&gt; dictList= new ArrayList&lt;&gt;();
    	dictList = dictRead(args[0]);
    }
    public static List&lt;String&gt; dictRead (String filePath) throws FileNotFoundException{
    	Scanner dictScan = null;
    	dictScan = new Scanner(new File(filePath));
    	List&lt;String&gt; dictList = new ArrayList&lt;&gt;();
    	int i=0;
    	while (dictScan.hasNext()) {
    		dictList.add(i, dictScan.nextLine());
    		i+=1;
    	}
    	return dictList;
    }

}

This I don't know why I keep getting this exception. I changed my run configuration and put TestCases/dictionary.txt as my first argument.

Here's a picture of my directory and I'm running WordSearch.java:

修复Java中的文件未找到异常

答案1

得分: 1

你的文件夹结构中包含一个项目内嵌在另一个项目中。在你的集成开发环境中,如果只导入了作为项目的 PA1-WordSearch,它将正常工作。

英文:

Your folder structure has project within project. In you IDE, if you have only PA1-WordSearch imported as a project, it will work.

答案2

得分: 0

希望它对你有用

dictList = dictRead("./FileFolder/aaa.txt");

英文:

I hope it will work for you

dictList = dictRead("./FileFolder/aaa.txt");

答案3

得分: 0

文件不在Java期望的位置。

TestCases/dictionary.txt 是一个相对路径。为了在您的驱动器上找到该文件,它会相对于某个"工作目录"进行解释。您可能可以在运行配置中找到它。

但是,更健壮的方法是使用绝对路径指定文件,即以 C:\ 或类似内容开头,如果您在Windows计算机上的话。使用绝对路径使您不依赖于工作目录。

因此:使用Windows资源管理器找到文件的绝对路径,并将该路径作为运行配置中的参数插入。

英文:

The file is not in the place where Java expects it.

TestCases/dictionary.txt is a relative path. To find the file on your drive, it gets interpreted relative to some "working directory". You can probably find that in the run configuration.

But it's more robust to specify the file with an absolute path, one that begins with C:\ or similar if you are on a Windows machine. Using the absolute path makes you independent of the working directory.

So: find the absolute path of your file using the Windows Explorer, and insert that path as the argument in your run configuration.

huangapple
  • 本文由 发表于 2020年9月9日 15:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63806670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定