Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.nio.file.DirectoryStream;
  4. import java.nio.file.Files;
  5. import java.nio.file.Path;
  6. import java.nio.file.Paths;
  7. import java.nio.file.attribute.FileTime;
  8. import java.util.Iterator;
  9.  
  10.  
  11. public class DirMonitor{
  12. private Path myPath;
  13.  
  14. public DirMonitor(Path myPath) throws IOException{
  15. if (!(Files.isReadable(myPath)) || !(Files.isDirectory(myPath))){
  16. throw new IOException();
  17. }
  18. this.myPath = myPath;
  19. }
  20.  
  21. public void displayFiles() throws IOException{
  22.  
  23. Iterator<Path> it = Files.newDirectoryStream(myPath).iterator();
  24.  
  25. while (it.hasNext()){
  26. System.out.println(it.next());
  27.  
  28. }
  29. }
  30.  
  31. public long sizeOfFiles() throws IOException{
  32.  
  33. long size=0;
  34.  
  35. Iterator<Path> it = Files.newDirectoryStream(myPath).iterator();
  36.  
  37. while (it.hasNext()){
  38. Path path =it.next();
  39.  
  40. if(!(Files.isDirectory(path)))
  41. size += Files.size(path);
  42.  
  43. }
  44.  
  45. // renvoie des octets
  46. return size;
  47.  
  48. }
  49.  
  50.  
  51. public Path mostRecent() throws IOException{
  52. FileTime mr= FileTime.fromMillis(0) ; //most recent
  53. Path path = null;
  54.  
  55. for ( Path allFiles : Files.newDirectoryStream(myPath)){
  56. if(mr.compareTo(Files.getLastModifiedTime(allFiles)) < 0){
  57. path = allFiles;
  58. mr = Files.getLastModifiedTime(allFiles);
  59. }
  60. }
  61.  
  62. return path;
  63. }
  64.  
  65. /*
  66. class SizeFilter implements DirectoryStream.Filter<Path>
  67. {
  68.  
  69.  
  70. @Override
  71. public boolean accept(Path arg0) throws IOException {
  72. // TODO Auto-generated method stub
  73. return false;
  74. }
  75.  
  76. }
  77.  
  78. */
  79. public static void main(String[] args) throws IOException {
  80. DirMonitor dm = new DirMonitor(Paths.get("."));
  81. dm.displayFiles();
  82. System.out.println(dm.sizeOfFiles());
  83. System.out.println(dm.mostRecent());
  84. }
  85.  
  86.  
  87.  
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement