JAVAIO (文件流与字符数组流)

package com.lz.byteArrayStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * FileStream
 * ByteArrayStream
 * 
 * @author LZ
 *
 */
public class Test {
    public static void main(String[] args) {
        try {
            read(write());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copy(File in, File out) throws FileNotFoundException,
            IOException {
        FileInputStream fis = new FileInputStream(in);
        FileOutputStream fos = new FileOutputStream(out);

        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        byte[] flush = new byte[10];
        int len = 0;
        while ((len = bis.read(flush)) != -1) {
            bos.write(flush, 0, len);
        }
        bos.flush();
        bos.close();
        bis.close();
        fos.close();
        fis.close();
    }

    public static void read(byte[] src) throws IOException {
        //资源 由方法传入
        
        //选择流
        InputStream is = new BufferedInputStream(new ByteArrayInputStream(src));
        byte[] flush = new byte[1024];
        int len = 0;
        while ((len = is.read(flush)) != -1) {
            System.out.println(new String(flush, 0, len));
        }
        is.close();
    }

    public static byte[] write() throws IOException {
        //目的地
        byte[] dest;
        //选择流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        String msg = "byteOutStream";
        byte[] info = msg.getBytes();
        bos.write(info, 0, info.length);
        //获取数据
        dest = bos.toByteArray();
        //关闭资源
        bos.close();
        return dest;
    }
}

 

你可能感兴趣的:(JAVAIO (文件流与字符数组流))