Last time, I wrote Process and output CSV file. I want to process the CSV file when it comes into the target directory. I thought, so I will write down the code.
-** WatchService usage memo ** -** Wait for the file to be created in Java, detect the creation and then perform the next process ) ** : grinning: I referred to this page. Thank you very much.
test.java
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.nio.file.Watchable;
import static java.nio.file.StandardWatchEventKinds.*;
import static java.nio.file.WatchEvent.*;
public class test {
public static void main(String[] args) throws Exception {
Path dir = Paths.get("C://develop//BOX");
WatchService watcher = FileSystems.getDefault().newWatchService();
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
for (;;) {
WatchKey watchKey = watcher.take();
for (WatchEvent<?> event: watchKey.pollEvents()) {
if (event.kind() == OVERFLOW) continue;
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
System.out.format("%s: %s\n", event.kind().name(), child);
}
watchKey.reset();
}
}
@SuppressWarnings("unchecked")
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>)event;
}
}
Now, create a directory called ** BOX ** in ** C: \ develop ** and
Try compiling test.java anywhere and running the class. (* This time I am running with D: )
Wow! If you get an error when compiling:: beginner: ** here ** It will be helpful.
Now let's run the class file. Try putting a suitable file in the BOX directory.
Oops! The sample .txt that I put in the directory It says ENTRY_CREATE and ENTRY_MODIFY.
When I delete the file from the directory, I see ENTRY_DELETE.
I think that you can understand it after actually moving it, so I pasted the image when I moved it first This is written in test.java. (The comment is written in my interpretation, so I may have made a mistake ...) The second line ** WatchService watcher = FileSystems.getDefault (). newWatchService (); ** It seems to be a fixed phrase, so I will use it as it is without thinking deeply.
Path dir = Paths.get("C://develop//BOX"); //"dir"Specify the directory to be monitored in
WatchService watcher = FileSystems.getDefault().newWatchService(); //WatchService"watcher"Created with the name
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); //"dir"Specify the event to be monitored in(Create,Delete,Change)
for (;;) { //Implement an infinite loop to wait for an event
WatchKey watchKey = watcher.take(); //Watcher key from watcher"watchKey"To get
for (WatchEvent<?> event: watchKey.pollEvents()) { //You can get the List where WatchEvent is stored by pollEvents method.
if (event.kind() == OVERFLOW) continue; //"OVERFLOW"Is a special monitoring event that indicates that the event has disappeared or was destroyed
WatchEvent<Path> ev = cast(event);
Path name = ev.context(); //"name"Get in event context and set the file name
Path child = dir.resolve(name); //Full path with resolve method("dir"In the path of"name"Added)The set
System.out.format("%s: %s\n", event.kind().name(), child); //At the time of output, [event name:Instructions to output with [Full path of file]
}
watchKey.reset(); //Reset the watch key(To return the WatchKey state to READY)
test.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test{
public static void main(String[] args) {
process("20180712.csv", ":00,","output.csv");
}
public static void process(String read_file, String searchString, String output_file){
try(FileReader fr = new FileReader(read_file);BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(output_file);BufferedWriter bw = new BufferedWriter(fw)){
String line;
while ((line = br.readLine()) != null) {
Pattern p = Pattern.compile(searchString);
Matcher m = p.matcher(line);
if (m.find()){
String[] csvArray;
csvArray = line.split(",");
String time = csvArray[0];
float press = Float.parseFloat(csvArray[1])%10000;
String pressure = String.format("%.2f", press);
String temperature = csvArray[2];
String outputLine = String.join(",",time,pressure,temperature);
bw.write(outputLine);
bw.newLine();
}else{
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
The previous code and the code I wrote above haven't changed much. It's almost just combined.
test.java
import java.nio.file.FileSystems; //Required for directory monitoring
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.nio.file.Watchable;
import static java.nio.file.StandardWatchEventKinds.*;
import static java.nio.file.WatchEvent.*;
import java.io.BufferedReader; //Required to process and output csv
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test {
public static void main(String[] args) throws Exception {
Path dir = Paths.get(System.getenv("CSV_DIR")); //This time, the directory to be monitored is an environment variable"CSV_DIR"Set to
WatchService watcher = FileSystems.getDefault().newWatchService();
dir.register(watcher, ENTRY_CREATE); //This time ENTRY_OK if only CREATE is detected
for (;;) {
WatchKey watchKey = watcher.take();
for (WatchEvent<?> event: watchKey.pollEvents()) {
if (event.kind() == OVERFLOW) continue;
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
String newfile =String.format("%s", child);
process(newfile, ":00,","D://tedkuma//★"+name); //Pass the full path of the detected file to the file read by process
System.out.println("Output success:"+name);
}
watchKey.reset();
}
}
@SuppressWarnings("unchecked")
static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>)event;
}
public static void process(String read_file, String searchString, String output_file){ //The following is the same as the previous one
try(FileReader fr = new FileReader(read_file);BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(output_file);BufferedWriter bw = new BufferedWriter(fw)){
String line;
while ((line = br.readLine()) != null) {
Pattern p = Pattern.compile(searchString);
Matcher m = p.matcher(line);
if (m.find()){
String[] csvArray;
csvArray = line.split(",");
String time = csvArray[0];
float press = Float.parseFloat(csvArray[1])%10000;
String pressure = String.format("%.2f", press);
String temperature = csvArray[2];
String outputLine = String.join(",",time,pressure,temperature);
bw.write(outputLine);
bw.newLine();
}else{
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Since Japanese comments and ★ marks are included in the java file javac alone will result in a compile error: sweat: : skull: Error: This character cannot be mapped to encoding MS932, so it will appear. This time I'm compiling with ** javac -encodhing utf-8 test.java **. Let's run it.
I set the environment variable "CSV_DIR" to the location C: \ develop \ monitored. (This monitored directory) Put the CSV file you want to process in this.
The file name put in the directory is displayed after Output success :.
Check the tedkuma directory of the D drive specified as the output destination. A file with a star at the beginning of the file name is output, and the csv inside is also processed.
This time is over.
Recommended Posts