How to import a Text file content to a JTextArea in a Java application?

后端 未结 5 2058
夕颜
夕颜 2020-12-18 16:19

how to import a Text file content to a JTextArea in a Java application using JFileChooser?

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 17:09

    should be something like the following code:

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

    Luca

提交回复
热议问题