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 }