码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-02-28 15:04:31      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

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 (文件流与字符数组流)

标签:

原文地址:http://www.cnblogs.com/larobyo/p/5224644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!