流之间的转换,几个demo

本文介绍了不同类型的流之间的转换方法,包括字节流转字符流、byte数组与文件互转、InputStream转字节数组及BufferedImage转InputStream等。通过具体代码示例展示了如何实现这些转换。

字节流转换成字符流:

    @Test
    public void test02() throws Exception {
        Reader r = new BufferedReader(new InputStreamReader(        new BufferedInputStream(new FileInputStream(new File("d:/a.txt")))));
        Writer w = new BufferedWriter(new OutputStreamWriter(        new BufferedOutputStream(new FileOutputStream(new File("d:/a.txt")))));
        //空格处, 以字节流转换到字符流, 底层使用适配器模式, 改变客户期望的接口
        
        w.write(2);
        w.flush();
        System.out.print("-----------------------------------------------" + r.read());
    }

 

byte数组和文件的相互转换:

一:

    /** 
     * 获得指定文件的byte数组 
     */  
    private byte[] getBytes(String filePath){  
        byte[] buffer = null;  
        try {  
            File file = new File(filePath);  
            FileInputStream fis = new FileInputStream(file);  
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
            byte[] b = new byte[1000];  
            int n;  
            while ((n = fis.read(b)) != -1) {  
                bos.write(b, 0, n);  
            }  
            fis.close();  
            bos.close();  
            buffer = bos.toByteArray();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return buffer;  
    }

二:

    /** 
     * 根据byte数组,生成文件 
     */  
    public static void getFile(byte[] bfile, String filePath,String fileName) {  
        BufferedOutputStream bos = null;  
        FileOutputStream fos = null;  
        File file = null;  
        try {  
            File dir = new File(filePath);  
            if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在  
                dir.mkdirs();  
            }  
            file = new File(filePath+"\\"+fileName);  
            fos = new FileOutputStream(file);  
            bos = new BufferedOutputStream(fos);  
            bos.write(bfile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (bos != null) {  
                try {  
                    bos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
            if (fos != null) {  
                try {  
                    fos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
        }  
    }

 

InputStream转换成字节数组:

package cn.outofmemory.io;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStream2ByteArray {

    public static void main(String[] args) throws IOException {
        InputStream in=new FileInputStream("/tmp/req.data");
        byte[] data=toByteArray(in);
        in.close();
        FileOutputStream out=new FileOutputStream("/tmp/req2.data");
        out.write(data);
        out.close();
    }

    public static byte[] toByteArray(InputStream in) throws IOException {
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        byte[] buffer=new byte[1024*4];
        int n=0;
        while ( (n=in.read(buffer)) !=-1) {
            out.write(buffer,0,n);
        }
        return out.toByteArray();
    }
}

 

BufferedImage转换成InputStream:

URL url = new URL("http://www.google.com/intl/en_ALL/images/logo.gif");
BufferedImage image = ImageIO.read(url);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "gif", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());

 

转载于:https://www.cnblogs.com/gscq073240/articles/7081052.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值