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();
        }
    }   


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 是一种高级编程语言,具有广泛的应用领域。在 Java 开发中,使用工具类可以提高开发效率、减少代码冗余,同时也可以提供一些常用的功能模块供开发者使用。下面列举几个常用的 Java 工具类: 1. StringUtils:这是 Apache Commons Lang 提供的工具类,主要用于字符串的操作和处理。它含了很多方便的方法,如字符串的判空、拼接、截取、替换等,能够极大地简化字符串操作的代码。 2. FileUtil:这是一个用于文件操作的工具类,它封装了一些常用的文件处理方法,如文件的复制、移动、删除,以及读取文件内容、写入文件等操作。使用该工具类可以简化文件操作的代码,提高开发效率。 3. DateUtils:日期操作是 Java 开发中经常用到的功能之一。DateUtils 是 Apache Commons Lang 提供的工具类,可以方便地进行日期的格式化、解析、计算和比较等操作。使用该工具类可以减少日期操作的代码量,同时也能保证日期处理的准确性。 4. CollectionUtils:这是 Apache Commons Collections 提供的工具类,用于对集合类进行操作。它提供了很多方法,如集合的交集、并集、差集,以及集合的查找、筛选、排序等操作。使用 CollectionUtils 可以简化集合操作的代码,提高开发效率。 5. MathUtils:数学计算在一些复杂的项目中是不可避免的。MathUtils 是 Apache Commons Math 提供的工具类,它含了很多数学计算的方法,如常见数学函数的计算、矩阵运算、随机数生成等。使用该工具类可以简化数学计算的代码,提高开发效率。 以上所列的工具类只是 Java 开发中常用的一部分,根据具体的项目需求,还可以使用其他工具类来简化开发工作。总之,合理使用工具类可以提高代码的可读性、减少冗余,并提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值