【Java工具類】Java文件工具類

描述: 整理常用文件操作方法。

public class FileUtils {
    /**
     * 讀取文件並作爲InputStream返回
     *
     * @param file 目標文件
     * @return
     * @throws FileNotFoundException
     */
    public static InputStream readFileAsInputStream(File file) throws FileNotFoundException {
        return new BufferedInputStream(new FileInputStream(file));
    }

    /**
     * 讀取文件並作爲byte[]返回
     *
     * @param file 目標文件
     * @return
     * @throws IOException
     */
    public static byte[] readFileAsBytes(File file) throws IOException {
        InputStream inputStream = readFileAsInputStream(file);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(inputStream.available());
        readAndWrite(inputStream, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    /**
     * 讀取文件並作爲Base64編碼String返回
     *
     * @param file 目標文件
     * @return
     * @throws IOException
     */
    public static String readFileAsStringByBase64(File file) throws IOException {
        byte[] bytes = readFileAsBytes(file);
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(bytes);
    }

    /**
     * 向目標文件寫數據
     *
     * @param file  目標文件
     * @param bytes 待寫數據
     * @throws IOException
     */
    public static void writeFile(File file, byte[] bytes) throws IOException {
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
        outputStream.write(bytes);
        outputStream.close();
    }

    /**
     * 向目標文件寫數據
     *
     * @param file   目標文件
     * @param bytes  待寫數據
     * @param append 是否追加
     * @throws IOException
     */
    public static void writeFile(File file, byte[] bytes, boolean append) throws IOException {
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file, append));
        outputStream.write(bytes);
        outputStream.close();
    }

    /**
     * 獲取文件後綴,不包含“.”
     *
     * @param file 目標文件
     * @return
     */
    public static String getFileSuffix(File file) {
        String fileName = file.getName();
        return fileName.substring(fileName.lastIndexOf(BaseConstants.PUNCTUATION_DOT) + 1);
    }

    /**
     * 將文件或文件夾複製到目標文件夾內
     *
     * @param src 源地址,支持文件或者文件夾
     * @param des 目標地址,支持文件夾
     * @throws IOException
     */
    public static void copyFile(File src, File des) throws IOException {
        if (src.isFile()) {
            String path = new StringBuilder(des.getAbsolutePath()).append(File.separator).append(src.getName()).toString();
            File file = new File(path).getParentFile();
            if (!file.exists()) {
                file.mkdirs();
            }
            InputStream inputStream = new BufferedInputStream(new FileInputStream(src));
            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(path));
            readAndWrite(inputStream, outputStream);
        } else if (src.isDirectory()) {
            String directoryName = src.getName();
            String directory = new StringBuilder(des.getAbsolutePath()).append(File.separator).append(directoryName).toString();
            File directoryFile = new File(directory);
            directoryFile.mkdirs();
            File[] files = src.listFiles();
            if (files.length != 0) {
                for (File file : files) {
                    copyFile(file, directoryFile);
                }
            }
        }
    }

    /**
     * 刪除文件及文件夾內所有內容
     *
     * @param path 待刪除文件或文件夾
     */
    public static void deleteFiles(File path) {
        if (path.isFile()) {
            path.delete();
        } else if (path.isDirectory()) {
            File[] files = path.listFiles();
            if (files.length != 0) {
                for (File file : files) {
                    deleteFiles(file);
                }
            }
            path.delete();
        }
    }

    /**
     * 讀取並寫入
     *
     * @param inputStream  讀取
     * @param outputStream 寫入
     * @throws IOException
     */
    public static void readAndWrite(InputStream inputStream, OutputStream outputStream) throws IOException {
        int length;
        byte[] bytes = new byte[BaseConstants.NUMBER_1024];
        while ((length = inputStream.read(bytes, 0, BaseConstants.NUMBER_1024)) != -1) {
            outputStream.write(bytes, 0, length);
        }
        inputStream.close();
        outputStream.close();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章