2011-08-11 84 views

回答

7

應該像下面的代碼:

JFileChooser chooser = new JFileChooser(); 
int returnVal = chooser.showOpenDialog(null); //replace null with your swing container 
File file; 
if(returnVal == JFileChooser.APPROVE_OPTION)  
    file = chooser.getSelectedFile();  
} 

JTextArea text = new JTextArea(); 
BufferedReader in = new BufferedReader(new FileReader(file)); 
String line = in.readLine(); 
while(line != null){ 
    text.append(line + "\n"); 
    line = in.readLine(); 
} 

盧卡

+1

-1,是的,我知道這個作品。但我傾向於投票解決方案,重新發明輪子。所有文本組件都支持應該使用的read(...)方法。 – camickr

+1

@camickr我同意我們不應該重新發明輪子......無論如何我沒有找到你的建議,我不明白爲什麼你只投了礦,當有其他兩個答案時提出了同樣的方法沒有張貼代碼。 – Maverik

+2

我的建議是使用read(..)方法。我把它投下來是因爲這是被接受的答案,我不希望搜索論壇的人找到這個答案來使用它。 – camickr

2

要將文件的內容導入JTextArea,只需執行以下步驟即可!

  1. 創建一個框架併爲其添加一個JTextArea。
  2. 您聲明並初始化JFileChooser。
  3. 您將偵聽器添加到JFileChooser。
  4. 在你的actionPerformed中,你應該選擇被選中的文件,並將它傳遞給一個可以讀取這個文件的方法(參見下面的註釋)。
  5. 在該方法中,您將打開一個文件讀取器並逐行讀取該文件的內容。在你這樣做的時候,你將每行添加到JTextArea。
  6. 當您到達文件末尾時,關閉文件閱讀器。
  7. 運行程序,你應該很好去。

以上步驟足以完成您的任務。然而,當你試試看,我會編輯我的帖子並添加一個可能的解決方案。

注意:您必須注意,當您使用JFileChooser選擇文件時,它將返回File類型的對象。然後,您應該使用File類提供的getName()方法來獲取文件的名稱。

可能有幫助的鏈接!
JFileChooser
File
Java tutorials on how to use the JFileChooser

3

Document Viewer

import java.awt.BorderLayout; 
import java.awt.event.*; 
import javax.swing.*; 
import java.io.File; 

class DocumentViewer { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       final JFrame f = new JFrame("Document Viewer"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       final JFileChooser fileChooser = new JFileChooser(); 

       JPanel gui = new JPanel(new BorderLayout()); 

       final JEditorPane document = new JEditorPane(); 
       gui.add(new JScrollPane(document), BorderLayout.CENTER); 

       JButton open = new JButton("Open"); 
       open.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent ae) { 
         int result = fileChooser.showOpenDialog(f); 
         if (result==JFileChooser.APPROVE_OPTION) { 
          File file = fileChooser.getSelectedFile(); 
          try { 
           document.setPage(file.toURI().toURL()); 
          } catch(Exception e) { 
           e.printStackTrace(); 
          } 
         } 
        } 
       }); 
       gui.add(open, BorderLayout.NORTH); 

       f.setContentPane(gui); 
       f.pack(); 
       f.setSize(400,300); 
       f.setLocationByPlatform(true); 

       f.setVisible(true); 
      } 
     }); 
    } 
} 
0
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    JFileChooser jf = new JFileChooser(); 
    final JEditorPane document = new JEditorPane(); 
    int returnval=jf.showDialog(this, null); 
    File file = null; 
    if(returnval == JFileChooser.APPROVE_OPTION)  
    file = jf.getSelectedFile(); 
    String str ; 
    try { 
     byte bt[]= Files.readAllBytes(file.toPath()); 
     str=new String(bt,"UTF-8"); 
     System.out.println(str); 
    } catch (IOException ex) { 
     Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
相關問題