英文:
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't forget your file header comment here!
*/
public class WordSearch {
/*
* This is how you declare constants in Java. You can now type
* 'MIN_WORD_LENGTH' 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<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;
}
}
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:
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论