J技术跟踪

记录在程序开发中使用过的技术心得及常用的技巧;
数据加载中……
文件处理通用类
  1
  2
  3import java.io.BufferedReader;
  4import java.io.File;
  5import java.io.FileInputStream;
  6import java.io.FileOutputStream;
  7import java.io.InputStreamReader;
  8import java.io.OutputStreamWriter;
  9import java.io.Writer;
 10
 11/**
 12 * 文件操作通用类
 13 * 
 14 * @author YangHui
 15 */

 16public class FileHandle {
 17
 18    /**
 19     * 判断指定路径文件或目录是否存在
 20     * 
 21     * @param strPath
 22     *            文件全路径(含文件名)/目录全路径
 23     * @return boolean
 24     */

 25    public static boolean getState(String strPath) {
 26        boolean blnResult = false;
 27        File file = null;
 28        try {
 29            file = new File(strPath);
 30            if (file.exists() || file.isFile()) {
 31                blnResult = true;
 32            }

 33        }

 34        catch (Exception e) {
 35            System.out.println("类:FileHandle,方法:getState,信息:路径有误," + strPath);
 36        }

 37        finally {
 38            file = null;
 39        }

 40        // 释放对象
 41        strPath = null;
 42        return blnResult;
 43    }

 44
 45    /**
 46     * 读取文件到字符串中
 47     * 
 48     * @param strFilePath
 49     *            文件全路径(含文件名)
 50     * @param strCoding
 51     *            编码格式
 52     * @return String
 53     */

 54    public static String fileToString(String strFilePath, String strCoding) {
 55        StringBuffer strBuffResult = new StringBuffer();
 56        int i = 0;
 57        if (strCoding == null || strCoding.trim().length() <= 0{
 58            strCoding = "UTF-8";
 59        }

 60        BufferedReader bufferedReader = null;
 61        try {
 62            if (strCoding == null || strCoding.trim().length() <= 0{
 63                bufferedReader = new BufferedReader(new InputStreamReader(
 64                        new FileInputStream(strFilePath)));
 65            }
 else {
 66                bufferedReader = new BufferedReader(new InputStreamReader(
 67                        new FileInputStream(strFilePath), strCoding));
 68            }

 69            while ((i = bufferedReader.read()) != -1{
 70                strBuffResult.append((char) i);
 71            }

 72            bufferedReader.close();
 73        }

 74        catch (Exception ex) {
 75            System.out.println("类:FileHandle,方法:fileToString,信息:" + ex);
 76        }

 77        finally {
 78            bufferedReader = null;
 79        }

 80        // 释放对象
 81        strCoding = null;
 82        strFilePath = null;
 83        return strBuffResult.toString();
 84    }

 85
 86    /**
 87     * 将字符串写入到文件中
 88     * 
 89     * @param strContent
 90     *            字符串内容
 91     * @param strFilePath
 92     *            文件全路径(含文件名)
 93     * @param strCoding
 94     *            编码格式,默认:UTF-8
 95     * @return boolean
 96     */

 97    public static boolean stringToFile(String strContent, String strFilePath,
 98            String strCoding) {
 99        boolean blnResult = false;
100        if (strCoding == null || strCoding.trim().length() <= 0{
101            strCoding = "UTF-8";
102        }

103        FileOutputStream fileOutputStream = null// 文件输出对象
104        Writer writer = null;
105        try {
106            fileOutputStream = new FileOutputStream(strFilePath);
107            if (strCoding == null || strCoding.trim().length() <= 0{
108                writer = new OutputStreamWriter(fileOutputStream);
109            }
 else {
110                writer = new OutputStreamWriter(fileOutputStream, strCoding);
111            }

112            writer.write(strContent);
113            writer.flush();
114            writer.close();
115            fileOutputStream.close();
116            blnResult = true;
117        }

118        catch (Exception ex) {
119            System.out.println("类:FileHandle;方法:stringToFile;信息:" + ex);
120        }

121        finally {
122            writer = null;
123            fileOutputStream = null;
124        }

125        // 释放对象
126        strCoding = null;
127        strContent = null;
128        strFilePath = null;
129        return blnResult;
130    }

131
132    /**
133     * 新建目录
134     * 
135     * @param strFolderPath
136     *            目录路径(含要创建的目录名称)
137     * @return boolean
138     */

139    public static boolean createFolder(String strFolderPath) {
140        boolean blnResult = true;
141        File file = null;
142        if (strFolderPath != null && strFolderPath.trim().length() > 0{
143            try {
144                file = new File(strFolderPath);
145                if (!file.exists()) {
146                    file.mkdir();
147                }

148            }

149            catch (Exception e) {
150                System.out.println("类:FileHandle,方法:createFolder,信息:创建目录操作出错,"
151                        + strFolderPath);
152                blnResult = false;
153            }

154            finally {
155                file = null;
156            }

157        }

158        // 释放对象
159        strFolderPath = null;
160        return blnResult;
161    }

162
163    /**
164     * 复制整个文件夹的内容
165     * 
166     * @param strOldFolderPath
167     *            准备拷贝的目录
168     * @param strNewFolderPath
169     *            指定绝对路径的新目录
170     * @return void
171     */

172    public static void copyFolder(String strOldFolderPath,
173            String strNewFolderPath) {
174        FileInputStream fileInputStream = null;
175        FileOutputStream fileOutputStream = null;
176        File file = null;
177        String[] strArrayFile = null;
178        File fileTemp = null;
179        byte[] byteArray = null;
180        int intIndex = 0;
181        try {
182            new File(strNewFolderPath).mkdirs(); // 如果文件夹不存在 则建立新文件夹
183            file = new File(strOldFolderPath);
184            strArrayFile = file.list();
185            for (int i = 0; i < strArrayFile.length; i++{
186                if (strOldFolderPath.endsWith(File.separator)) {
187                    fileTemp = new File(strOldFolderPath + strArrayFile[i]);
188                }
 else {
189                    fileTemp = new File(strOldFolderPath + File.separator
190                            + strArrayFile[i]);
191                }

192                if (fileTemp.isFile() && (!fileTemp.isHidden())) {
193                    fileInputStream = new FileInputStream(fileTemp);
194                    fileOutputStream = new FileOutputStream(strNewFolderPath
195                            + "/" + (fileTemp.getName()).toString());
196                    byteArray = new byte[1024 * 5];
197                    while ((intIndex = fileInputStream.read(byteArray)) != -1{
198                        fileOutputStream.write(byteArray, 0, intIndex);
199                    }

200                    fileOutputStream.flush();
201                    fileOutputStream.close();
202                    fileInputStream.close();
203                    intIndex = 0;
204                }

205                if (fileTemp.isDirectory() && (!fileTemp.isHidden())) {// 如果是子文件夹
206                    copyFolder(strOldFolderPath + File.separator
207                            + strArrayFile[i], strNewFolderPath
208                            + File.separator + strArrayFile[i]);
209                }

210            }

211        }

212        catch (Exception e) {
213            System.out.println("类:FileHandle,方法:copyFolder,信息:复制整个文件夹内容操作出错,"
214                    + e);
215        }

216        finally {
217            fileInputStream = null;
218            fileOutputStream = null;
219            file = null;
220            fileTemp = null;
221            byteArray = null;
222        }

223        // 释放对象
224        strArrayFile = null;
225        strNewFolderPath = null;
226        strOldFolderPath = null;
227    }

228
229    /**
230     * 复制单个文件
231     * 
232     * @param strOldFilePath
233     *            准备复制的文件源
234     * @param strNewFilePath
235     *            拷贝到新绝对路径带文件名
236     * @return boolean
237     */

238    public static boolean copyFile(String strOldFilePath, String strNewFilePath) {
239        boolean blnResult = false;
240        FileInputStream fileInputStream = null;
241        FileOutputStream fileOutputStream = null;
242        File file = null;
243        byte[] byteArray = null;
244        int intIndex = 0;
245        try {
246            fileInputStream = new FileInputStream(strOldFilePath);
247            file = new File(strNewFilePath);
248            if (!file.exists()) {
249                file.createNewFile();
250            }

251            fileOutputStream = new FileOutputStream(file);
252            byteArray = new byte[1024];
253            while ((intIndex = fileInputStream.read(byteArray)) != -1{
254                for (int i = 0; i < intIndex; i++)
255                    fileOutputStream.write(byteArray[i]);
256            }

257            intIndex = 0;
258            fileInputStream.close();
259            fileOutputStream.close();
260            blnResult = true;
261        }

262        catch (Exception e) {
263            System.out.println("类:FileHandle,方法:copyFile,信息:被拷贝文件不存在!" + e);
264        }

265        finally {
266            fileInputStream = null;
267            fileOutputStream = null;
268            file = null;
269            byteArray = null;
270        }

271        // 释放对象
272        strNewFilePath = null;
273        strOldFilePath = null;
274        return blnResult;
275    }

276
277    /**
278     * 删除文件
279     * 
280     * @param strFilePath
281     *            文件全路径(含文件名)
282     * @return boolean
283     */

284    public static boolean delFile(String strFilePath) {
285        boolean blnResult = false;
286        File file = null;
287        if (strFilePath != null && strFilePath.trim().length() > 0{
288            try {
289                file = new File(strFilePath);
290                if (file.exists()) {
291                    file.delete();
292                    blnResult = true;
293                }
 else {
294                    System.out.println("类:FileHandle,方法:delFile,信息:被文件不存在,"
295                            + strFilePath);
296                }

297            }

298            catch (Exception e) {
299                System.out.println("类:FileHandle,方法:delFile,信息:删除文件有误," + e);
300            }

301            finally {
302                file = null;
303            }

304        }
 else {
305            System.out.println("类:FileHandle,方法:delFile,"
306                    + "信息:strFilePath = null.");
307        }

308        // 释放对象
309        strFilePath = null;
310        return blnResult;
311    }

312
313    /**
314     * 删除文件夹
315     * 
316     * @param strFolderPath
317     *            文件夹完整绝对路径
318     * @return void
319     */

320    public static void delFolder(String strFolderPath) {
321        File file = null;
322        if (strFolderPath != null && strFolderPath.trim().length() > 0{
323            try {
324                delAllFile(strFolderPath); // 删除完里面所有内容
325                file = new File(strFolderPath);
326                file.delete(); // 删除空文件夹
327            }

328            catch (Exception e) {
329                System.out.println("类:FileHandle,方法:delFolder,信息:删除目录有误," + e);
330            }

331            finally {
332                file = null;
333            }

334        }
 else {
335            System.out.println("类:FileHandle,方法:delFolder,"
336                    + "信息:strFolderPath=null");
337        }

338        // 释放对象
339        strFolderPath = null;
340    }

341
342    /**
343     * 删除指定文件夹下所有文件及目录
344     * 
345     * @param strFolderPath
346     *            文件夹完整绝对路径
347     * @return boolean
348     */

349    public static boolean delAllFile(String strFolderPath) {
350        boolean blnResult = false;
351        int intFileCount = 0;
352        String[] strArrayFile = null;
353        File file = new File(strFolderPath);
354        if (file.exists() && file.isDirectory()) {
355            strArrayFile = file.list();
356            if (strArrayFile == null || strArrayFile.length <= 0{
357                blnResult = true;
358            }
 else {
359                intFileCount = strArrayFile.length;
360                if (intFileCount > 0{
361                    for (int i = 0; i < intFileCount; i++{
362                        if (strFolderPath.endsWith(File.separator)) {
363                            file = new File(strFolderPath + strArrayFile[i]);
364                        }
 else {
365                            file = new File(strFolderPath + File.separator
366                                    + strArrayFile[i]);
367                        }

368                        if (file.isFile()) {
369                            file.delete();
370                        }

371                        if (file.isDirectory()) {
372                            delAllFile(strFolderPath + File.separator
373                                    + strArrayFile[i]);// 先删除文件夹里面的文件
374                            delFolder(strFolderPath + File.separator
375                                    + strArrayFile[i]);// 再删除空文件夹
376                        }

377                        blnResult = true;
378                    }

379                }
 else {
380                    blnResult = true;
381                }

382            }

383        }
 else {
384            System.out.println("类:FileHandle,方法:delAllFile,信息:删除文件目录有误,"
385                    + strFolderPath);
386        }

387        // 释放对象
388        strFolderPath = null;
389        strArrayFile = null;
390        file = null;
391        return blnResult;
392    }

393
394    /**
395     * 获得一个目录下面所有文件
396     * 
397     * @param strFolderPath
398     *            文件夹完整绝对路径
399     * @return String[]
400     */

401    public static String[] getAllFile(String strFolderPath) {
402        String[] strArrayResult = null;
403        File file = null;
404        if (strFolderPath != null && strFolderPath.trim().length() > 0{
405            file = new File(strFolderPath);
406            if (getState(strFolderPath) && file.isDirectory()) {
407                strArrayResult = file.list();
408            }

409            if (strArrayResult == null || strArrayResult.length <= 0
410                    || strArrayResult[0].trim().length() <= 0{
411                strArrayResult = null;
412            }

413        }
 else {
414            System.out.println("类:FileHandle,方法:getAllFile,"
415                    + "信息:strFolderPath=null");
416        }

417        // 释放对象
418        file = null;
419        strFolderPath = null;
420        return strArrayResult;
421    }

422
423    /**
424     * 获得指定目录下所有的一级目录
425     * 
426     * @param strFolderPath
427     *            指定路径名
428     * @return File[]
429     */

430    public static File[] getAllFolder(String strFolderPath) {
431        File file = null;
432        File[] fileArray = null;
433        if (strFolderPath != null && strFolderPath.trim().length() > 0{
434            file = new File(strFolderPath);
435            fileArray = file.listFiles();
436            if (fileArray == null || fileArray.length <= 0{
437                fileArray = null;
438            }

439        }
 else {
440            System.out.println("类:FileHandle,方法:getAllFolder,"
441                    + "信息:strFolderPath=null");
442        }

443        // 释放对象
444        file = null;
445        strFolderPath = null;
446        return fileArray;
447    }

448
449}

450

posted on 2009-11-17 10:34 J_sp-404 阅读(230) 评论(0)  编辑  收藏 所属分类: Java技术


只有注册用户登录后才能发表评论。


网站导航: