Java读取InputStream文本的几种方法总结,附读取字节流bytes字节数组byte[]方法

package org.jeecg.common.util;

import com.alibaba.fastjson.util.IOUtils;
import lombok.var;

import java.io.*;

public class StreamUtil {
    /**
     * @deprecated IOUtils.readAll
     */
    static String readText_ali(InputStream inputStream) {
        try {
            InputStreamReader dataReader = new InputStreamReader(inputStream);
            String result = IOUtils.readAll(dataReader);
            dataReader.close();
            inputStream.close();
            return result;
        } catch (Exception ex) {
            LogUtil.debug("Stream2Text error! readText_ali");
            return ex.toString();
        }
    }

    /**
     * @deprecated java.io.EOFException
     */
    static String readText_readUtf(InputStream inputStream) {
        try {
            var dataInputStream = new DataInputStream(inputStream);
            var result = dataInputStream.readUTF();
            dataInputStream.close();
            inputStream.close();
            return result;
        } catch (Exception ex) {
            LogUtil.debug("Stream2Text error! readText_readUtf");
            return ex.toString();
        }
    }

    /**
     * @deprecated 这个是有缺陷的,如果换行符是\n不是\r\n那么文本就变了
     */
    static String readText_bufferedReader(InputStream inputStream) {
        try {
            var inputStreamReader = new InputStreamReader(inputStream);
            var bufferedReader = new BufferedReader(inputStreamReader);
            String line = null;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null)
                stringBuilder.append(line + "\r\n");
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            return stringBuilder.toString();
        } catch (Exception ex) {
            LogUtil.debug("Stream2Text error! getText_bufferedReader");
            return ex.toString();
        }
    }

    static String readText_bytesOut(InputStream inputStream, String charset) {
        try {
            var byteArrayOutputStream = new ByteArrayOutputStream();
            int _byte;
            while ((_byte = inputStream.read()) != -1)
                byteArrayOutputStream.write(_byte);
            var result = byteArrayOutputStream.toString(charset);
            byteArrayOutputStream.close();
            inputStream.close();
            return result;
        } catch (Exception ex) {
            LogUtil.debug("Stream2Text error! readText_bytesOut");
            return ex.toString();
        }
    }

    public static String readText(InputStream inputStream, String charset) {
        return readText_bytesOut(inputStream, charset);
    }

    public static byte[] readBytes(InputStream inputStream) {
        try {
            var byteArrayOutputStream = new ByteArrayOutputStream();
            int _byte;
            while ((_byte = inputStream.read()) != -1)
                byteArrayOutputStream.write(_byte);
            var result = byteArrayOutputStream.toByteArray();
            byteArrayOutputStream.close();
            inputStream.close();
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

总结:ByteArrayOutputStream最科学

上一篇:Java获取数据库的列名、列数、标题、类型等信息,ResultSetMetaData的各种方法举例


下一篇:弹窗