文件流读取 InputStream

/**

     * BufferedReader转换成String<br/>

     * 注意:流关闭需要自行处理

     * @param reader

     * @return String

     * @throws IOException

     */

    public static String bufferedReader2String(BufferedReader reader) throws IOException {

        StringBuffer buf = new StringBuffer();

        String line = null;

        while( (line = reader.readLine()) != null) {

            buf.append(line);

            buf.append("\r\n");

        }

 

        return buf.toString();

    }

 

    /**

     * 处理输出<br/>

     * 注意:流关闭需要自行处理

     * @param out

     * @param data

     * @param len

     * @throws IOException

     */

    public static void doOutput(OutputStream out, byte[] data, int len)

            throws IOException {

        int dataLen = data.length;

        int off = 0;

        while (off < data.length) {

            if (len >= dataLen) {

                out.write(data, off, dataLen);

                off += dataLen;

            } else {

                out.write(data, off, len);

                off += len;

                dataLen -= len;

            }

 

            // 刷新缓冲区

            out.flush();

        }

 

    }

 

    /**

     * 字符串转换成char数组

     * @param str

     * @return char[]

     */

    public static char[] str2CharArray(String str) {

        if(null == str) return null;

 

        return str.toCharArray();

    }

 

    public static InputStream String2Inputstream(String str) {

        return new ByteArrayInputStream(str.getBytes());

    }

 

    /**

     * InputStream转换成Byte

     * 注意:流关闭需要自行处理

     * @param in

     * @return byte

     * @throws Exception

     */

    public static byte[] InputStreamTOByte(InputStream in) throws IOException{  

 

        int BUFFER_SIZE = 4096;  

        ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 

        byte[] data = new byte[BUFFER_SIZE];  

        int count = -1;  

 

        while((count = in.read(data,0,BUFFER_SIZE)) != -1)  

            outStream.write(data, 0, count);  

 

        data = null;  

        byte[] outByte = outStream.toByteArray();

        outStream.close();

 

        return outByte;  

    } 

 

    /**

     * InputStream转换成String

     * 注意:流关闭需要自行处理

     * @param in

     * @param encoding 编码

     * @return String

     * @throws Exception

     */

    public static String InputStreamTOString(InputStream in,String encoding) throws IOException{  

 

        return new String(InputStreamTOByte(in),encoding);

 

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值