//文件打开函数
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//初始化文件选择窗口
FileDialog fileDialog;
//An abstract representation of file and directory pathnames.
File file = null;
Frame frame = null;
fileDialog = new FileDialog(frame, "Open", FileDialog.LOAD);
fileDialog.setVisible(true); //创建并显示打开文件对话框
try { //以缓冲区方式读取文件内容
jTextArea1.setText("");
file = new File(fileDialog.getDirectory(), fileDialog.getFile());
FileReader filereader = new FileReader(file);
BufferedReader bufferreader = new BufferedReader(filereader);
String aline;
while ((aline = bufferreader.readLine()) != null)
//按行读取文本,显示在TEXTAREA中
jTextArea1.append(aline + "\r\n");
filereader.close();
bufferreader.close();
} catch (IOException e) {
System.out.println(e);
}
}
Java读取文件到TextArea的实现
最新推荐文章于 2022-07-24 07:02:06 发布