private static void StringWriterAndReader() throws Exception {
//字符串流(字符串的内存流)
//字符串输入流
StringWriter sWriter=new StringWriter();
sWriter.write("文档在手,天下我有!");
sWriter.write("吃得苦中苦,方为人上人!");
// System.out.println(sWriter.toString());
//字符串的输出流
StringReader sReader=new StringReader(sWriter.toString());
char[] buffer =new char[1024];
int len=-1;
while((len=sReader.read(buffer))!=-1){
System.out.println(new String(buffer,0,len) );
}
sWriter.close();
sReader.close(); } private static void charArray() throws IOException {
//字符数组流/内存流
//字符数组输出流:程序----->内存
CharArrayWriter writer =new CharArrayWriter();
writer.write("欢迎学习Java!");
char[] content=writer.toCharArray();
//字节数组输入流:内存----->程序
CharArrayReader reader=new CharArrayReader(content);
int len=-1;
char[] ch=new char[1024];
while((len=reader.read(ch))!=-1){
System.out.println(new String(ch,0,len));
}
writer.close();
reader.close();
} private static void byteArray() throws Exception {
//字节数组流/内存流
//字节数组输出流:程序----->内存
ByteArrayOutputStream out=new ByteArrayOutputStream();
out.write("大家好啊!".getBytes());
byte[] bye=out.toByteArray();
//字节数组输入流:内存----->程序
ByteArrayInputStream in=new ByteArrayInputStream(bye);
byte[] buffer=new byte[1024];
int len=-1;
while((len=in.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
out.close();
in.close();
}
合并流(SequenceInputStream)
//创建顺序流对象
SequenceInputStream in= new SequenceInputStream(new FileInputStream("file/copy.txt"), new FileInputStream("file/copy12.txt"));
byte[] buffer=new byte[1024];
int len=-1;
while((len=in.read(buffer))!=-1){
System.out.println(new String(buffer,0,len));
}
in.close();