001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.util;
004:
005: import java.io.*;
006: import java.util.*;
007:
008: public class FileUtil {
009: public static final String ENDL = System
010: .getProperty("line.separator");
011:
012: public static File createFile(String path, String content) {
013: return createFile(new File(path), content);
014: }
015:
016: public static File createFile(File file, String content) {
017: try {
018: FileOutputStream fileOutput = new FileOutputStream(file);
019: fileOutput.write(content.getBytes());
020: fileOutput.close();
021: } catch (IOException e) {
022: e.printStackTrace();
023: }
024: return file;
025: }
026:
027: public static File createFile2(String path, String content)
028: throws Exception {
029: File file = new File(path);
030: FileOutputStream fileOutput = new FileOutputStream(file);
031: fileOutput.write(content.getBytes());
032: fileOutput.close();
033: return file;
034: }
035:
036: public static boolean makeDir(String path) {
037: return new File(path).mkdirs();
038: }
039:
040: public static void deleteFileSystemDirectory(String dirPath) {
041: deleteFileSystemDirectory(new File(dirPath));
042: }
043:
044: public static void deleteFileSystemDirectory(File current) {
045: File[] files = current.listFiles();
046:
047: for (int i = 0; files != null && i < files.length; i++) {
048: File file = files[i];
049: if (file.isDirectory())
050: deleteFileSystemDirectory(file);
051: else
052: deleteFile(file);
053: }
054: deleteFile(current);
055: }
056:
057: public static void deleteFile(String filename) {
058: deleteFile(new File(filename));
059: }
060:
061: public static void deleteFile(File file) {
062: if (!file.exists())
063: return;
064: if (!file.delete())
065: throw new RuntimeException("Could not delete '"
066: + file.getAbsoluteFile() + "'");
067: waitUntilFileDeleted(file);
068: }
069:
070: private static void waitUntilFileDeleted(File file) {
071: int i = 10;
072: while (file.exists()) {
073: if (--i <= 0) {
074: System.out.println("Breaking out of delete wait");
075: break;
076: }
077: try {
078: Thread.sleep(500);
079: } catch (InterruptedException e) {
080: }
081: }
082: }
083:
084: public static String getFileContent(String path) throws Exception {
085: File input = new File(path);
086: return getFileContent(input);
087: }
088:
089: public static String getFileContent(File input) throws Exception {
090: return new String(getFileBytes(input));
091: }
092:
093: public static byte[] getFileBytes(File input) throws Exception {
094: long size = input.length();
095: FileInputStream stream = new FileInputStream(input);
096: byte[] bytes = new StreamReader(stream).readBytes((int) size);
097: stream.close();
098: return bytes;
099: }
100:
101: public static LinkedList getFileLines(String filename)
102: throws Exception {
103: return getFileLines(new File(filename));
104: }
105:
106: public static LinkedList getFileLines(File file) throws Exception {
107: LinkedList lines = new LinkedList();
108: BufferedReader reader = new BufferedReader(new FileReader(file));
109: String line;
110: while ((line = reader.readLine()) != null)
111: lines.add(line);
112:
113: reader.close();
114: return lines;
115: }
116:
117: public static void writeLinesToFile(String filename, List lines)
118: throws Exception {
119: writeLinesToFile(new File(filename), lines);
120: }
121:
122: public static void writeLinesToFile(File file, List lines)
123: throws Exception {
124: PrintStream output = new PrintStream(new FileOutputStream(file));
125: for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
126: String line = (String) iterator.next();
127: output.println(line);
128: }
129: output.close();
130: }
131:
132: public static void copyBytes(InputStream input, OutputStream output)
133: throws Exception {
134: StreamReader reader = new StreamReader(input);
135: while (!reader.isEof())
136: output.write(reader.readBytes(1000));
137: }
138:
139: public static File createDir(String path) {
140: makeDir(path);
141: return new File(path);
142: }
143:
144: public static File[] getDirectoryListing(File dir) {
145: SortedSet dirSet = new TreeSet();
146: SortedSet fileSet = new TreeSet();
147: File[] files = dir.listFiles();
148: for (int i = 0; i < files.length; i++) {
149: if (files[i].isDirectory())
150: dirSet.add(files[i]);
151: else
152: fileSet.add(files[i]);
153: }
154: List fileList = new LinkedList();
155: fileList.addAll(dirSet);
156: fileList.addAll(fileSet);
157: return (File[]) fileList.toArray(new File[] {});
158: }
159:
160: public static String buildPath(String[] parts) {
161: return StringUtil.join(Arrays.asList(parts), System
162: .getProperty("file.separator"));
163: }
164: }
|