2011-08-01 14 views
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(); 
     } 

    } 

} 


} 
+0

コードを投稿できますか? – Maverik

+0

何が質問ですか?コードはどこですか? –

+3

[Java IO実装のunix/linux "tail -f"]の可能な複製(http://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f) – NPE

答えて

1

wait方法は、あなたがここに欲しいものではありません。代わりにThread.sleepを試してください。

+0

私はそれを変更します。私の実験の前にはかなり古いコードでした – jacbar

関連する問題