import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class HtmlWatcher {
private String htmlFilePath;
private String previousContent = "";
private JFrame frame;
private JTextArea textArea;
public HtmlWatcher(String htmlFilePath) {
this.htmlFilePath = htmlFilePath;
// Create GUI
frame = new JFrame("HTML Watcher");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane);
frame.setVisible(true);
// Start watching
Timer timer = new Timer();
timer.scheduleAtFixedRate(new WatchTask(), 0, 1000); // Check every 1 second
}
private class WatchTask extends TimerTask {
@Override
public void run() {
try {
String currentContent = readHtmlFile();
if (!currentContent.equals(previousContent)) {
textArea.setText(currentContent);
previousContent = currentContent;
System.out.println("HTML file changed."); // Simple error message
}
} catch (IOException e) {
textArea.setText("Error reading HTML file: " + e.getMessage());
System.err.println("Error reading HTML file: " + e.getMessage());
}
}
}
private String readHtmlFile() throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(htmlFilePath))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
return content.toString();
}
}
public static void main(String[] args) {
// Replace with your HTML file path
String htmlFilePath = "example.html";
SwingUtilities.invokeLater(() -> new HtmlWatcher(htmlFilePath));
}
}
Add your comment