2016-10-22 182 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"改爲

相關問題