Java文件操作類封裝

jopen 11年前發布 | 42K 次閱讀 Java Java開發

java文件操作類,簡單封裝,封裝了復制,剪切,刪除目錄,壓縮解壓zip,等常用功能。

package com.wiker;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


/**
 * @author Wiker Yong Email:<a href="mailto:wikeryong@gmail.com">wikeryong@gmail.com</a>
 * @date 2013-11-8 下午6:21:45
 * @version 1.0-SNAPSHOT
 */
@SuppressWarnings("resource")
public class FileUtils {

    /**
     * 獲取文件MD5值
     * 
     * @param file
     * @return
     */
    public static String getMd5ByFile(File file) {
        String value = null;
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0,
                    file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            BigInteger bi = new BigInteger(1, md5.digest());
            value = bi.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }

    /**
     * 獲取文件大小
     * 
     * @param file
     * @return
     */
    public static long getFileLength(File file)
            throws IOException {
        FileInputStream fis = null;
        fis = new FileInputStream(file);
        return fis.available();
    }

    /**
     * 讀取文件到二進制
     * 
     * @author WikerYong Email:<a href="#">yw_312@foxmail.com</a>
     * @version 2012-3-23 上午11:47:06
     * @param file
     * @return
     * @throws IOException
     */
    public static byte[] getBytesFromFile(File file)
            throws IOException {
        InputStream is = new FileInputStream(file);

        long length = file.length();

        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        byte[] bytes = new byte[(int) length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
                && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("不能讀取文件: " + file.getName());
        }

        is.close();
        return bytes;
    }

    /**
     * 獲取標準文件大小,如30KB,15.5MB
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static String getFileSize(File file)
            throws IOException {
        long size = getFileLength(file);
        DecimalFormat df = new DecimalFormat("###.##");
        float f;
        if (size < 1024 * 1024) {
            f = (float) ((float) size / (float) 1024);
            return (df.format(new Float(f).doubleValue()) + " KB");
        } else {
            f = (float) ((float) size / (float) (1024 * 1024));
            return (df.format(new Float(f).doubleValue()) + " MB");
        }

    }

    /**
     * 復制文件
     * 
     * @param f1
     *            源文件
     * @param f2
     *            目標文件
     * @throws Exception
     */
    public static void copyFile(File f1, File f2)
            throws Exception {
        int length = 2097152;
        FileInputStream in = new FileInputStream(f1);
        FileOutputStream out = new FileOutputStream(f2);
        FileChannel inC = in.getChannel();
        FileChannel outC = out.getChannel();
        ByteBuffer b = null;
        while (true) {
            if (inC.position() == inC.size()) {
                inC.close();
                outC.close();
            }
            if ((inC.size() - inC.position()) < length) {
                length = (int) (inC.size() - inC.position());
            } else
                length = 2097152;
            b = ByteBuffer.allocateDirect(length);
            inC.read(b);
            b.flip();
            outC.write(b);
            outC.force(false);
        }
    }

