Last active
August 29, 2015 14:07
-
-
Save OlgaKulikova/86adf78146ff039b089f to your computer and use it in GitHub Desktop.
Проект Monitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Monitor; | |
public class FileEvent implements IFileEvent { | |
@Override | |
public void onFileAdded() { | |
System.out.println("Files added!"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Monitor; | |
public interface IFileEvent { | |
void onFileAdded(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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