如何在Netbeans中获取文本区域以显示我已保存在文本文件中的内容?我希望文本区域txtAllOrders在单击按钮btnViewOrders时显示文本文件Output.txt中的内容。
答案 0 :(得分:1)
我不是Java的专业程序员所以如果我错了请纠正我,但我会尝试这个:
FileInputStream fstream = new FileInputStream("YourFile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String sw ="";
try
{
while(sw != null)
{
sw = br.readLine();
txtAllOrders.append(sw + "\n");
}
} catch (IOException ex) {
ex.printStackTrace();
}
答案 1 :(得分:0)
尝试将以下代码添加到btnViewOrders
File file = new File("Output.txt");
FileInputStream fis = null;
String text = "";
try {
fis = new FileInputStream(file);
int content;
while ((content = fis.read()) != -1) {
text += (char) content;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
txtAllOrders.setText(text);