如何读取位于java项目文件夹中的文件
使用eclipse,我在名为HackGSU的项目文件夹中有一个名为bad_words.txt的文件。我想找到文件并读取文件的内容。我看到了这个答案,我试了一下:如何读取位于java项目文件夹中的文件,java,io,filepath,filereader,filenotfoundexception,Java,Io,Filepath,Filereader,Filenotfoundexception,使用eclipse,我在名为HackGSU的项目文件夹中有一个名为bad_words.txt的文件。我想找到文件并读取文件的内容。我看到了这个答案,我试了一下: 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; /
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;
}
私有静态字符串字[][]=新字符串[3][];
专用静态int ok=17+2//在文本文件中添加两个注释和空行
专用静态int med=26+2//在文本文件中添加两个注释和空行
专用静态int bad=430+1//在文本文件中添加一条注释
私有静态字符串p=新文件(“”).getAbsolutePath();
公共静态字符串[][]openFile(){
p、 concat(“/HackGSU/bad_words.txt”);
//设置每行字数的限制
单词[0]=新字符串[getOk()];
单词[1]=新字符串[getMed()];
单词[2]=新字符串[getBad()];
//读取文本文件以将坏字添加到数组中
试一试{
FileReader fr=新的FileReader(p);
//包装文件
BufferedReader br=新的BufferedReader(fr);
//将文件的每一行添加到单词数组中
对于(inti=0;i
但我收到了回溯:
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)
java.io.FileNotFoundException:C:\Users\nRow\u 000\workspaceProjects\HackGSU(访问被拒绝)
位于java.io.FileInputStream.open0(本机方法)
位于java.io.FileInputStream.open(未知源代码)
位于java.io.FileInputStream。(未知源)
位于java.io.FileInputStream。(未知源)
位于java.io.FileReader。(未知源)
在gsu.hack.harrist.BadWords.openFile(BadWords.java:28)上
在gsu.hack.harrist.HarassFilter.(HarassFilter.java:10)
位于gsu.hack.harrist.CheckPercentage.main(CheckPercentage.java:20)
线程“main”java.lang.NullPointerException中出现异常
在gsu.hack.harrist.HarassFilter.checkHarass(HarassFilter.java:23)
位于gsu.hack.harrist.CheckPercentage.main(CheckPercentage.java:20)
我也是这样回答的,但URL构造函数有问题
如果我放置本地文件路径(C://…/bad_words.txt),它可以正常工作,但是如何让程序读取文件,以便在打包软件时仍能找到正确的文件路径。您可能应该使用反斜杠而不是斜杠:
p.concat("\HackGSU\bad_words.txt");
您也可以尝试键入双反斜杠:
p.concat("\\HackGSU\\bad_words.txt");
从错误的角度看,您似乎没有得到完整的路径。而不是
p.concat("/HackGSU/bad_words.txt");
试一试
如果资源已经在类路径中,则不需要使用
文件
类的相对路径。您可以很容易地从类路径获得它,如:
java.net.URL url = getClass().getResource("bad_words.txt");
File file = new File(url.getPath());
或者甚至直接到输入流:
InputStream in = getClass().getResourceAsStream("bad_words.txt");
由于您有一个静态
方法,请使用:
InputStream in = YourClass.class.getResourceAsStream("bad_words.txt");
此外,正如我在评论中已经提到的:
p.concat("/HackGSU/bad_words.txt");
不连接到p
,但返回一个新的连接字符串,因为字符串是不可变的。
只需使用:
p+=“/HackGSU/bad_words.txt”
改为HackGSU/bad_words.txt
是项目的文件夹吗?或者其中的子文件夹?p.concat(“/HackGSU/bad_words.txt”)
将无法正常工作。字符串是不可变的concat
不会向p
添加内容,而是返回一个新字符串。在项目文件夹下更改为p+=“/HackGSU/bad_words.txt”
@c0。我意识到我必须从文件中删除“/HackGSU”path@MadMatts是的,这是一个问题,我也必须更改文件路径concat
只会不会更改结果中的任何内容,因为字符串是不可变的,您需要重新分配回pThe“/HackGSU”部分是个问题,因为我已经有了文件夹。谢谢
p.concat("/HackGSU/bad_words.txt");