PageRenderTime 275ms CodeModel.GetById 34ms RepoModel.GetById 9ms app.codeStats 0ms

/src/util/FileUtil.java

https://github.com/sfyrat/fitnesse
Java | 222 lines | 191 code | 29 blank | 2 comment | 28 complexity | 481754c678fee28ce93ee71cd44d7373 MD5 | raw file
  1. // Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
  2. // Released under the terms of the CPL Common Public License version 1.0.
  3. package util;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.net.URL;
  7. import java.net.URLClassLoader;
  8. import java.lang.reflect.Method;
  9. public class FileUtil {
  10. public static final String ENDL = System.getProperty("line.separator");
  11. public static File createFile(String path, String content) {
  12. String names[] = path.split("/");
  13. if (names.length == 1)
  14. return createFile(new File(path), content);
  15. else {
  16. File parent = null;
  17. for (int i = 0; i < names.length - 1; i++) {
  18. parent = parent == null ? new File(names[i]) : new File(parent, names[i]);
  19. if (!parent.exists())
  20. parent.mkdir();
  21. }
  22. File fileToCreate = new File(parent, names[names.length - 1]);
  23. return createFile(fileToCreate, content);
  24. }
  25. }
  26. public static File createFile(File file, String content) {
  27. FileOutputStream fileOutput = null;
  28. try {
  29. fileOutput = new FileOutputStream(file);
  30. fileOutput.write(content.getBytes());
  31. }
  32. catch (IOException e) {
  33. throw new RuntimeException(e);
  34. }
  35. finally {
  36. if (fileOutput != null)
  37. try {
  38. fileOutput.close();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. return file;
  44. }
  45. public static boolean makeDir(String path) {
  46. return new File(path).mkdir();
  47. }
  48. public static void deleteFileSystemDirectory(String dirPath) {
  49. deleteFileSystemDirectory(new File(dirPath));
  50. }
  51. public static void deleteFileSystemDirectory(File current) {
  52. File[] files = current.listFiles();
  53. for (int i = 0; files != null && i < files.length; i++) {
  54. File file = files[i];
  55. if (file.isDirectory())
  56. deleteFileSystemDirectory(file);
  57. else
  58. deleteFile(file);
  59. }
  60. deleteFile(current);
  61. }
  62. public static void deleteFile(String filename) {
  63. deleteFile(new File(filename));
  64. }
  65. public static void deleteFile(File file) {
  66. if (!file.exists())
  67. return;
  68. for (int i = 0; i < 10; i++) {
  69. if (file.delete()) {
  70. waitUntilFileDeleted(file);
  71. return;
  72. }
  73. waitFor(10);
  74. }
  75. throw new RuntimeException("Could not delete '" + file.getAbsoluteFile() + "'");
  76. }
  77. private static void waitUntilFileDeleted(File file) {
  78. int i = 10;
  79. while (file.exists()) {
  80. if (--i <= 0) {
  81. System.out.println("Breaking out of delete wait");
  82. break;
  83. }
  84. waitFor(500);
  85. }
  86. }
  87. private static void waitFor(int milliseconds) {
  88. try {
  89. Thread.sleep(milliseconds);
  90. }
  91. catch (InterruptedException e) {
  92. }
  93. }
  94. public static String getFileContent(String path) throws Exception {
  95. File input = new File(path);
  96. return getFileContent(input);
  97. }
  98. public static String getFileContent(File input) throws Exception {
  99. return new String(getFileBytes(input));
  100. }
  101. public static byte[] getFileBytes(File input) throws Exception {
  102. long size = input.length();
  103. FileInputStream stream = null;
  104. try {
  105. stream = new FileInputStream(input);
  106. byte[] bytes = new StreamReader(stream).readBytes((int) size);
  107. return bytes;
  108. } finally {
  109. if (stream != null)
  110. stream.close();
  111. }
  112. }
  113. public static LinkedList<String> getFileLines(String filename) throws Exception {
  114. return getFileLines(new File(filename));
  115. }
  116. public static LinkedList<String> getFileLines(File file) throws Exception {
  117. LinkedList<String> lines = new LinkedList<String>();
  118. BufferedReader reader = new BufferedReader(new FileReader(file));
  119. String line;
  120. while ((line = reader.readLine()) != null)
  121. lines.add(line);
  122. reader.close();
  123. return lines;
  124. }
  125. public static void writeLinesToFile(String filename, List<?> lines) throws Exception {
  126. writeLinesToFile(new File(filename), lines);
  127. }
  128. public static void writeLinesToFile(File file, List<?> lines) throws Exception {
  129. PrintStream output = new PrintStream(new FileOutputStream(file));
  130. for (Iterator<?> iterator = lines.iterator(); iterator.hasNext();) {
  131. String line = (String) iterator.next();
  132. output.println(line);
  133. }
  134. output.close();
  135. }
  136. public static void copyBytes(InputStream input, OutputStream output) throws Exception {
  137. StreamReader reader = new StreamReader(input);
  138. while (!reader.isEof())
  139. output.write(reader.readBytes(1000));
  140. }
  141. public static File createDir(String path) {
  142. makeDir(path);
  143. return new File(path);
  144. }
  145. public static File[] getDirectoryListing(File dir) {
  146. SortedSet<File> dirSet = new TreeSet<File>();
  147. SortedSet<File> fileSet = new TreeSet<File>();
  148. File[] files = dir.listFiles();
  149. if (files == null)
  150. return new File[0];
  151. for (int i = 0; i < files.length; i++) {
  152. if (files[i].isDirectory())
  153. dirSet.add(files[i]);
  154. else
  155. fileSet.add(files[i]);
  156. }
  157. List<File> fileList = new LinkedList<File>();
  158. fileList.addAll(dirSet);
  159. fileList.addAll(fileSet);
  160. return fileList.toArray(new File[]{});
  161. }
  162. public static String buildPath(String[] parts) {
  163. return StringUtil.join(Arrays.asList(parts), System.getProperty("file.separator"));
  164. }
  165. public static List<String> breakFilenameIntoParts(String fileName) {
  166. List<String> parts = new ArrayList<String>(Arrays.asList(fileName.split("/")));
  167. return parts;
  168. }
  169. public static String getPathOfFile(String fileName) {
  170. List<String> parts = breakFilenameIntoParts(fileName);
  171. parts.remove(parts.size()-1);
  172. return buildPath(parts.toArray(new String[parts.size()]));
  173. }
  174. public static void addItemsToClasspath(String classpathItems) throws Exception {
  175. final String separator = System.getProperty("path.separator");
  176. String currentClassPath = System.getProperty("java.class.path");
  177. System.setProperty("java.class.path", currentClassPath + separator + classpathItems);
  178. String[] items = classpathItems.split(separator);
  179. for (String item : items) {
  180. addFileToClassPath(item);
  181. }
  182. }
  183. private static void addFileToClassPath(String fileName) throws Exception {
  184. addUrlToClasspath(new File(fileName).toURI().toURL());
  185. }
  186. public static void addUrlToClasspath(URL u) throws Exception {
  187. URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
  188. Class<URLClassLoader> sysclass = URLClassLoader.class;
  189. Method method = sysclass.getDeclaredMethod("addURL", new Class[]{URL.class});
  190. method.setAccessible(true);
  191. method.invoke(sysloader, new Object[]{u});
  192. }
  193. }