Java 文件幫助類


import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 文件幫助類
 */
@Slf4j
public class FileUtils {

    /**
     * 複製文件並輸入文件
     * @param : @param filePath1 原文件
     * @param : @param filePath2 複製之後的文件
     */
    public static void copyFile(String filePath1, String filePath2) {
        // 讀取本地文件
        try (FileInputStream in = new FileInputStream(filePath1);
            // 創建新的輸出文件類
            FileOutputStream out = new FileOutputStream(filePath2)){
            byte[] b = new byte[1024];
            while (in.read(b) > -1) {
                out.write(b);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 文件轉成byte[]
     * @param : @param filePath
     * @param : @return
     * @return: byte[]
     */
    public static byte[] getBytesByFile(String filePath) {
        byte[] data = null;
        // 創建文件類
        File file = new File(filePath);
        try (FileInputStream fis = new FileInputStream(file);
            // 字節輸出流,其中數據被寫入到字節數組中
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000)) {

            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            data = bos.toByteArray();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return data;
    }

    /**
     * 文件轉成byte[]
     * @param : @param file
     * @param : @return
     * @return: byte[]
     */
    public static byte[] getBytesByFile(File file) {
        byte[] data = null;
        try (FileInputStream fis = new FileInputStream(file);
            // 字節輸出流,其中數據被寫入到字節數組中
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000)) {

            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            data = bos.toByteArray();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return data;
    }


    /**
     * 將byte數組轉換成文件
     * @param : @param bytes
     * @param : @param filePath
     * @param : @param fileName
     */
    public static void writeFileByBytes(byte[] bytes, String filePath, String fileName) {
        // 檢查路徑是否存在,創建file文件
        File file = new File(ExistsPathUtils.exists(filePath), fileName);
        try (InputStream is = new ByteArrayInputStream(bytes);
            OutputStream os = new FileOutputStream(file)) {

            // 每次傳10M, 斷點續傳, 否則內存溢出; 如果一味的調整JVM的大小,治標不治本
            byte[] byteStr = new byte[1024 * 1024 * 10];
            int len = 0;

            while ((len = is.read(byteStr)) > 0) {
                os.write(byteStr,0,len);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }


    /**
     * 把某個文件寫到指定地址
     * @param : @param file 文件
     * @param : @param filePath 寫入地址
     */
    public static void writeFile(File file, String filePath) {
        byte[] b = getBytesByFile(file);
        try (FileOutputStream outputStream = new FileOutputStream(filePath)){
            outputStream.write(b);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 獲取網絡圖片流
     **/
    public static InputStream getImageStream(String url) {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = connection.getInputStream();
                return inputStream;
            }
        } catch (IOException e) {
            log.error("獲取網絡圖片出現異常,圖片路徑爲:" + url);
            log.error(e.getMessage());
        }
        return null;
    }

    // 靜態幫組類
    private static class ExistsPathUtils {
        // 判斷文件是否存在
        public static File exists(String filePath) {
            File disk = new File(filePath);
            // 如果路徑不存在就創建
            if (!disk.exists()) disk.mkdirs();
            return disk;
        }
    }
}

常用的io包,還有一個lombok包

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章