Java-FileUtil工具类

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

/**
 * 文件处理工具
 */
public class FileUtils {

    /**
     * 將文件转为字节
     * 
     * @param filename
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(String filename) throws IOException {

        File f = new File(filename);
        if (!f.exists()) {
            throw new FileNotFoundException(filename);
        }

        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(f));
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            int len = 0;
            while (-1 != (len = in.read(buffer, 0, buf_size))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bos.close();
        }

    }

    /**
     * InputStream 转为 byte[]
     * 
     * @param inStream
     * @return
     * @throws IOException
     */
    public static final byte[] inputStreamToByteArray(InputStream inStream)
            throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }

    /**
     * 文件类型
     * 
     * @param fileName
     * @return
     */
    public static String getFileType(String fileName) {

        int start = 0;
        start = fileName.lastIndexOf(".");
        if (start <= 0) {
            return "";
        }
        return "." + fileName.substring(start + 1);
    }

    /**
     * @param 文件路径
     * @return 文件名
     */
    public static String getFileName(String filepath) {

        int start = 0;
        start = filepath.lastIndexOf("/");
        if (start <= 0) {
            return "";
        }
        return filepath.substring(start + 1);
    }

    public static int getFileSize(File file) {
        if (file.exists() && file.isFile()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                return fis.available() / 1024;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null)
                        fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return 0;
    }

    /**
     * 目的: 将String数据存为文件 。
     * @param src
     *            字符串
     * @param path
     *            文件保存路徑
     * @return
     */
    public static File saveFileFromStr(String src, String path) {
        byte[] b = src.getBytes();
        BufferedOutputStream stream = null;
        File file = null;
        try {
            file = new File(path);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }

    /**
     * 目的: 将byte数据存为文件。
     * @param b
     *            byte数据
     * @param path
     *            文件保存路径
     * @return
     */
    public static File saveFileFromBytes(byte[] b, String path) {
        BufferedOutputStream stream = null;
        File file = null;
        try {
            file = new File(path);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }


    /**
     * 目的:byte 转String 编码BASE64 。
     * @param bstr
     * @return
     */
    public static String encodeBASE64(byte[] bstr) {

        return new sun.misc.BASE64Encoder().encode(bstr);

    }


    /**
     * 目的:string 转 byte 解码BASE64 。
     * @param str
     * @return
     */
    public static byte[] decodeBASE64(String str) {
        byte[] bt = null;
        try {
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            bt =decoder.decodeBuffer(str);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bt;
    }

    /**
     * 目的:byte[] 转为String  
     * @param b
     * @return
     */
    public static String byteToString(byte[] b){
        try {
            return new String(b,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return new String(b);
        }
    }

    /**
     * 目的:String 转为 byte[]  。
     * @param b
     * @return
     */
    public static byte[] stringTobyte(String str){
        try {
            return str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return str.getBytes();
        }
    }   


}
原文地址:https://www.cnblogs.com/bilaisheng/p/10211021.html