2012-03-17 48 views
0

我創建了一個「幫助」文本文件,當用戶在未打開文件選擇器的情況下單擊我的Java應用程序上的「幫助」時,我想打開該文件。我已保存在同一個地方的幫助文件作爲我的代碼如何在沒有文件選擇器的情況下打開文件

我嘗試:

JTextArea open = new JTextArea(); 
TabPane.add ("Help", open); 
open.read (new FileReader (help.txt), null); 

回答

0

如果我給你念你要顯示的文件,以textarea的內容,對不對?

JTextArea open = new JTextArea(); 

BufferedInputStream inStream = new BufferedInputStream(this.getClass().getResourceAsStream("help.txt")); 
    byte[] chars = new byte[1024]; 
    int bytesRead = 0; 
    try { 
     while((bytesRead = inStream.read(chars)) > -1){ 
      open.append(new String(chars, 0, bytesRead)); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

你可以那樣做的話..

+0

我不必然要在文本區域中顯示它只是簡單地打開該文件,但我不知道該代碼的調用特定文件。 – donthedestroyer 2012-03-17 15:02:37

+0

嗨,它讀取您的help.txt文件,並將結果寫入JTextArea .. this.getClass()。getResourceAsStream(「help.txt」)訪問您的文件 – Vossi 2012-03-17 16:47:41

+0

謝謝它由於某種原因它不是拿起help.txt即使它已被保存在相同的位置,但我已經制定出我自己的解決方案,現在「相當」地工作 – donthedestroyer 2012-03-17 17:47:07

相關問題