sunfresh

Files

Jan 23rd, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. Files
  2. # Files Utils
  3.  
  4. >Hint: You can use the File class from the standard Java library for the implementation of the utils below (https://www.geeksforgeeks.org/file-class-in-java/).
  5.  
  6. ## 1. Directory Traversal
  7.  
  8. Write a recursive method `void traverseDirectories(String path)` which prints the names of all files in the given `path`
  9.  
  10. ### Example usage
  11.  
  12. ```java
  13. String path = Paths.get("demo").toAbsolutePath().toString();
  14. FilesUtils.traverseDirectories(path);
  15. ```
  16.  
  17. ### Expected output
  18.  
  19. ```
  20. demo:
  21. nested-1:
  22. file-1.md
  23. file-2.md
  24. file-3.md
  25. nested-1-1:
  26. example.txt
  27. nested-2:
  28. test-1.csv
  29. test-2.txt
  30. test-3.md
  31. ```
  32.  
  33. ## 2. Find Files
  34.  
  35. Write method `List<String> findFiles(String path, String extension)` which return a collection of all files which have the given `extension` in the given directory.
  36.  
  37. ### Example usage
  38.  
  39. ```java
  40. String path = Paths.get("demo").toAbsolutePath().toString();
  41. System.out.println(findFiles(path, "md"));
  42. ```
  43.  
  44. ### Expected output
  45.  
  46. ```
  47. [file-1.md, file-2.md, file-3.md, test-3.md]
  48. ```
  49.  
  50. ### 3. File Exists
  51.  
  52. Write a recursive method `boolean fileExists(String path, String fileName)`
  53.  
  54. ### Example usage
  55.  
  56. ```java
  57. String path = Paths.get("demo").toAbsolutePath().toString();
  58. System.out.println(fileExists(path, "test-3.md"));
  59. System.out.println(fileExists(path, "test-2.md"));
  60. ```
  61.  
  62. ### Expected Output
  63.  
  64. ```
  65. true
  66. false
  67. ```
  68.  
  69. ### 4. Directory Stats
  70.  
  71. Write a recursive method `Map<String, Integer> getDirectoryStats(String path)` which returns the number of files for each file extension.
  72.  
  73. ### Example usage
  74.  
  75. ```java
  76. String path = Paths.get("demo").toAbsolutePath().toString();
  77. System.out.println(FilesUtils.getDirectoryStats(path));
  78. ```
  79.  
  80. ### Expected Output
  81.  
  82. ```
  83. {txt=2, md=4, csv=1}
  84. ```
  85.  
  86.  
  87. public class FilesUtils {
  88.  
  89. public static Map<String, Integer> directoryStats = new HashMap<>();
  90.  
  91. public static void traverseDirectories(String path) {
  92. File dir = new File(path);
  93. File[] allFiles = dir.listFiles();
  94. if (allFiles == null) {
  95. return;
  96. }
  97. if (allFiles.length == 0) {
  98. return;
  99. }
  100. System.out.println(dir.getName() + ":");
  101.  
  102. String indent = "";
  103. traverseRecursive(allFiles, indent);
  104. }
  105.  
  106. private static void traverseRecursive(File[] list, String indent) {
  107. if (list == null) return;
  108. if (list.length == 0) return;
  109. indent += " ";
  110. for (File file : list) {
  111. if (file.isDirectory()) {
  112. System.out.println(indent + file.getName() + ":");
  113. traverseRecursive(file.listFiles(), indent);
  114. }
  115. if (file.isFile()) {
  116. System.out.println(indent + file.getName());
  117. }
  118. }
  119. }
  120.  
  121. public static List<String> findFiles(String path, String extension) {
  122. File fileSearch = new File(path);
  123. File[] allFiles = fileSearch.listFiles();
  124. List<String> result = new ArrayList<>();
  125. if (allFiles == null) {
  126. return result;
  127. }
  128. if (allFiles.length == 0) {
  129. return result;
  130. }
  131. for (File file : allFiles) {
  132. if (file.isDirectory()) {
  133. result.addAll(findFiles(file.toString(), extension));
  134. } else {
  135. if (file.getName().endsWith(extension)) {
  136. result.add(file.getName());
  137. }
  138. }
  139. }
  140. return result;
  141. }
  142.  
  143. public static boolean fileExists(String path, String fileName) {
  144. File dir = new File(path);
  145. File[] allFilesInDir = dir.listFiles();
  146. boolean fileNameExist = false;
  147.  
  148. if (allFilesInDir == null || allFilesInDir.length == 0) {
  149. return false;
  150. }
  151. for (File file : allFilesInDir) {
  152. if (file.isDirectory()) {
  153. fileNameExist = fileExists(file.toString(), fileName);
  154. }
  155. if (file.getName().equals(fileName)) {
  156. return true;
  157. }
  158. }
  159. return fileNameExist;
  160. }
  161.  
  162. public static Map<String, Integer> getDirectoryStats(String path) {
  163. File dir = new File(path);
  164. File[] allFilesInDir = dir.listFiles();
  165. for (File file : allFilesInDir) {
  166. String temp = file.getName().substring(file.getName().indexOf(".") + 1);
  167. if(file.isDirectory()){
  168. getDirectoryStats(file.toString());
  169. }
  170. if(file.isFile() && directoryStats.containsKey(temp)){
  171. directoryStats.replace(temp, directoryStats.get(temp) + 1);
  172. }
  173. if(file.isFile() && !directoryStats.containsKey(temp)){
  174. directoryStats.put(temp, 1);
  175. }
  176. }
  177. return directoryStats;
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment