• 【java】内存流:java.io.ByteArrayInputStream、java.io.ByteArrayOutputStream、java.io.CharArrayReader、java.io.CharArrayWriter


     1 package 内存流;
     2 
     3 import java.io.ByteArrayInputStream;
     4 import java.io.ByteArrayOutputStream;
     5 import java.io.IOException;
     6 import java.io.InputStream;
     7 import java.io.OutputStream;
     8 
     9 public class Test {
    10     public static void main(String[] args) throws IOException {
    11         String str="Hello World!中国好国国国。";
    12         InputStream in=new ByteArrayInputStream(str.getBytes());
    13         OutputStream out=new ByteArrayOutputStream();
    14         int tmp=0;
    15         while((tmp=in.read())!=-1)
    16             out.write(Character.toUpperCase(tmp));
    17         System.out.println(out);//HELLO WORLD!中冠好冠冠冠。
    18         //中文有乱码!!!
    19         in.close();
    20         out.close();
    21     }
    22 }
    字节数组字节输入输出内存流
     1 package 内存流;
     2 
     3 import java.io.CharArrayReader;
     4 import java.io.CharArrayWriter;
     5 import java.io.IOException;
     6 import java.io.Reader;
     7 import java.io.Writer;
     8 
     9 public class Test {
    10     public static void main(String[] args) throws IOException {
    11         String str="Hello World!中国好国国国。";
    12         Reader in=new CharArrayReader(str.toCharArray());
    13         Writer out=new CharArrayWriter();
    14         int tmp=0;
    15         while((tmp=in.read())!=-1)
    16             out.write(Character.toUpperCase(tmp));
    17         System.out.println(out);//HELLO WORLD!中国好国国国。
    18         //字符流处理中文无乱码!!!
    19         in.close();
    20         out.close();
    21     }
    22 }
    字符数组字符输入输出内存流
     1 package 内存流;
     2 
     3 import java.io.*;
     4 
     5 public class Test {
     6     public static void main(String[] args) throws IOException {
     7         File[] files={
     8                 new File("D:"+File.separator+"testA.txt"),
     9                 new File("D:"+File.separator+"testB.txt"),
    10         };
    11         Reader[] ins={
    12                     new FileReader(files[0]),
    13                     new FileReader(files[1])
    14         };
    15         Writer out=new CharArrayWriter();
    16         int tmp=0;
    17         while((tmp=ins[0].read())!=-1)
    18             out.write(tmp);
    19         while((tmp=ins[1].read())!=-1)
    20             out.write(tmp);
    21         System.out.println(out.toString());
    22 //        System.out.println(new String(out.toCharArray()));
    23         //字节内存输出流ByteArrayOutputStream下有个类似的toByteArray()方法
    24         ins[0].close();
    25         ins[1].close();
    26         out.close();
    27     }
    28 }
    将多个文件读入到同一个内存输出流
  • 相关阅读:
    JSP内置/隐式对象(9个)
    会话技术——Cookie&Session
    Head First Java——读书笔记(一)
    并发和并行
    HotSpot
    域对象的比较
    书籍——Java
    润乾报表基础知识
    Android引导页过多导致OOM内存泄漏
    AngularJS 之1-初识
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/6684634.html
Copyright © 2020-2023  润新知