日期:2014-05-20 浏览次数:21173 次
class SaveFileRunnable implements Runnable {
public void run() {
while (true) {
// 从标题栏取得文件名称
File file = new File(getTitle());
// 若指定的文件不存在
if (!file.exists()) {
// 执行另存为
saveFileAs();
} else {
try {
// 开启指定的文件
BufferedWriter buf = new BufferedWriter(new FileWriter(
file));
// 将文字编辑区的文字写入文件
buf.write(textArea.getText());
buf.close();
// 设定状态栏为未修改
stateBar.setText("未修改");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.toString(),
"写入文件失败", JOptionPane.ERROR_MESSAGE);
}
}
try {
TimeUnit.MICROSECONDS.sleep(60000);//可以设置保存时间间隔 比如一分钟
} catch (InterruptedException e) {
e.printStackTrace();
}//这里设置保存时间
}
}
}