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

package ttstudy.io;   
import java.io.*;   
import java.util.*;   
import java.util.zip.*;   
  
public class FileManager {   
    private static ArrayList lsFiles = new ArrayList();   
    private static FileInputStream fis = null;   
    private static FileOutputStream fos = null;   
    /**  
     * list all files  
     * @param path  
     * @return ArrayList  
     * @throws FileNotFoundException  
     */  
    public static ArrayList listAllFiles(String path) throws FileNotFoundException {   
        File file = new File(path);   
        File[] f = file.listFiles();   
        for(int i=0; i lsFiles = listAllFiles(srcFile);   
        for(int i=0; i 1024) {   
            perLen = 512;   
        } else {   
            perLen = 2;   
        }   
        byte[] buf = new byte[perLen];   
        fis = new FileInputStream(f);   
        for(int i=0; i dirs = new LinkedList();    
        dirs.add(new File(srcFile));   
        while(dirs.size() > 0){   
            File currentDir = (File)dirs.getFirst();   
            File[] files = currentDir.listFiles();   
            boolean emptyDir = true;   
            for(int i = 0 ;i < files.length;i++) {   
                if (files[i].isFile()) {   
                    files[i].delete();   
                } else {   
                    dirs.addFirst(files[i]);   
                    emptyDir = false;   
                }   
            }   
            if (emptyDir){   
                currentDir.delete();   
                dirs.removeFirst();   
            }   
        }   
    }   
    /**  
     * move srcFile to desFile  
     * @param srcFile  
     * @param desFile  
     * @throws FileNotFoundException  
     */  
    public static void moveFile(String srcFile, String desFile) throws FileNotFoundException, IOException {   
        copyFile(srcFile, desFile);   
        new File(srcFile).delete();   
    }   
    /**  
     * move srcFile to desFile  
     * @param srcFile  
     * @param desFile  
     * @throws FileNotFoundException  
     */  
    public static void moveFiles(String srcFile, String desFile) throws FileNotFoundException, IOException {   
        copyFiles(srcFile, desFile);   
        deleteFiles(srcFile);   
    }   
    /**  
     * compress files  
     * @param srcFile  
     * @param rarFile  
     * @throws FileNotFoundException  
     * @throws IOException  
     */  
    public static void compressFile(String _unZipFile, String _zipFile) throws FileNotFoundException, IOException {   
        File srcFile = new File(_unZipFile);   
        File zipFile = new File(_zipFile);   
        DataInputStream dis = new DataInputStream(new FileInputStream(srcFile));   
        if(!zipFile.exists()) {   
            File zipdir = new File(zipFile.getParent());   
            if(!zipdir.exists()) {   
                zipdir.mkdirs();   
            }   
            zipFile.createNewFile();   
        }   
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));   
        zos.setMethod(ZipOutputStream.DEFLATED);   
        ZipEntry ze = new ZipEntry(srcFile.getName());   
        zos.putNextEntry(ze);   
        DataOutputStream dos = new DataOutputStream(zos);   
           
        byte[] buf = new byte[2048];   
        int len=0;   
        while((len=dis.read(buf)) != -1) {   
            dos.write(buf, 0, len);   
        }   
        dos.close();   
        dis.close();   
    }   
    /**  
     * uncompress files  
     * @param rarFile  
     * @param srcFile  
     * @throws FileNotFoundException  
     * @throws IOException  
     */  
    @SuppressWarnings("unchecked")   
    public static void unCompressFile(String _zipFile, String _unZipDir) throws FileNotFoundException, IOException {   
        File unZipFile = new File("_unZipFile");   
        if(! unZipFile.exists()) {   
            unZipFile.mkdirs();   
        }   
        ZipEntry ze = null;   
        ZipFile zf = new ZipFile(new File(_zipFile));   
        Enumeration en = (Enumeration)zf.entries();   
        if(en.hasMoreElements()) {   
            ze = (ZipEntry)en.nextElement();   
        }   
        unZipFile = new File(_unZipDir+File.separator+ze.getName());   
        if(! unZipFile.exists()) {   
            unZipFile.createNewFile();   
        }   
        DataInputStream dis = new DataInputStream(zf.getInputStream(ze));   
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(unZipFile));   
        int len = 0;   
        byte[] buf = new byte[2048];   
        while((len=dis.read(buf)) != -1) {   
            dos.write(buf, 0, len);   
        }   
        dos.close();   
        dis.close();   
    }   
    /**  
     *   
     * @param args  
     */  
    public static void main(String[] args) throws FileNotFoundException, IOException {   
        //          ArrayList lsFiles = listAllFiles("D:/temp");   
        //          for(int i=0; i 
  

转载自http://blog.csdn.net/tsyj810883979/article/details/6522065