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 boolean makeDir(String path) {
028: return new File(path).mkdir();
029: }
030:
031: public static void deleteFileSystemDirectory(String dirPath) {
032: deleteFileSystemDirectory(new File(dirPath));
033: }
034:
035: public static void deleteFileSystemDirectory(File current) {
036: File[] files = current.listFiles();
037:
038: for (int i = 0; files != null && i < files.length; i++) {
039: File file = files[i];
040: if (file.isDirectory())
041: deleteFileSystemDirectory(file);
042: else
043: deleteFile(file);
044: }
045: deleteFile(current);
046: }
047:
048: public static void deleteFile(String filename) {
049: deleteFile(new File(filename));
050: }
051:
052: public static void deleteFile(File file) {
053: if (!file.exists())
054: return;
055: if (!file.delete())
056: throw new RuntimeException("Could not delete '"
057: + file.getAbsoluteFile() + "'");
058: waitUntilFileDeleted(file);
059: }
060:
061: private static void waitUntilFileDeleted(File file) {
062: int i = 10;
063: while (file.exists()) {
064: if (--i <= 0) {
065: System.out.println("Breaking out of delete wait");
066: break;
067: }
068: try {
069: Thread.sleep(500);
070: } catch (InterruptedException e) {
071: }
072: }
073: }
074:
075: public static String getFileContent(String path) throws Exception {
076: File input = new File(path);
077: return getFileContent(input);
078: }
079:
080: public static String getFileContent(File input) throws Exception {
081: return new String(getFileBytes(input));
082: }
083:
084: public static byte[] getFileBytes(File input) throws Exception {
085: long size = input.length();
086: FileInputStream stream = new FileInputStream(input);
087: byte[] bytes = new StreamReader(stream).readBytes((int) size);
088: stream.close();
089: return bytes;
090: }
091:
092: public static LinkedList getFileLines(String filename)
093: throws Exception {
094: return getFileLines(new File(filename));
095: }
096:
097: public static LinkedList getFileLines(File file) throws Exception {
098: LinkedList lines = new LinkedList();
099: BufferedReader reader = new BufferedReader(new FileReader(file));
100: String line;
101: while ((line = reader.readLine()) != null)
102: lines.add(line);
103:
104: reader.close();
105: return lines;
106: }
107:
108: public static void writeLinesToFile(String filename, List lines)
109: throws Exception {
110: writeLinesToFile(new File(filename), lines);
111: }
112:
113: public static void writeLinesToFile(File file, List lines)
114: throws Exception {
115: PrintStream output = new PrintStream(new FileOutputStream(file));
116: for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
117: String line = (String) iterator.next();
118: output.println(line);
119: }
120: output.close();
121: }
122:
123: public static void copyBytes(InputStream input, OutputStream output)
124: throws Exception {
125: StreamReader reader = new StreamReader(input);
126: while (!reader.isEof())
127: output.write(reader.readBytes(1000));
128: }
129:
130: public static File createDir(String path) {
131: makeDir(path);
132: return new File(path);
133: }
134:
135: public static File[] getDirectoryListing(File dir) {
136: SortedSet dirSet = new TreeSet();
137: SortedSet fileSet = new TreeSet();
138: File[] files = dir.listFiles();
139: for (int i = 0; i < files.length; i++) {
140: if (files[i].isDirectory())
141: dirSet.add(files[i]);
142: else
143: fileSet.add(files[i]);
144: }
145: List fileList = new LinkedList();
146: fileList.addAll(dirSet);
147: fileList.addAll(fileSet);
148: return (File[]) fileList.toArray(new File[] {});
149: }
150:
151: public static String buildPath(String[] parts) {
152: return StringUtil.join(Arrays.asList(parts), System
153: .getProperty("file.separator"));
154: }
155: }
|