/src/org/ooc/utils/FileUtils.java

http://github.com/nddrylliog/ooc · Java · 162 lines · 105 code · 21 blank · 36 comment · 19 complexity · fe57d2b16777d23f8c566b50a4360760 MD5 · raw file

  1. package org.ooc.utils;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.nio.channels.FileChannel;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.StringTokenizer;
  13. /**
  14. * A collection of file utilities that should have been in the
  15. * Java SDK.
  16. *
  17. * @author Amos Wenger
  18. */
  19. public class FileUtils {
  20. /**
  21. * Recursively deletes a file/folder and all its subcontent.
  22. * In other words, "rm -rf"
  23. * @param file
  24. */
  25. public static void deleteRecursive(File file) {
  26. List<File> exploreQueue = new ArrayList<File>();
  27. List<File> exploreQueue2 = new ArrayList<File>();
  28. List<File> deleteQueue = new ArrayList<File>();
  29. exploreQueue.add(file);
  30. while(!exploreQueue.isEmpty()) {
  31. deleteQueue.addAll(0, exploreQueue);
  32. for(File toExplore: exploreQueue) {
  33. if(toExplore.isDirectory()) {
  34. for(File child: toExplore.listFiles()) {
  35. exploreQueue2.add(0, child);
  36. }
  37. }
  38. }
  39. exploreQueue.clear();
  40. exploreQueue.addAll(exploreQueue2);
  41. exploreQueue2.clear();
  42. }
  43. for(File toDelete: deleteQueue) {
  44. toDelete.delete();
  45. }
  46. }
  47. /**
  48. * Copy a source file to a specified destination path.
  49. * @param sourceFile the source
  50. * @param destFile the destination (will be created if doesn't exist, will
  51. * be overwritten if it exists)
  52. * @throws IOException we never know...
  53. */
  54. public static void copy(File sourceFile, File destFile) throws IOException {
  55. if(!destFile.exists()) {
  56. destFile.createNewFile();
  57. }
  58. FileChannel source = null;
  59. FileChannel destination = null;
  60. try {
  61. source = new FileInputStream(sourceFile).getChannel();
  62. destination = new FileOutputStream(destFile).getChannel();
  63. destination.transferFrom(source, 0, source.size());
  64. }
  65. finally {
  66. if(source != null) {
  67. source.close();
  68. }
  69. if(destination != null) {
  70. destination.close();
  71. }
  72. }
  73. }
  74. /**
  75. * Read a file contents and return it as a string
  76. * @param file
  77. * @return
  78. * @throws IOException
  79. */
  80. public static String read(File file) throws IOException {
  81. StringBuilder builder = new StringBuilder((int) file.length());
  82. FileReader reader = new FileReader(file);
  83. int n;
  84. char[] cbuf = new char[4096];
  85. while((n = reader.read(cbuf)) != -1) {
  86. builder.append(cbuf, 0, n);
  87. }
  88. reader.close();
  89. return builder.toString();
  90. }
  91. /**
  92. * Write a string to a file
  93. * @param file
  94. * @param data
  95. * @throws IOException
  96. */
  97. public static void write(File file, String data) throws IOException {
  98. file.getParentFile().mkdirs();
  99. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  100. writer.write(data);
  101. writer.close();
  102. }
  103. /**
  104. * Resolve redundancies, ie. ".." and "."
  105. * @param file
  106. * @return
  107. */
  108. public static File resolveRedundancies(File file) {
  109. String path = file.getPath();
  110. List<String> elems = new ArrayList<String>();
  111. StringTokenizer st = new StringTokenizer(path, File.separator);
  112. boolean startsWithSeparator = path.startsWith(File.separator);
  113. while(st.hasMoreTokens()) {
  114. String elem = st.nextToken();
  115. if(elem.equals("..")) {
  116. if(!elems.isEmpty()) {
  117. elems.remove(elems.size() - 1);
  118. } else {
  119. elems.add(elem);
  120. }
  121. } else if(elem.equals(".")) {
  122. // do nothing
  123. } else {
  124. elems.add(elem);
  125. }
  126. }
  127. StringBuffer buffer = new StringBuffer(path.length());
  128. if(startsWithSeparator) {
  129. buffer.append(File.separator);
  130. }
  131. int size = elems.size();
  132. int count = 0;
  133. for(String elem: elems) {
  134. buffer.append(elem);
  135. if(++count < size) {
  136. buffer.append(File.separator);
  137. }
  138. }
  139. return new File(buffer.toString());
  140. }
  141. }