FileManager类的设计与实现

本文介绍了一个名为FileManager的Java类,该类实现了多种文件操作功能,包括文件复制、移动、删除、压缩、解压、分割和组合。类中包含如listAllFiles、copyFile、copyFiles、splitFile、mergeFile、deleteFile、deleteFiles、moveFile、moveFiles、compressFile和unCompressFile等方法,覆盖了单个文件和多层目录的处理。
摘要由CSDN通过智能技术生成

在学习java之初就一直想着要自己封装一个文件管理的类,可以实现各种文件操作。
但是一直迟迟没有实现,然后我就作为一个“任务”给了小猫,让小猫去学习一些文件操作相关的知识,其实小猫挺认真的,并且也挺聪明,至少她花费的时间要比我花费的时间要短,而完成的内容要比我的好,下面这个是我在不同阶段经过三次修改和实现才完成的。


Java文件操作,共实现了文件复制(单个文件和多层目录文件),文件移动(单个文件和多层目录文件),文件删除(单个文件和多层目录文件),文件压缩(单个文件),文件解压(单个文件),文件分割(将一个大文件分割为若干个小文件),文件组合(将多个文件组合到一个文件中)。

Code:
  1. package ttstudy.io;   
  2. import java.io.*;   
  3. import java.util.*;   
  4. import java.util.zip.*;   
  5.   
  6. public class FileManager {   
  7.     private static ArrayList<File> lsFiles = new ArrayList<File>();   
  8.     private static FileInputStream fis = null;   
  9.     private static FileOutputStream fos = null;   
  10.     /**  
  11.      * list all files  
  12.      * @param path  
  13.      * @return ArrayList<File>  
  14.      * @throws FileNotFoundException  
  15.      */  
  16.     public static ArrayList<File> listAllFiles(String path) throws FileNotFoundException {   
  17.         File file = new File(path);   
  18.         File[] f = file.listFiles();   
  19.         for(int i=0; i<f.length; i++) {   
  20.             lsFiles.add(f[i]);   
  21.             //If the current file is a directory, Recursion listed catalogue of the files   
  22.             if(f[i].isDirectory()) {   
  23.                 listAllFiles(f[i].getAbsolutePath());   
  24.             }   
  25.         }   
  26.         return lsFiles;   
  27.     }   
  28.     /**  
  29.      * copy srcFile to desFile  
  30.      * @param srcFile  
  31.      * @param desFile  
  32.      * @throws FileNotFoundException  
  33.      */  
  34.     public static void copyFile(String srcFile, String desFile) throws FileNotFoundException, IOException {   
  35.         fis = new FileInputStream(new File(srcFile));   
  36.         fos = new FileOutputStream(new File(desFile));   
  37.         byte[] buf = new byte[1024];   
  38.         int len = 0;   
  39.         while((len=fis.read(buf)) != -1) {   
  40.             fos.write(buf, 0, len);   
  41.         }   
  42.         fos.close();   
  43.         fis.close();   
  44.     }   
  45.     /**  
  46.      * copy all files in srcFile to desFile  
  47.      * @param srcFile  
  48.      * @param desFile  
  49.      * @throws FileNotFoundException  
  50.      */  
  51.     public static void copyFiles(String srcFile, String desFile) throws F
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值