0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

11/28

Posted at

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileReaderSample {

// 呼び出されるたびに最新の内容を返す
public static String readFile(String filePath) {
    try {
        Path path = Paths.get(filePath);
        return Files.readString(path);  // ファイル全体を文字列として読む
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}

public static void main(String[] args) {
    String path = "data.txt";

    // 何度呼んでも都度ファイルを読み直す
    while (true) {
        String content = readFile(path);
        System.out.println("最新のファイル内容:");
        System.out.println(content);

        try {
            Thread.sleep(3000); // 3秒おきにチェック
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?