Guest User

Untitled

a guest
Jan 18th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. D:
  2. root
  3. popcorn-folder1
  4. popcorn-subfolder1
  5. popcorn-subfile1
  6. popcorn-file1
  7. popcorn-folder2
  8. popcorn-subfolder2
  9. popcorn-file2
  10.  
  11. D:
  12. root
  13. folder1
  14. subfolder1
  15. subfile1
  16. file1
  17. folder2
  18. subfolder2
  19. file2
  20.  
  21. package com.din.pach;
  22.  
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.nio.file.FileVisitResult;
  26. import java.nio.file.FileVisitor;
  27. import java.nio.file.Files;
  28. import java.nio.file.Path;
  29. import java.nio.file.Paths;
  30. import java.nio.file.StandardCopyOption;
  31. import java.nio.file.attribute.BasicFileAttributes;
  32.  
  33. public class FileNio {
  34.  
  35. public static void main(String[] args) throws IOException {
  36.  
  37. Path sourcePath = Paths.get("D:\root\");
  38.  
  39. Files.walkFileTree(sourcePath, new FileVisitor<Path>() {
  40. @Override
  41. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  42. // System.out.println("pre visit dir:" + dir);
  43. //rename(dir);
  44. //renameFile(dir);
  45. return FileVisitResult.CONTINUE;
  46. }
  47.  
  48. @Override
  49. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  50. //System.out.println("visit file: " + file);
  51. renameFile(file);
  52. System.out.println("====================================================================");
  53. return FileVisitResult.CONTINUE;
  54. }
  55.  
  56. @Override
  57. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  58. // System.out.println("visit file failed: " + file);
  59. return FileVisitResult.CONTINUE;
  60. }
  61.  
  62. @Override
  63. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  64. // System.out.println("post visit directory: " + dir);
  65. renameDirectory(dir);
  66. return FileVisitResult.CONTINUE;
  67. }
  68.  
  69.  
  70.  
  71. });
  72.  
  73.  
  74.  
  75. }
  76.  
  77. public static void renameFile(Path file) throws IOException {
  78.  
  79.  
  80. boolean isDirectory = Files.isDirectory(file);
  81. boolean isWritable = Files.isWritable(file);
  82. System.out.println("isDirectory-> "+isDirectory);
  83. System.out.println("isWritable-> "+isWritable);
  84.  
  85. Path sourcePath = Paths.get(file.toString());
  86. String origName = file.getFileName().toString();
  87. String newName = origName.replaceAll("POPCORN-", "");
  88. if (isWritable&&!isDirectory) {
  89.  
  90.  
  91.  
  92.  
  93. System.out.println("fname-> "+origName);
  94. /*get the path of the directory*/
  95. String baseLoc = file.getParent().toString();
  96. System.out.println("baseLoc-> "+baseLoc);
  97. if (origName.contains("POPCORN-") /*|| origName.contains("#") || origName.contains("@")*/){
  98.  
  99. System.out.println("Orig name-> "+origName);
  100. /*origName = origName.replaceAll("&", "_and_");
  101. origName = origName.replaceAll("@", "_at_");*/
  102.  
  103.  
  104. System.out.println("New Name-> "+newName);
  105. String newLoc = baseLoc+File.separator+newName;//having "/" hardcoded is not cross-platform.
  106. System.out.println("newLoc-> "+newLoc);
  107. //File newFile = new File(newLoc);
  108.  
  109.  
  110. Path destinationPath = Paths.get(newLoc);
  111.  
  112. Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
  113.  
  114. } else {
  115. System.out.println("No write permission");
  116. }
  117. }else{
  118.  
  119. /*if(origName.contains("POPCORN-") || origName.contains("#") || origName.contains("@")){
  120. Files.copy(sourcePath, sourcePath.resolveSibling(newName),StandardCopyOption.REPLACE_EXISTING);
  121. }*/
  122. }
  123. }
  124.  
  125.  
  126.  
  127. public static void renameDirectory(Path file) throws IOException {
  128.  
  129.  
  130. boolean isDirectory = Files.isDirectory(file);
  131. boolean isWritable = Files.isWritable(file);
  132. System.out.println("isDirectory-> "+isDirectory);
  133. System.out.println("isWritable-> "+isWritable);
  134.  
  135. Path sourcePath = Paths.get(file.toString());
  136. String origName = file.getFileName().toString();
  137. String newName = origName.replaceAll("POPCORN-", "");
  138. if (isWritable&&isDirectory) {
  139.  
  140. if(origName.contains("POPCORN-") /*|| origName.contains("#") || origName.contains("@")*/){
  141. Files.move(sourcePath, sourcePath.resolveSibling(newName),StandardCopyOption.ATOMIC_MOVE);
  142. }
  143.  
  144. } else {
  145. System.out.println("No write permission");
  146. }
  147. }
  148. }
  149.  
  150. Exception in thread "main" java.nio.file.AccessDeniedException: D:rootPOPCORN-folder1 -> D:rootfolder1
  151. at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
  152. at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
  153. at sun.nio.fs.WindowsFileCopy.move(Unknown Source)
  154. at sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
  155. at java.nio.file.Files.move(Unknown Source)
  156. at com.din.pach.FileNio.renameDirectory(FileNio.java:121)
  157. at com.din.pach.FileNio$1.postVisitDirectory(FileNio.java:45)
  158. at com.din.pach.FileNio$1.postVisitDirectory(FileNio.java:1)
  159. at java.nio.file.Files.walkFileTree(Unknown Source)
  160. at java.nio.file.Files.walkFileTree(Unknown Source)
  161. at com.din.pach.FileNio.main(FileNio.java:19)
Add Comment
Please, Sign In to add comment