IOUtils--IO流应用

来源:互联网 发布:工控软件开发是什么 编辑:程序博客网 时间:2024/06/10 10:54

IO字节流基类:--InputStream:--OutputStream:

IO字符流基类:--Reader:--Writer:

IO转换流:InputStreamReader 字节流通向字符流的桥梁,用于操作字节流的字符流对象。

                  OutputStreamWriter 字符流通向字节流的桥梁,用于操作字符流的字节流对象。

在使用IO流的时候尽量使用自定义小数组和Buffer组合,速度杠杠的!下面是自己写的一个IO工具类,自定义了异常和对IO流的处理异常(要求jdk为1.7版本以上)

public class IOUtils {
    /**
     * 用io自带缓存流拷贝数据(非文本文件!!!)
     *
     * @param address1
     *            待拷贝文件的地址
     * @param address2
     *            拷贝文件后的存放地址
     * @throws IOException
     */
    public static void copyFiles(String address1, String address2)
            throws IOException {
        // jdk1.7 提供的异常处理方法 不需要手动关流
        try (BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream(address1));
                BufferedOutputStream bos = new BufferedOutputStream(
                        new FileOutputStream(address2));) {
            int length;
            byte[] arr = new byte[1024 * 8];
            while ((length = bis.read(arr)) != -1) {
                bos.write(arr, 0, length);
            }
        }
    }

    /**
     * 用io自带缓存流拷贝数据(纯文本文件!!!)
     *
     * @param address1
     *            待拷贝文件的地址
     * @param address2
     *            拷贝文件后的存放地址
     * @throws IOException
     */
    public static void copyTxtFiles(String address1, String address2)
            throws IOException {
        // jdk1.7 提供的异常处理方法 不需要手动关流
        try (BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(address1), "UTF-8"));
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(address2), "UTF-8"));) {
            int length;
            char[] arr = new char[1024 * 8];
            while ((length = br.read(arr)) != -1) {
                bw.write(arr, 0, length);
                bw.flush();// 字符流需要刷新 因为中间存在一个转换的过程
            }
        }
    }

    /**
     * 将字符串转换为文件
     *
     * @param data
     * @param address
     * @throws IOException
     * @throws CustomException
     */
    public static void stringToFiles(String data, String address)
            throws IOException, CustomException {
        if (address == null || address == "") {
            throw new CustomException("文件地址为空,请检查重试!");
        }
        try (BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(address));) {
            byte[] bytes = data.getBytes();
            bos.write(bytes);
        }
    }

    /**
     * 将(本地)文件转换为字符串
     *
     * @param address
     * @return
     * @throws IOException
     */
    public static String filesToString(String address) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(address), "UTF-8"));
        StringBuffer sb = new StringBuffer();
        String msg;
        while ((msg = br.readLine()) != null) {
            sb.append(msg);
        }
        br.close();
        return sb.toString();
    }

    /**
     * 将文件转换为字符串
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static String filesToString(File file) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(file), "UTF-8"));
        StringBuffer sb = new StringBuffer();
        String msg;
        while ((msg = br.readLine()) != null) {
            sb.append(msg);
        }
        br.close();
        return sb.toString();
    }
}

/**
 * 自定义异常
 *
 * @author Simon
 *
 */
class CustomException extends Exception {

    public CustomException() {
        super();
    }

    public CustomException(String message) {
        super(message);
    }

}

0 0
原创粉丝点击