2016-10-22 180 views
0

使用eclipse,我的项目文件夹中标题为bad_words.txt的文件名为HackGSU。我想找到该文件并阅读该文件的内容。我看到这个答案how to read text file relative path,我想这:如何读取位于项目文件夹中的文件java

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; 
} 

但我收到此回溯:

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) 

我也因此这个答案How to read a text file directly from Internet using Java?,但我曾与URL构造一个问题。

如果我把本地文件路径(C://.../bad_words.txt)它工作得很好,但我怎样才能让程序读取文件,如果我打包软件它仍然会找到适当的文件路径。

+0

改为'HackGSU/bad_words.txt' – emotionlessbananas

+0

'HackGSU'是项目的文件夹吗?或其中的子文件夹? – c0der

+0

'p.concat(“/ HackGSU/bad_words.txt”)'不会像你可能除外。字符串是不可变的。 'concat'不会将某些内容添加到'p',但会返回一个新的String。更改为'p + =“/ HackGSU/bad_words.txt”' –

回答

1

望着误差不看像你正在得到完整的路径。取而代之的

p.concat("/HackGSU/bad_words.txt"); 

尝试

p = p.concat("/HackGSU/bad_words.txt"); 
+0

“/ HackGSU”部分是个问题,因为我已经拥有了这个文件夹。谢谢! –

0

你或许应该用反斜杠代替斜杠:

p.concat("\HackGSU\bad_words.txt"); 

您也可以尝试键入双反斜线:

p.concat("\\HackGSU\\bad_words.txt"); 
+0

'concat'只是不会改变结果中的某个东西,因为字符串是不可变的,所以需要一个新的任务返回到p –

1

如果你的资源是已经在classpath中,你不需要使用相对路径与File类。

java.net.URL url = getClass().getResource("bad_words.txt"); 
File file = new File(url.getPath()); 

甚至直接的InputStream:您可以轻松地像classpath中获得它

InputStream in = getClass().getResourceAsStream("bad_words.txt"); 

既然你有一个static方法使用:

InputStream in = YourClass.class.getResourceAsStream("bad_words.txt"); 

此外,正如我提到它已经在我的评论中:

p.concat("/HackGSU/bad_words.txt"); 

不会连接到p,但会返回一个新的连接字符串,因为字符串是不可变的。 简单使用:p+="/HackGSU/bad_words.txt"改为

相关问题