java ByteArray的使用

案例:

 1 package cn.kongxh.io5;
 2 
 3 import java.io.* ;
 4 public class ByteArrayDemo01{
 5     public static void main(String args[]){
 6         String str = "HELLOWORLD" ;        // 定义一个字符串,全部由大写字母组成
 7         ByteArrayInputStream bis = null ;    // 内存输入流
 8         ByteArrayOutputStream bos = null ;    // 内存输出流
 9         bis = new ByteArrayInputStream(str.getBytes()) ;    // 向内存中输出内容
10         bos = new ByteArrayOutputStream() ;    // 准备从内存ByteArrayInputStream中读取内容
11         int temp = 0 ;
12         while((temp=bis.read())!=-1){
13             char c = (char) temp ;    // 读取的数字变为字符
14             bos.write(Character.toLowerCase(c)) ;    // 将字符变为小写
15         }
16         // 所有的数据就全部都在ByteArrayOutputStream中
17         String newStr = bos.toString() ;    // 取出内容
18         try{
19             bis.close() ;
20             bos.close() ;
21         }catch(IOException e){
22             e.printStackTrace() ;
23         }
24         System.out.println(newStr) ;
25     }
26 };

 

posted on 2017-05-20 16:03  祥昊  阅读(5221)  评论(0)    收藏  举报

导航