[英]How to read a file that is located in the project folder java
Using eclipse, I have a file titled bad_words.txt in my project folder titled HackGSU.使用 Eclipse,我在名为 HackGSU 的项目文件夹中有一个名为 bad_words.txt 的文件。 I want locate the file and read the contents of the file.
我想定位文件并读取文件的内容。 I saw this answer how to read text file relative path and I tried this:
我看到了这个如何读取文本文件相对路径的答案,我试过这个:
private static String words[][] = new String[3][];
private static int ok = 17 + 2; //Add two for comment & empty line in text file
private static int med = 26 + 2; //Add two for comment & empty line in text file
private static int bad = 430 + 1; //Add one for comment in text file
private static String p = new File("").getAbsolutePath();
public static String[][] openFile() {
p.concat("/HackGSU/bad_words.txt");
//Set the a limit for the amount of words in each row
words[0] = new String[getOk()];
words[1] = new String[getMed()];
words[2] = new String[getBad()];
//Read the text file to add bad words to an array
try {
FileReader fr = new FileReader(p);
//Wrap file
BufferedReader br = new BufferedReader(fr);
//Add each line of file into the words array
for(int i = 0; i < words.length; i++) {
for(int j = 0; j < words[i].length; j++) {
try {
words[i][j] = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
}
return words;
}
But I received this traceback:但我收到了这个回溯:
java.io.FileNotFoundException: C:\Users\nbrow_000\workspaceProjects\HackGSU (Access is denied)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at gsu.hack.harassment.BadWords.openFile(BadWords.java:28)
at gsu.hack.harassment.HarassFilter.<clinit>(HarassFilter.java:10)
at gsu.hack.harassment.CheckPercentage.main(CheckPercentage.java:20)
Exception in thread "main" java.lang.NullPointerException
at gsu.hack.harassment.HarassFilter.checkHarass(HarassFilter.java:23)
at gsu.hack.harassment.CheckPercentage.main(CheckPercentage.java:20)
I also so this answer How to read a text file directly from Internet using Java?我也这样回答如何使用 Java 直接从 Internet 读取文本文件? , but I had a problem with the URL constructor.
,但我的 URL 构造函数有问题。
If I put the local file path (C://.../bad_words.txt) it works just fine, but how can I get the program to read the file so that if I package the software it will still find the proper file path.如果我把本地文件路径 (C://.../bad_words.txt) 它工作得很好,但我怎样才能让程序读取文件,以便如果我打包软件它仍然会找到正确的文件小路。
Looking at the error it doesn't look like you are getting the full path.查看错误,您似乎没有获得完整路径。 Instead of
代替
p.concat("/HackGSU/bad_words.txt");
Try尝试
p = p.concat("/HackGSU/bad_words.txt");
Check out the answer in this question, it might be what you are looking for! 找出这个问题的答案,可能是您要寻找的!
How to read file from relative path in Java project? 如何从Java项目中的相对路径读取文件? java.io.File cannot find the path specified
java.io.File找不到指定的路径
If your resource is already in the classpath, you don't need to make use of relative paths with the File
class.如果您的资源已经在类路径中,则不需要对
File
类使用相对路径。 You can easily obtain it from the classpath like:您可以轻松地从类路径中获取它,例如:
java.net.URL url = getClass().getResource("bad_words.txt");
File file = new File(url.getPath());
or even directly to an inputstream:甚至直接到输入流:
InputStream in = getClass().getResourceAsStream("bad_words.txt");
Since you have a static
method use:由于您有
static
方法,请使用:
InputStream in = YourClass.class.getResourceAsStream("bad_words.txt");
Furthermore, as I mentioned it already in my comment:此外,正如我在评论中已经提到的那样:
p.concat("/HackGSU/bad_words.txt");
does not concat to p
but returns a new concatenated String, because Strings are immutable.不连接到
p
而是返回一个新的连接字符串,因为字符串是不可变的。 Simply use: p+="/HackGSU/bad_words.txt"
instead只需使用:
p+="/HackGSU/bad_words.txt"
代替
You probably should use backslash instead of slash:您可能应该使用反斜杠而不是斜杠:
p.concat("\HackGSU\bad_words.txt");
You can also try to type double backslash:您也可以尝试键入双反斜杠:
p.concat("\\HackGSU\\bad_words.txt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.