一、推回输入流
(1)一般情况下使用输入流从磁盘,网络或者其它的物理介质读取数据都是顺序读取的,在流的内部会维护一个指针,读取数据的同时,指针会向后移动,直到读完为止。
(2)在一些实际常见中,如果读出来的数据不是想要的又不能再放回去怎么办?可以使用io提供的推回输入流。使用普通的IO输入流如果读取到不想要的数据,只能在程序里面处理掉,而使用IO里面的推回输入流读取数据则可以把数据给推回到输入流的缓冲区中。
(3)推回输入流主要为两个类:PushbackInputStream和PushbackReader
示例代码:
/**
* 推回输入流 PushbackReader 实现查找程序中"//字符型 char"字符串,
* 找到之后只是打印目标串之前的内容。
*
* @Author sxj
*
*/
publicclass PushbackReaderDemo {
publicstaticvoid main(String[] args) throws IOException {
// 创建字符输入流
Reader reader1 = new FileReader("E:/Java课件/HelloWorld.java");
// 创建推回输入流指定推回缓冲区大小为64,
//如果不指定,默认缓冲区大小为1
PushbackReader reader = new PushbackReader(reader1, 64);
char[] cs = newchar[32];// 临时数组
int count = 0;// 读取的字符数
String strLast = "";// 记录上次读取的字符串
while ((count = reader.read(cs, 0, cs.length)) != -1) {
// 本次读取内容
String str = new String(cs, 0, count);
// 上次+本次读取内容 -- 避免要查找的字符串被截断
String StrContent = strLast + str;
// 查找目标字符串
// 目标出现位置
int targetIndex = StrContent.indexOf("//字符型 char");
if (targetIndex > -1) {
// 找到目标字符串
// 将本次内容和上次内容一起推回缓冲区
// *****推回缓冲区的内容大小不能超过缓冲区的大小
reader.unread(StrContent.toCharArray());
// 判断 targetIndex 是否 > 32(临时数组大小)
if (targetIndex > 32) {
cs = newchar[targetIndex];
}
// 再次读取指定长度的内容(就是目标字符串之前的内容)
reader.read(cs, 0, targetIndex);
str = new String(cs, 0, targetIndex);
System.out.print(str);
break;
} else {
// 为找到目标字符串
// 直接输出
System.out.print(strLast);
strLast = str;
}
}
}
}
示例代码执行结果:
二、访问数组
(1)ByteArrayOutputStream和ByteArrayInputStream类是在创建它的实例时,程序内部创 建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的实例向数组中写入或读出byte型数据。在网络传输中我们往往要传输很多变量,我们可以利用ByteArrayOutputStream把所有的变量收集到一起,然后一次性把数据发送出去
(2)ByteArrayOutputStream:可以捕获内存缓冲区的数据,转换成字节数组。
(3)ByteArrayInputStream:可以将字节数组转化为输入流
(4)CharArrayReader、CharArrayWriter的使用与ByteArrayOutputStream、 ByteArrayInputStream差不多。
示例代码:
/**
* 访问 byte[] 和 char[]的IO流
* ByteArrayInputStream ByteArrayOutputStream
* CharArrayReader CharArrayWriter
*
* @author sxj
*
*/
publicclass ArrayStreamDemo {
publicstaticvoid main(String[] args) throws IOException {
String str1="访问 byte[] 和 char[]的IO流 ;";
String str2="ByteArrayInputStreamByteArrayOutputStream;";
String str3="CharArrayReaderCharArrayWriter;";
//ByteArrayInputStreamByteArrayOutputStream
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(str1.getBytes());
out.write(str2.getBytes());
out.write(str3.getBytes());
//转换为byte数组
byte[] buff = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(buff);
byte[] bs=newbyte[1024];
int count=0;
while ((count = in.read(bs,0,bs.length)) != -1) {
System.out.println(new String(bs,0,count));
}
//ByteArrayInputStreamByteArrayOutputStream 不需要关闭流
// out.close();
// in.close();
System.out.println("---------------");
//CharArrayReader CharArrayWriter
CharArrayWriter writer=new CharArrayWriter();
writer.write(str1);
writer.write(str2);
writer.write(str3);
char[] chars=writer.toCharArray();
CharArrayReader reader=new CharArrayReader(chars);
char[] cs=newchar[200];
while((count=reader.read(cs, 0, cs.length))!=-1){
System.out.println(new String(cs,0,count));
}
//CharArrayReader CharArrayWriter 不需要关闭流
}
}
示例代码执行结果:
(1)一般情况下使用输入流从磁盘,网络或者其它的物理介质读取数据都是顺序读取的,在流的内部会维护一个指针,读取数据的同时,指针会向后移动,直到读完为止。
(2)在一些实际常见中,如果读出来的数据不是想要的又不能再放回去怎么办?可以使用io提供的推回输入流。使用普通的IO输入流如果读取到不想要的数据,只能在程序里面处理掉,而使用IO里面的推回输入流读取数据则可以把数据给推回到输入流的缓冲区中。
(3)推回输入流主要为两个类:PushbackInputStream和PushbackReader
示例代码:
/**
* 推回输入流 PushbackReader 实现查找程序中"//字符型 char"字符串,
* 找到之后只是打印目标串之前的内容。
*
* @Author sxj
*
*/
publicclass PushbackReaderDemo {
publicstaticvoid main(String[] args) throws IOException {
// 创建字符输入流
Reader reader1 = new FileReader("E:/Java课件/HelloWorld.java");
// 创建推回输入流指定推回缓冲区大小为64,
//如果不指定,默认缓冲区大小为1
PushbackReader reader = new PushbackReader(reader1, 64);
char[] cs = newchar[32];// 临时数组
int count = 0;// 读取的字符数
String strLast = "";// 记录上次读取的字符串
while ((count = reader.read(cs, 0, cs.length)) != -1) {
// 本次读取内容
String str = new String(cs, 0, count);
// 上次+本次读取内容 -- 避免要查找的字符串被截断
String StrContent = strLast + str;
// 查找目标字符串
// 目标出现位置
int targetIndex = StrContent.indexOf("//字符型 char");
if (targetIndex > -1) {
// 找到目标字符串
// 将本次内容和上次内容一起推回缓冲区
// *****推回缓冲区的内容大小不能超过缓冲区的大小
reader.unread(StrContent.toCharArray());
// 判断 targetIndex 是否 > 32(临时数组大小)
if (targetIndex > 32) {
cs = newchar[targetIndex];
}
// 再次读取指定长度的内容(就是目标字符串之前的内容)
reader.read(cs, 0, targetIndex);
str = new String(cs, 0, targetIndex);
System.out.print(str);
break;
} else {
// 为找到目标字符串
// 直接输出
System.out.print(strLast);
strLast = str;
}
}
}
}
示例代码执行结果:
二、访问数组
(1)ByteArrayOutputStream和ByteArrayInputStream类是在创建它的实例时,程序内部创 建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的实例向数组中写入或读出byte型数据。在网络传输中我们往往要传输很多变量,我们可以利用ByteArrayOutputStream把所有的变量收集到一起,然后一次性把数据发送出去
(2)ByteArrayOutputStream:可以捕获内存缓冲区的数据,转换成字节数组。
(3)ByteArrayInputStream:可以将字节数组转化为输入流
(4)CharArrayReader、CharArrayWriter的使用与ByteArrayOutputStream、 ByteArrayInputStream差不多。
示例代码:
/**
* 访问 byte[] 和 char[]的IO流
* ByteArrayInputStream ByteArrayOutputStream
* CharArrayReader CharArrayWriter
*
* @author sxj
*
*/
publicclass ArrayStreamDemo {
publicstaticvoid main(String[] args) throws IOException {
String str1="访问 byte[] 和 char[]的IO流 ;";
String str2="ByteArrayInputStreamByteArrayOutputStream;";
String str3="CharArrayReaderCharArrayWriter;";
//ByteArrayInputStreamByteArrayOutputStream
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(str1.getBytes());
out.write(str2.getBytes());
out.write(str3.getBytes());
//转换为byte数组
byte[] buff = out.toByteArray();
ByteArrayInputStream in = new ByteArrayInputStream(buff);
byte[] bs=newbyte[1024];
int count=0;
while ((count = in.read(bs,0,bs.length)) != -1) {
System.out.println(new String(bs,0,count));
}
//ByteArrayInputStreamByteArrayOutputStream 不需要关闭流
// out.close();
// in.close();
System.out.println("---------------");
//CharArrayReader CharArrayWriter
CharArrayWriter writer=new CharArrayWriter();
writer.write(str1);
writer.write(str2);
writer.write(str3);
char[] chars=writer.toCharArray();
CharArrayReader reader=new CharArrayReader(chars);
char[] cs=newchar[200];
while((count=reader.read(cs, 0, cs.length))!=-1){
System.out.println(new String(cs,0,count));
}
//CharArrayReader CharArrayWriter 不需要关闭流
}
}
示例代码执行结果: