0
我試圖監視文件內容並向JTextArea添加任何新行。我創建了監視文件的線程,但是當掃描器對象到達文件末尾時,它停止工作。我嘗試了非常簡單的方法,它創建新的Scanner對象並從頭開始讀取文件,但這不是很好的解決方案。 這是該停止,什麼也不做的版本:監控文件更改
public class TextAreaThread implements Runnable {
JTextArea text = null;
File file = null;
Scanner read = null;
public TextAreaThread(JTextArea text, File file) {
this.text = text;
this.file = file;
try{
read = new Scanner(file);
}catch(FileNotFoundException e){
JOptionPane.showMessageDialog(null,"Wrong file or file doesn't exist","Error",JOptionPane.ERROR_MESSAGE);
}
}
public void run() {
while(true){
while(read.hasNext())
text.append(read.nextLine()+"\n");
try {
wait(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
您可以發佈您的代碼嗎? – Maverik
什麼問題?代碼在哪裏? –
[unix/linux的Java IO實現「tail -f」]的可能的重複(http://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f) – NPE