Skip to content

Instantly share code, notes, and snippets.

@OlgaKulikova
Last active August 29, 2015 14:07
Show Gist options
  • Save OlgaKulikova/86adf78146ff039b089f to your computer and use it in GitHub Desktop.
Save OlgaKulikova/86adf78146ff039b089f to your computer and use it in GitHub Desktop.
Проект Monitor
package Monitor;
public class FileEvent implements IFileEvent {
@Override
public void onFileAdded() {
System.out.println("Files added!");
}
}
package Monitor;
public interface IFileEvent {
void onFileAdded();
}
package Monitor;
// Добавить в проект «монитор» функцию вывода даты создания файла на экран (см. java.io.File).
//Добавить в проект ф-ю мониторинга более одного файла.
public class Main {
public static void main(String[] args) {
String[] files = {"d:\\1.txt", "d:\\2.txt", "d:\\3.txt"};
Monitor m = new Monitor(files, new FileEvent());
m.start();
}
}
package Monitor;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
public class Monitor {
String[] files;
IFileEvent event;
public Monitor(String[] files, IFileEvent event) {
this.files = Arrays.copyOf(files, files.length);
this.event = event;
}
public void start() {
File f;
int n = 0;
while (true) {
for (int i = 0; i < files.length; i++) {
if (files[i] != null) {
f = new File(files[i]);
dateLastMode(f, files[i]);
if (f.exists() && f.isFile()) {
files[i] = null;
n++;
}
}
}
if (n == files.length) {
event.onFileAdded();
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
System.out.println("Waiting...");
}
}
public void dateLastMode(File f, String path) {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
System.out.println("File " + path + " modified " + sdf.format(f.lastModified()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment