KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > io > v1 > FileUtils


1 /*
2  * FileUtils.java - 0.9.0 12/13/2000 - 15:41:52
3  *
4  * Copyright (C) 2000,,2003 2002 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26  
27  
28 package net.sourceforge.groboutils.util.io.v1;
29  
30 import java.util.Hashtable JavaDoc;
31 import java.util.Vector JavaDoc;
32 import java.util.Stack JavaDoc;
33
34 import java.io.IOException JavaDoc;
35 import java.io.FileNotFoundException JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.FilenameFilter JavaDoc;
38 import java.io.BufferedOutputStream JavaDoc;
39 import java.io.BufferedInputStream JavaDoc;
40 import java.io.FileOutputStream JavaDoc;
41 import java.io.FileInputStream JavaDoc;
42
43
44 /**
45  * Simple and common file operations in a Utility class.
46  *
47  *
48  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
49  * @since December 13, 2000
50  * @version $Date: 2003/05/19 20:31:47 $
51  */

52 public class FileUtils
53 {
54     //---------------------------------------------------------------------
55
// Public Static Fields
56

57      
58     //---------------------------------------------------------------------
59
// Protected Static Fields
60

61     protected static FileUtils s_instance = new FileUtils();
62      
63     //---------------------------------------------------------------------
64
// Private Static Fields
65

66      
67     //---------------------------------------------------------------------
68
// Public Fields
69

70      
71     //---------------------------------------------------------------------
72
// Protected Fields
73

74      
75     //---------------------------------------------------------------------
76
// Private Fields
77

78      
79      
80      
81     //---------------------------------------------------------------------
82
// Constructors
83

84      
85     /**
86      * Default Constructor
87      */

88     protected FileUtils()
89     {
90         // do nothing
91
}
92      
93      
94      
95      
96     //---------------------------------------------------------------------
97
// Public Static Methods
98

99     public static FileUtils getInstance()
100     {
101         return s_instance;
102     }
103      
104      
105      
106     //---------------------------------------------------------------------
107
// Public Methods
108

109      
110     /**
111      * @param source the source file to copy
112      * @param dir the target directory to copy the sourcefile into: the
113      * name will remain the same.
114      */

115     public void copyFileToDirectory( File JavaDoc source, File JavaDoc dir )
116             throws IOException JavaDoc
117     {
118         copyFileToFile( source, new File JavaDoc( dir, source.getName() ) );
119     }
120     
121     
122     /**
123      * @param source the source file to copy from.
124      * @param target the target file to copy to.
125      */

126     public void copyFileToFile( File JavaDoc source, File JavaDoc target )
127             throws IOException JavaDoc
128     {
129         FileInputStream JavaDoc fis = null;
130         FileOutputStream JavaDoc fos = null;
131         BufferedInputStream JavaDoc bis = null;
132         BufferedOutputStream JavaDoc bos = null;
133
134         try
135         {
136             fis = new FileInputStream JavaDoc( source );
137             fos = new FileOutputStream JavaDoc( target );
138             bis = new BufferedInputStream JavaDoc( fis );
139             bos = new BufferedOutputStream JavaDoc( fos );
140             
141             byte buffer[] = new byte[ 4096 ];
142             int size = bis.read( buffer, 0, 4096 );
143             while (size > 0)
144             {
145                 bos.write( buffer, 0, size );
146                 size = bis.read( buffer, 0, 4096 );
147             }
148         }
149         finally
150         {
151             if (bis != null) bis.close();
152             if (bos != null) bos.close();
153             if (fis != null) fis.close();
154             if (fos != null) fos.close();
155         }
156     }
157     
158     
159     /**
160      * Recursively descends into the base directory, looking for all files
161      * that pass the filter.
162      *
163      * @param baseDir the base directory to start the search.
164      * @param filter the filename filter to search. If <tt>null</tt>, then
165      * all files are allowed.
166      */

167     public File JavaDoc[] findFileRecurse( File JavaDoc baseDir, FilenameFilter JavaDoc filter )
168 // throws IOException
169
{
170         if (baseDir == null)
171         {
172             throw new IllegalArgumentException JavaDoc( "no null arguments" );
173         }
174         if (filter == null)
175         {
176             filter = new AllFilenameFilter();
177         }
178         
179         Stack JavaDoc dirStack = new Stack JavaDoc();
180         Vector JavaDoc files = new Vector JavaDoc();
181         
182         dirStack.push( baseDir );
183         
184         while (!dirStack.empty())
185         {
186             baseDir = (File JavaDoc)dirStack.pop();
187             String JavaDoc[] list = baseDir.list( filter );
188             for (int i = list.length; --i >= 0;)
189             {
190                 File JavaDoc f = new File JavaDoc( baseDir, list[i] );
191                 if (f.isDirectory())
192                 {
193                     dirStack.push( f );
194                 }
195                 else
196                 if (f.isFile())
197                 {
198                     files.addElement( f );
199                 }
200             }
201         }
202         
203         File JavaDoc[] flist = new File JavaDoc[ files.size() ];
204         files.copyInto( flist );
205         return flist;
206     }
207      
208      
209     //---------------------------------------------------------------------
210
// Protected Methods
211

212      
213     //---------------------------------------------------------------------
214
// Private Methods
215

216      
217  }
218  
219
Popular Tags