Popular Posts
Enable edit option in Shutter in Linux sudo apt-get install libgoo-canvas-perl Reference: How To Fix Disabled Edit Option In Shutter in Linux Mint CORS in Asp.net MVC Web API v2 Step 1. Install cors from NeGet Step 2. Enable cors in config using System; using System.Collections.Generic; using System.Linq; using ... DNS SERVER LIST Google 8.8.8.8 8.8.4.4 TWNIC 192.83.166.11 211.72.210.250 HiNet 168.95.1.1 168.95.192.1 Seednet 北區 DNS (台北, 桃園, 新竹, 宜蘭, 花蓮, 苗栗) 139....
Stats
FileFactory
package bruce.lib.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * @author Bruce Tsai
 * 
 */
public final class FileFactory {

    /**
     * 建立目錄, 若父層目錄不存在一併建立
     * 
     * @param file
     *            目錄路徑
     */
    public static void createDirectory(String file) {
        createDirectory(new File(file));
    }

    /**
     * 建立目錄, 若父層目錄不存在一併建立
     * 
     * @param file
     *            目錄
     */
    public static void createDirectory(File file) {
        if (file.getParentFile() != null && !file.getParentFile().exists()) {
            createDirectory(file.getParentFile());
        }
        if (!file.exists())
            file.mkdir();
    }

    /**
     * 刪除檔案或目錄, 若刪除的為目錄, 一併刪除目錄下的所有檔案
     * 
     * @param file
     *            檔案或目錄
     */
    public static void delete(String file) {
        delete(new File(file));
    }

    /**
     * 刪除檔案或目錄, 若刪除的為目錄, 一併刪除目錄下的所有檔案
     * 
     * @param file
     *            檔案或目錄
     */
    public static void delete(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File f : files) {
                    FileFactory.delete(f);
                }
            }
            int limit = 10 * 1000; // 10秒
            long tick = System.currentTimeMillis();
            while (file.exists()) {
                // 指定時間內無法刪除時擲出例外
                if (file.delete() && System.currentTimeMillis() - tick > limit)
                    throw new RuntimeException();

            }
        }
    }

    /**
     * 複製檔案或目錄
     * 
     * @param sourceFile
     *            來源檔案或目錄
     * @param destFile
     *            目的目錄
     * @throws IOException
     */
    public static void copy(String sourceFile, String destFile) throws IOException {
        copy(new File(sourceFile), new File(destFile));
    }

    /**
     * 複製檔案或目錄
     * 
     * @param sourceFile
     *            來源檔案或目錄
     * @param destFile
     *            目的目錄
     * @throws IOException
     */
    public static void copy(File sourceFile, File destFile) throws IOException {
        if (sourceFile.isFile()) {
            sourceFile.setReadable(true);
            FileInputStream fis = new FileInputStream(sourceFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            byte[] buffer = new byte[128];
            int readed = -1;
            while ((readed = bis.read(buffer)) > -1) {
                bos.write(buffer, 0, readed);
            }

            bos.close();
            fos.close();
            fis.close();
            fos.close();
        } else {
            destFile.mkdir();
            File[] files = sourceFile.listFiles();
            for (File f : files) {
                copy(f, new File(String.format("%s\\%s", destFile.getAbsolutePath(), f.getName())));
            }
        }
    }

    /**
     * 讀取檔案內的全部文字資料
     * 
     * @param file
     *            要讀取的檔案
     * @return
     * @throws IOException
     */
    public static String readAllText(File file) throws IOException {
        return new String(readAllBytes(file));
    }

    /**
     * 讀取檔案內的全部文字資料
     * 
     * @param file
     *            要讀取的檔案
     * @param encoding
     *            文字編碼
     * @return
     * @throws IOException
     */
    public static String readAllText(File file, String encoding) throws IOException {
        return new String(readAllBytes(file), encoding);
    }

    /**
     * 讀取檔案內的全部位元資料
     * 
     * @param file
     *            要讀取的檔案
     * @return
     * @throws IOException
     */
    public static byte[] readAllBytes(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int b = -1;
        while ((b = fis.read()) > -1) {
            baos.write(b);
        }
        baos.close();
        fis.close();
        return baos.toByteArray();
    }

    /**
     * 將內容寫入檔案. 寫入時呼叫 toString() 並將回傳值寫入檔案
     * 
     * @param file
     *            要寫入的檔案
     * @param content
     *            要寫入的內容
     * @throws IOException
     */
    public static void writeAllText(File file, Object content) throws IOException {
        FileWriter writer = new FileWriter(file);
        writer.write(content.toString());
        writer.close();
    }

    /**
     * 將內容寫入檔案. 寫入時呼叫 toString() 並將回傳值寫入檔案
     * 
     * @param file
     *            要寫入的檔案
     * @param content
     *            要寫入的內容
     * @param encoding
     *            文字編碼
     * @throws IOException
     */
    public static void writeAllText(File file, Object content, String encoding) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter writer = new OutputStreamWriter(fos, encoding);
        writer.write(content.toString());
        writer.close();
        fos.close();
    }

    /**
     * 將內容寫入檔案. 寫入時呼叫 toString() 並將回傳值寫入檔案, 若檔案存在, 將內容加入至檔案結尾, 檔案不存在時, 直接寫入
     * 
     * @param file
     *            要寫入的檔案
     * @param content
     *            要寫入的內容
     * @throws IOException
     */
    public static void appendAllText(File file, Object content) throws IOException {
        FileWriter writer = new FileWriter(file, true);
        writer.write(content.toString());
        writer.close();
    }

    /**
     * 將內容寫入檔案. 寫入時呼叫 toString() 並將回傳值寫入檔案, 若檔案存在, 將內容加入至檔案結尾, 檔案不存在時, 直接寫入
     * 
     * @param file
     *            要寫入的檔案
     * @param content
     *            要寫入的內容
     * @param encoding
     *            文字編碼
     * @throws IOException
     */
    public static void appendAllText(File file, Object content, String encoding) throws IOException {
        if (file.exists()) {
            appendAllText(file, content);
        } else {
            writeAllText(file, content, encoding);
        }
    }

    /**
     * 將內容寫入檔案
     * 
     * @param file
     *            要寫入的檔案
     * @param bytes
     *            要寫入的內容
     * @throws IOException
     */
    public static void writeAllBytes(File file, byte[] bytes) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bytes);
        fos.close();
    }

    /**
     * 將內容寫入檔案. 若檔案存在, 將內容加入至檔案結尾, 檔案不存在時, 直接寫入
     * 
     * @param f
     *            要寫入的檔案
     * @param bytes
     *            要寫入的內容
     * @throws IOException
     */
    public static void appendAllBytes(File f, byte[] bytes) throws IOException {
        FileOutputStream fos = new FileOutputStream(f, true);
        fos.write(bytes);
        fos.close();
    }
}