Java工具集-IO工具

代码示例

package com.simple.util.io;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @program: simple_tools
 * @description:
 * @author: Mr.chen
 * @create: 2020-06-09 10:00
 **/
public class IOUtil {
    /**
     *
     * @param is
     * @return
     */
    public static String ioToString(InputStream is) {
        try {
            return IOUtils.toString(is, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     *
     * @param xml
     * @param out
     */
    public static void write(String xml, OutputStream out) {
        try {
            IOUtils.write(xml, out, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param obj
     */
    public static void close(Object obj) {
        try {
            if (obj instanceof InputStream) {
                ((InputStream) obj).close();
            }
            if (obj instanceof OutputStream) {
                ((OutputStream) obj).close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(Java工具集)