java基础知识回顾之javaIO类--内存操作流ByteArrayInputStream和ByteArrayOutputSteam(操作字节数组)

直接看代码:

package cn.itcast.io.p6.bytestream;

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

public class ByteArrayStreamDemo {

    /**
     * @param args
     * @throws IOException 
     * 特点
     * 1.内存操作流
     * 2.不操作底层资源,不调用操作系统的底层资源,操作内存中的数据,内存流不需要关闭
     * 3.关闭流后还可以使用
     * 本例:内存操作流完成的一个大小写字母转换的程序:
     */
    public static void main(String[] args) {
        String str = "HELLO WORLD!";
        ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());//将内容输入到内存中
        ByteArrayOutputStream bos = new ByteArrayOutputStream();//将内存中的数据输出
        int ch = 0;
        bis.skip(2);//跳过两个字节
        System.out.println(bis.available());//返回此输入流读取的(或跳过)剩余的字节数
        while((ch=bis.read())!=-1){
            bos.write(Character.toLowerCase(ch));//将大小字符转化成小写
        }
        System.out.println(bos.toString());
    }

}

输出:由于跳过两个字节,HELLO WORLD!总共12个字节,则剩余10个字节。

10
llo world!

 
原文地址:https://www.cnblogs.com/200911/p/3909203.html