    /**
     * 檢查文件是否存在
     * 
     * @param fileName
     * @return
     * @throws IOException
     */
    public static boolean existFile(String fileName)
            throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new IOException("文件未找到:" + fileName);
        }
        return file.exists();
    }

    /**
     * 刪除文件
     * 
     * @param fileName
     */
    public static void deleteFile(String fileName)
            throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new IOException("文件未找到:" + fileName);
        }
        file.delete();
    }

    /**
     * 讀取文件到字符串
     * 
     * @param fileName
     * @return
     * @throws IOException
     */
    public static String readFile(String fileName)
            throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new IOException("文件未找到:" + fileName);
        }

        BufferedReader in = new BufferedReader(new FileReader(file));
        StringBuffer sb = new StringBuffer();
        String str = "";
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
        in.close();
        return sb.toString();
    }

    /**
     * 獲取目錄所有所有文件和文件夾
     * 
     * @param fileName
     * @return
     * @throws IOException
     */
    public static List<File> listFiles(String fileName)
            throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new IOException("文件未找到:" + fileName);
        }
        return Arrays.asList(file.listFiles());
    }

    /**
     * 創建目錄
     * 
     * @param dir
     */
    public static void mkdir(String dir) {
        String dirTemp = dir;
        File dirPath = new File(dirTemp);
        if (!dirPath.exists()) {
            dirPath.mkdir();
        }
    }

    /**
     * 新建文件
     * 
     * @param fileName
     *            String 包含路徑的文件名 如:E:\phsftp\src\123.txt
     * @param content
     *            String 文件內容
     */
    public static void createNewFile(String fileName, String content)
            throws IOException {
        String fileNameTemp = fileName;
        File filePath = new File(fileNameTemp);
        if (!filePath.exists()) {
            filePath.createNewFile();
        }
        FileWriter fw = new FileWriter(filePath);
        PrintWriter pw = new PrintWriter(fw);
        String strContent = content;
        pw.println(strContent);
        pw.flush();
        pw.close();
        fw.close();

    }

    /**
     * 刪除文件夾
     * 
     * @param folderPath
     *            文件夾路徑
     */
    public static void delFolder(String folderPath) {
        // 刪除文件夾里面所有內容
        delAllFile(folderPath);
        String filePath = folderPath;
        java.io.File myFilePath = new java.io.File(filePath);
        // 刪除空文件夾
        myFilePath.delete();
    }

    /**
     * 刪除文件夾里面的所有文件
     * 
     * @param path
     *            文件夾路徑
     */
    public static void delAllFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return;
        }
        if (!file.isDirectory()) {
            return;
        }
        String[] childFiles = file.list();
        File temp = null;
        for (int i = 0; i < childFiles.length; i++) {
            // File.separator與系統有關的默認名稱分隔符
            // 在UNIX系統上,此字段的值為'/';在Microsoft Windows系統上,它為 '\'。
            if (path.endsWith(File.separator)) {
                temp = new File(path + childFiles[i]);
            } else {
                temp = new File(path + File.separator + childFiles[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + File.separatorChar + childFiles[i]);// 先刪除文件夾里面的文件
                delFolder(path + File.separatorChar + childFiles[i]);// 再刪除空文件夾
            }
        }
    }

    /**
     * 復制單個文件,傳統方式
     * 
     * @param srcFile
     *            包含路徑的源文件 如:E:/phsftp/src/abc.txt
     * @param dirDest
     *            目標文件目錄;若文件目錄不存在則自動創建 如:E:/phsftp/dest
     * @throws IOException
     */
    public static void copyFile(String srcFile, String dirDest)
            throws IOException {
        FileInputStream in = new FileInputStream(srcFile);
        mkdir(dirDest);
        FileOutputStream out = new FileOutputStream(dirDest + "/" + new File(srcFile).getName());
        int len;
        byte buffer[] = new byte[1024];
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
        out.close();
        in.close();
    }

    /**
     * 復制文件夾
     * 
     * @param oldPath
     *            String 源文件夾路徑 如:E:/phsftp/src
     * @param newPath
     *            String 目標文件夾路徑 如:E:/phsftp/dest
     * @return boolean
     */
    public static void copyFolder(String oldPath, String newPath)
            throws IOException {
        // 如果文件夾不存在 則新建文件夾
        mkdir(newPath);
        File file = new File(oldPath);
        String[] files = file.list();
        File temp = null;
        for (int i = 0; i < files.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + files[i]);
            } else {
                temp = new File(oldPath + File.separator + files[i]);
            }

            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(newPath + "/"
                        + (temp.getName()).toString());
                byte[] buffer = new byte[1024 * 2];
                int len;
                while ((len = input.read(buffer)) != -1) {
                    output.write(buffer, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {// 如果是子文件夾
                copyFolder(oldPath + "/" + files[i], newPath + "/" + files[i]);
            }
        }
    }

    /**
     * 移動文件到指定目錄
     * 
     * @param oldPath
     *            包含路徑的文件名 如:E:/phsftp/src/ljq.txt
     * @param newPath
     *            目標文件目錄 如:E:/phsftp/dest
     */
    public static void moveFile(String oldPath, String newPath)
            throws IOException {
        copyFile(oldPath, newPath);
        deleteFile(oldPath);
    }

    /**
     * 移動文件到指定目錄,不會刪除文件夾
     * 
     * @param oldPath
     *            源文件目錄 如:E:/phsftp/src
     * @param newPath
     *            目標文件目錄 如:E:/phsftp/dest
     */
    public static void moveFiles(String oldPath, String newPath)
            throws IOException {
        copyFolder(oldPath, newPath);
        delAllFile(oldPath);
    }

    /**
     * 移動文件到指定目錄,會刪除文件夾
     * 
     * @param oldPath
     *            源文件目錄 如:E:/phsftp/src
     * @param newPath
     *            目標文件目錄 如:E:/phsftp/dest
     */
    public static void moveFolder(String oldPath, String newPath)
            throws IOException {
        copyFolder(oldPath, newPath);
        delFolder(oldPath);
    }

    /**
     * 解壓zip文件
     * 說明:本程序通過ZipOutputStream和ZipInputStream實現了zip壓縮和解壓功能. 
     * 問題:由于java.util.zip包并不支持漢字,當zip文件中有名字為中文的文件時, 
     * 就會出現異常:"Exception  in thread "main " java.lang.IllegalArgumentException  
     * at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285) 
     * @param srcDir
     *            解壓前存放的目錄
     * @param destDir
     *            解壓后存放的目錄
     * @throws Exception
     */
    public static void unZip(String srcDir, String destDir)
            throws IOException {
        int leng = 0;
        byte[] b = new byte[1024 * 2];
        /** 獲取zip格式的文件 **/
        File[] zipFiles = new ExtensionFileFilter("zip").getFiles(srcDir);
        if (zipFiles != null && !"".equals(zipFiles)) {
            for (int i = 0; i < zipFiles.length; i++) {
                File file = zipFiles[i];
                /** 解壓的輸入流 * */
                ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
                ZipEntry entry = null;
                while ((entry = zis.getNextEntry()) != null) {
                    File destFile = null;
                    if (destDir.endsWith(File.separator)) {
                        destFile = new File(destDir + entry.getName());
                    } else {
                        destFile = new File(destDir + File.separator + entry.getName());
                    }
                    /** 把解壓包中的文件拷貝到目標目錄 * */
                    FileOutputStream fos = new FileOutputStream(destFile);
                    while ((leng = zis.read(b)) != -1) {
                        fos.write(b, 0, leng);
                    }
                    fos.close();
                }
                zis.close();
            }
        }
    }

    /**
     * 壓縮文件
     * 說明:本程序通過ZipOutputStream和ZipInputStream實現了zip壓縮和解壓功能. 
     * 問題:由于java.util.zip包并不支持漢字,當zip文件中有名字為中文的文件時, 
     * 就會出現異常:"Exception  in thread "main " java.lang.IllegalArgumentException  
     * at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285) 
     * @param srcDir
     *            壓縮前存放的目錄
     * @param destDir
     *            壓縮后存放的目錄
     * @throws Exception
     */
    public static void zip(String srcDir, String destDir)
            throws IOException {
        String tempFileName = null;
        byte[] buf = new byte[1024 * 2];
        int len;
        // 獲取要壓縮的文件
        File[] files = new File(srcDir).listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    if (destDir.endsWith(File.separator)) {
                        tempFileName = destDir + file.getName() + ".zip";
                    } else {
                        tempFileName = destDir + File.separator + file.getName() + ".zip";
                    }
                    FileOutputStream fos = new FileOutputStream(tempFileName);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    ZipOutputStream zos = new ZipOutputStream(bos);// 壓縮包

                    ZipEntry ze = new ZipEntry(file.getName());// 壓縮包文件名
                    zos.putNextEntry(ze);// 寫入新的ZIP文件條目并將流定位到條目數據的開始處

                    while ((len = bis.read(buf)) != -1) {
                        zos.write(buf, 0, len);
                        zos.flush();
                    }
                    bis.close();
                    zos.close();

                }
            }
        }
    }

    /**
     * 讀取數據
     * 
     * @param inSream
     * @param charsetName
     * @return
     * @throws Exception
     */
    public static String readData(InputStream inSream, String charsetName)
            throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inSream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inSream.close();
        return new String(data, charsetName);
    }

    /**
     * 一行一行讀取文件,適合字符讀取,若讀取中文字符時會出現亂碼
     * 
     * @param path
     * @return
     * @throws Exception
     */
    public static Set<String> readFileLine(String path)
            throws IOException {
        Set<String> datas = new HashSet<String>();
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
        while ((line = br.readLine()) != null) {
            datas.add(line);
        }
        br.close();
        fr.close();
        return datas;
    }

    public static void main(String[] args) {
        try {
            unZip("c:/test", "c:/test");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


class ExtensionFileFilter
        implements FileFilter {

    private String extension;

    public ExtensionFileFilter(String extension) {
        this.extension = extension;
    }

    public File[] getFiles(String srcDir) throws IOException {
        return (File[]) FileUtils.listFiles(srcDir).toArray();
    }

    public boolean accept(File file) {
        if (file.isDirectory()) {
            return false;
        }

        String name = file.getName();
        // find the last
        int idx = name.lastIndexOf(".");
        if (idx == -1) {
            return false;
        } else if (idx == name.length() - 1) {
            return false;
        } else {
            return this.extension.equals(name.substring(idx + 1));
        }
    }

}

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!