0
ファイルの内容を監視し、JTextAreaに新しい行を追加しようとしています。私は、ファイルを監視するスレッドを作成しましたが、Scannerオブジェクトがファイルの終わりに達すると、それは動作を停止します。私は新しいScannerオブジェクトを作成し、beginからファイルを読み込む非常に簡単な方法を試しましたが、それは良い解決策ではありません。 それは停止し、何もしないバージョンです:ファイルの変更を監視する
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
何が質問ですか?コードはどこですか? –
[Java IO実装のunix/linux "tail -f"]の可能な複製(http://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f) – NPE