Created
October 19, 2014 22:24
-
-
Save OlgaKulikova/def8f1e759de7e73d160 to your computer and use it in GitHub Desktop.
Проект Monitor2
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.util.Arrays; | |
public class FileEvent implements IFileEvent { | |
@Override | |
public void onFileAdded(File fdir) { | |
System.out.println("Directory includes files .txt" + Arrays.toString(fdir.list())); | |
} | |
} |
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; | |
public interface IFileEvent { | |
void onFileAdded(File fdir); | |
} |
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; | |
//Написать код для мониторинга каталога. Выводить на экран | |
// предупреждение если в каталог добавляется текстовый файл (*.txt). | |
public class Main { | |
public static void main(String[] args) { | |
String dir = "d:\\Dir"; | |
Monitor m = new Monitor(dir, 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.io.FilenameFilter; | |
public class Monitor { | |
String dir; | |
IFileEvent event; | |
public Monitor(String dir, IFileEvent event) { | |
this.dir = dir; | |
this.event = event; | |
} | |
public void start() { | |
while (true) { | |
File fdir = new File(dir); | |
if (fdir.exists() && fdir.isDirectory() && event != null) { | |
File[] files = fdir.listFiles(new FilenameFilter() { | |
@Override | |
public boolean accept(File file, String name) { | |
return name.endsWith(".txt"); | |
} | |
}); | |
if (files.length != 0) { | |
event.onFileAdded(fdir); | |
break; | |
} | |
} | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
} | |
System.out.println("Waiting..."); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment