001: /*
002: * FileUtils.java - 0.9.0 12/13/2000 - 15:41:52
003: *
004: * Copyright (C) 2000,,2003 2002 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.io.v1;
028:
029: import java.util.Hashtable;
030: import java.util.Vector;
031: import java.util.Stack;
032:
033: import java.io.IOException;
034: import java.io.FileNotFoundException;
035: import java.io.File;
036: import java.io.FilenameFilter;
037: import java.io.BufferedOutputStream;
038: import java.io.BufferedInputStream;
039: import java.io.FileOutputStream;
040: import java.io.FileInputStream;
041:
042: /**
043: * Simple and common file operations in a Utility class.
044: *
045: *
046: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
047: * @since December 13, 2000
048: * @version $Date: 2003/05/19 20:31:47 $
049: */
050: public class FileUtils {
051: //---------------------------------------------------------------------
052: // Public Static Fields
053:
054: //---------------------------------------------------------------------
055: // Protected Static Fields
056:
057: protected static FileUtils s_instance = new FileUtils();
058:
059: //---------------------------------------------------------------------
060: // Private Static Fields
061:
062: //---------------------------------------------------------------------
063: // Public Fields
064:
065: //---------------------------------------------------------------------
066: // Protected Fields
067:
068: //---------------------------------------------------------------------
069: // Private Fields
070:
071: //---------------------------------------------------------------------
072: // Constructors
073:
074: /**
075: * Default Constructor
076: */
077: protected FileUtils() {
078: // do nothing
079: }
080:
081: //---------------------------------------------------------------------
082: // Public Static Methods
083:
084: public static FileUtils getInstance() {
085: return s_instance;
086: }
087:
088: //---------------------------------------------------------------------
089: // Public Methods
090:
091: /**
092: * @param source the source file to copy
093: * @param dir the target directory to copy the sourcefile into: the
094: * name will remain the same.
095: */
096: public void copyFileToDirectory(File source, File dir)
097: throws IOException {
098: copyFileToFile(source, new File(dir, source.getName()));
099: }
100:
101: /**
102: * @param source the source file to copy from.
103: * @param target the target file to copy to.
104: */
105: public void copyFileToFile(File source, File target)
106: throws IOException {
107: FileInputStream fis = null;
108: FileOutputStream fos = null;
109: BufferedInputStream bis = null;
110: BufferedOutputStream bos = null;
111:
112: try {
113: fis = new FileInputStream(source);
114: fos = new FileOutputStream(target);
115: bis = new BufferedInputStream(fis);
116: bos = new BufferedOutputStream(fos);
117:
118: byte buffer[] = new byte[4096];
119: int size = bis.read(buffer, 0, 4096);
120: while (size > 0) {
121: bos.write(buffer, 0, size);
122: size = bis.read(buffer, 0, 4096);
123: }
124: } finally {
125: if (bis != null)
126: bis.close();
127: if (bos != null)
128: bos.close();
129: if (fis != null)
130: fis.close();
131: if (fos != null)
132: fos.close();
133: }
134: }
135:
136: /**
137: * Recursively descends into the base directory, looking for all files
138: * that pass the filter.
139: *
140: * @param baseDir the base directory to start the search.
141: * @param filter the filename filter to search. If <tt>null</tt>, then
142: * all files are allowed.
143: */
144: public File[] findFileRecurse(File baseDir, FilenameFilter filter)
145: // throws IOException
146: {
147: if (baseDir == null) {
148: throw new IllegalArgumentException("no null arguments");
149: }
150: if (filter == null) {
151: filter = new AllFilenameFilter();
152: }
153:
154: Stack dirStack = new Stack();
155: Vector files = new Vector();
156:
157: dirStack.push(baseDir);
158:
159: while (!dirStack.empty()) {
160: baseDir = (File) dirStack.pop();
161: String[] list = baseDir.list(filter);
162: for (int i = list.length; --i >= 0;) {
163: File f = new File(baseDir, list[i]);
164: if (f.isDirectory()) {
165: dirStack.push(f);
166: } else if (f.isFile()) {
167: files.addElement(f);
168: }
169: }
170: }
171:
172: File[] flist = new File[files.size()];
173: files.copyInto(flist);
174: return flist;
175: }
176:
177: //---------------------------------------------------------------------
178: // Protected Methods
179:
180: //---------------------------------------------------------------------
181: // Private Methods
182:
183: }
|