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

JavaSE 高级 第10节 字节数组输出流ByteArrayOutputStream

时间:2016-07-24 19:09:28      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

2016-07-24

1,ByteArrayOutputStream

         FileOutputStream 把文件作为写入的目的地

         ByteArrayOutputStream 把字节数组作为写入的目的地

package com.java1995;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class Test {

    public static void main(String[] args) {
        String str = "abcdefghijklmnopqrstuvwxyz";
        byte[] b = str.getBytes();

        ByteArrayInputStream bais = new ByteArrayInputStream(b);
        BufferedInputStream bis = new BufferedInputStream(bais);
        int temp = 0;
        try {
            temp = bis.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int count = 0;
        while (temp != -1) {
            System.out.print((char) temp);
            try {
                temp = bis.read();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(count++);
        }
        System.out.println(count);

    }
}

技术分享

package com.java1995;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class TestByteArrayOutputStream {

    public static void main(String[] args) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        String temp = "hello world hello everyone!";
        byte[] b = temp.getBytes();

        try {
            bos.write(b);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(bos.toString());

        byte[] b1 = bos.toByteArray();
        for (int i = 0; i < b1.length; i++) {
            System.out.print((char) b1[i]);

        }
    }
}

技术分享

【参考资料】

[1] Java轻松入门经典教程【完整版】

JavaSE 高级 第10节 字节数组输出流ByteArrayOutputStream

标签:

原文地址:http://www.cnblogs.com/cenliang/p/5701213.html

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