[英]Read text and input file text to JTextArea
我的代碼有什么問題? 我正在嘗試讀取文本文件,然后將文本放入JTextArea
,但其輸入僅包含文本的最后一行。 怎么了?
代碼 :
public void read() {
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
pavadinimas = file.getName();
try {
FileInputStream fstream = new FileInputStream(fc.getCurrentDirectory() + "/" + pavadinimas);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
tekstas.setText(strLine);
}
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
使用append函數代替settext
不要重新發明輪子。 無需編寫循環代碼或附加您自己的行尾字符串。
使用JTextArea.read(...)
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.