//如果按下确定按钮,则获得该文件。  
         if(flag==JFileChooser.APPROVE_OPTION)   
           {   
               //获得你输入要保存的文件  
                 f=fc.getSelectedFile();   
               //获得文件名  
                 fileName=fc.getName(f);   
               //也可以使用fileName=f.getName();  
                 System.out.println(fileName);   
//这里读取文件中的内容。
           }   参考:public static List<String> readFile(File f) throws IOException {
if (f == null || !f.exists()) {
return null;
}
List<String> list = new ArrayList<String>();
String encoding = "UTF-16LE";
InputStreamReader read = new InputStreamReader(new FileInputStream(f),
encoding);
BufferedReader br = new BufferedReader(read);
String line = null;
while ((line = br.readLine()) != null) {
list.add(line);
}
br.close();
read.close();
return list;
}

解决方案 »

  1.   


    //如果按下確定按鈕,則獲得該文件。  
    if(flag==JFileChooser.APPROVE_OPTION){   
    t.setText("");//把你的textarea設置成全局
    //獲得該文件  
    f=fc.getSelectedFile();   
    System.out.println("open file----"+f.getName()); 
           
    try {
    InputStreamReader read = new InputStreamReader(
    new FileInputStream(f));
    BufferedReader br = new BufferedReader(read);
    String line = null;
    while ((line = br.readLine()) != null) {
    t.append(line+"\n");
    }
    br.close();
    read.close(); 
    } catch (Exception e) {
    // TODO: handle exception

    }   
      

  2.   

    DefaultEditorKit kit = new DefaultEditorKit();
    File file = ...; // jfilechooser 返回的文件
    try(FileReader reader = new FileReader(file)){
        kit.read(reader,jta.getDocument(),0);
    }catch(IOException|BadLocationException e){
        ...// process exception
    }
      

  3.   

    明白了,谢谢!我的问题出在没把JTextArea设置成全局。但是读入中文是乱码,不知道该怎么解决?