I am trying to build a program that loops forever, it starts by watching a file for changes. when it changes it takes the string saved to the file and then waits again for another change to the file, if both strings recorded are the same then it upload some information to a MySQL DB, but at the last function in the while loop just repeatedly repeats and doesn't finish the while loop and restart!
Here's my code,
package ClockCard;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.sql.Timestamp;
public class FileWatch implements Runnable {
static String clkID;
static String clkID2;
static String ts;
static String ts1;
static boolean done = false;
static boolean REdone = false;
static boolean finished = false;
static boolean ready;
public static void main(String[] args) throws IOException,
InterruptedException {
while (true) {
checkFile();
Thread.sleep(3000);
REcheckFile();
Thread.sleep(500);
//Thread.
if (clkID.equals(clkID2)) {
DbConnect.test();
}
else {
System.out.println("Wrong matching ID's");
}
}
}
// Thread th = new Thread(new FileWatch());
// th.start();
// checkFile();
// REcheckFile();
// if (clkID2.equals(clkID2)){
// System.out.println("worked");
// }
// else {
// System.out.println("not worked");
// }
//
public static void check() throws InterruptedException{
checkFile();
Thread.sleep(3000);
REcheckFile();
Thread.sleep(500);
}
public String getClkId() {
return clkID;
}
public static void checkFile() {
while (!done) {
try {
WatchService watcher = FileSystems.getDefault()
.newWatchService();
Path dir = Paths.get("C:\\test");
dir.register(watcher, ENTRY_MODIFY);
System.out.println("Watch Service registered for dir: "
+ dir.getFileName());
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
System.out.println(kind.name() + ": " + fileName);
if (kind == ENTRY_MODIFY
&& fileName.toString().equals("test.txt")) {
System.out.println("My source file has changed!!!");
String sCurrentLine = null;
try (BufferedReader br = new BufferedReader(
new FileReader("C:\\test\\test.txt"))) {
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
clkID = sCurrentLine;
System.out.println(clkID);
java.util.Date date = new java.util.Date();
date = new Timestamp(date.getTime());
// System.out.println(new
// Timestamp(date.getTime()));
ts = date.toString();
System.out.println(ts);
}
} catch (IOException e) {
e.printStackTrace();
}
File inputFile = new File("C:\\test\\test.txt"); // Your
// file
File tempFile = new File("C:\\test\\myTempFile.txt");// temp
// file
BufferedReader reader = new BufferedReader(
new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(
new FileWriter(tempFile));
String currentLine;
while ((currentLine = reader.readLine()) != null) {
currentLine = ("");
writer.write(currentLine);
}
writer.close();
reader.close();
done = true;
boolean successful = tempFile.renameTo(inputFile);
System.out.println(successful);
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
public static void REcheckFile() {
while (!REdone) {
try {
WatchService watcher = FileSystems.getDefault()
.newWatchService();
Path dir = Paths.get("C:\\test");
dir.register(watcher, ENTRY_MODIFY);
System.out.println("Watch Service registered for dir: "
+ dir.getFileName());
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
System.out.println(kind.name() + ": " + fileName);
if (kind == ENTRY_MODIFY
&& fileName.toString().equals("test.txt")) {
System.out.println("My source file has changed!!!");
String sCurrentLine = null;
try (BufferedReader br = new BufferedReader(
new FileReader("C:\\test\\test.txt"))) {
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
clkID2 = sCurrentLine;
System.out.println(clkID2);
java.util.Date date1 = new java.util.Date();
date1 = new Timestamp(date1.getTime());
// System.out.println(new
// Timestamp(date.getTime()));
String ts1 = date1.toString();
System.out.println(ts1);
}
} catch (IOException e) {
e.printStackTrace();
}
File inputFile = new File("C:\\test\\test.txt"); // Your
// file
File tempFile = new File("C:\\test\\myTempFile.txt");// temp
// file
BufferedReader reader = new BufferedReader(
new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(
new FileWriter(tempFile));
String currentLine;
while ((currentLine = reader.readLine()) != null) {
currentLine = ("");
writer.write(currentLine);
}
writer.close();
reader.close();
REdone = true;
boolean successful = tempFile.renameTo(inputFile);
System.out.println(successful);
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
checkFile();
}
}
The dbConnect.test() simply outputs test, but it continuously just outputs test test test in stead of restarting, any help would be great thanks!!
Jonny
Inside your checkFile()
and ReCheckFile()
methods, you are using done
and ReDone
fields, to loop.
But once they both are set to true
, both your methods become useless. And that will be set to true
in the first iteration (only after it's set to true, it will exit the methods).
Hence, from your second iteration, checkFile()
and reCheckFile()
is effectively not executed.
Reset the fields to false
at the top of checkFile()
and reCheckFile()
methods, or inside the while
loop in main
.