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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 (文件流与字符数组流)的主要内容,如果未能解决你的问题,请参考以下文章

JavaIO流--转换流--InpuStreamReaderOutputStreamWriter

javaIO--字符流

第2章 字符流与字节流

JAVA IO流相关代码(究极整理,字符流与字节流之间的转化)

javaIO流之字节到字符流的转换流

Java学习笔记6.2.1 字符流 - 文件字符流与缓冲字符流