JFileChooser ch = new JFileChooser();
if(ch.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
try{
File file = ch.getSelectedFile();
BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
while(br.readLine() != null){
textArea1.setText(br.readLine());
textArea1.setText("\n");
}
}catch (FileNotFoundException ex) {}catch (IOException ex) {}
}
所以我想为我的类(class)制作一个程序,在其中我将从我选择的文件中加载txt数据,并将其写入文本框中,然后继续进行。但我失败了。我不知道为什么,但它在文本框中没有显示任何内容。 另外我想将代码传输到表中并编辑它并随后保存。但我也失败了。
最佳答案
您应该将要显示的文本收集到 StringBuilder
中,然后显示它:
String line = null;
StringBuilder sb = new StringBuilder();
while((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
textArea1.setText(sb.toString());
关于Java txt 加载代码不起作用,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49873533/