java内存流:java.io.ByteArrayInputStreamjava.io.ByteArrayOutputStreamjava.io.CharArrayReaderjava.io(代码片段

Posted xiongjiawei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java内存流:java.io.ByteArrayInputStreamjava.io.ByteArrayOutputStreamjava.io.CharArrayReaderjava.io(代码片段相关的知识,希望对你有一定的参考价值。

技术分享
 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 }
字符数组字符输入输出内存流

 

以上是关于java内存流:java.io.ByteArrayInputStreamjava.io.ByteArrayOutputStreamjava.io.CharArrayReaderjava.io(代码片段的主要内容,如果未能解决你的问题,请参考以下文章

Java核心类库-IO-字节数组流/内存流

Java学习总结—内存流,打印流,对象流,RandomAccessFile,装饰者设计模式

Java学习总结—内存流,打印流,对象流,RandomAccessFile,装饰者设计模式

Java中的内存流

JAVA基础——内存流

输入流与输出流