Guest User

Untitled

a guest
Jan 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. package cwiczenie4;
  2.  
  3. import java.io.File;
  4. import java.io.FilenameFilter;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.nio.file.attribute.BasicFileAttributes;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11.  
  12. public class Main {
  13.  
  14. public static void main(String args[]) throws Exception {
  15. //printFilesSimple("C:\\");
  16. printFilesDetails("C:\\");
  17. //printFiles("C:\\", ".txt");
  18. //printTree("C:\\");
  19. }
  20.  
  21. public static void printFilesSimple(String path) {
  22. File folder = new File(path);
  23. File[] listOfFiles = folder.listFiles();
  24.  
  25. for (File file : listOfFiles) {
  26. System.out.println(file.getName());
  27. }
  28. }
  29.  
  30. public static void printFilesDetails(String path) throws Exception {
  31. // to be implemented
  32. Path path2 = Paths.get(path);
  33. BasicFileAttributes attr;
  34. attr = Files.readAttributes(path2, BasicFileAttributes.class);
  35. DateFormat df = new SimpleDateFormat("yyyy/MM/dd mm:ss");
  36.  
  37. String dirPath = path;
  38. File dir = new File(dirPath);
  39. File[] files = dir.listFiles();
  40. if (files.length == 0) {
  41. System.out.println("The directory is empty");
  42. } else {
  43. for (File aFile : files) {
  44. //System.out.println(aFile.getName() + " - " +
  45. if(aFile.isDirectory()) {
  46. System.out.println(aFile.getName() + " DIR ");
  47. } else {
  48. System.out.println(aFile.getName() + " "+attr.size()+" "+df.format(attr.creationTime().toMillis()));
  49. }
  50. }
  51. }
  52. }
  53.  
  54. public static void printFiles(String path, String extensionFilter) {
  55. // to be implemented
  56. FilenameFilter mp3Filter = new FilenameFilter() {
  57. public boolean accept(File file, String name) {
  58. if (name.endsWith(extensionFilter)) {
  59. // match files extension
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. };
  66.  
  67. String dirPath = path;
  68. File dir = new File(dirPath);
  69. File[] files = dir.listFiles(mp3Filter);
  70. if (files.length == 0) {
  71. System.out.println("There is no "+extensionFilter+" files");
  72. } else {
  73. for (File aFile : files) {
  74. System.out.println(aFile.getName() + " - " + aFile.length());
  75. }
  76. }
  77. }
  78.  
  79. public static void printTree(String path) {
  80. // to be implemented
  81. // Example
  82. // dirname
  83. // dirname/file1
  84. // dirname/file2
  85. // dirname/dirname1
  86. // dirname/dirname1/file1
  87. // dirname/dirname1/file2
  88. // dirname/dirname2/file1
  89.  
  90. String dirPath = path;
  91. String longPath = "";
  92. File dir = new File(dirPath);
  93. File[] subDirectories = dir.listFiles();
  94.  
  95. if (subDirectories.length == 0) {
  96. System.out.println("The directory is empty");
  97. } else {
  98. for (File aFile : subDirectories) {
  99.  
  100. if(aFile.isFile()) {
  101. System.out.println(aFile.getAbsolutePath());
  102. }else if (aFile.isDirectory()) {
  103.  
  104.  
  105. }
  106.  
  107. }
  108. }
  109.  
  110.  
  111. }
  112.  
  113. }
Add Comment
Please, Sign In to add comment