功能流 : 使用:包裹节点流使用
缓冲流 Buffered : 加快节点流的读写效率(字节缓冲流 :(字节输入缓冲流 BufferedInputStream;字节输出缓冲流 BufferedOutputStream);字符缓冲流)
字符流缓冲流:
1、字符输入缓冲流 BufferedReader( 新增功能: String readLine() 读一行文字。)
2、字符输出缓冲流 BufferedWriter(新增功能: void newLine() 写一个行分隔符。)
存在新增功能,不能发生多态
功能流 :
Data流 | 基本数据类型流 : 读写数据+保留数据类型->基本数据类型|字符串
是字节流的功能流
DataInputStream------Data输入流(新增功能 : readXxx())
DataOuutputStream----Data输出流(新增功能 : writeXxx())
注意: 1、读入与写出的顺序保持一致;2、读入数据的时候必须从源文件中读入,否则抛出异常 java.io.EOFException
public class Class001_BufferedInputStream {
public static void main(String[] args) throws IOException {
//1.构建流
InputStream is = new BufferedInputStream(new FileInputStream("D://test.txt"));
OutputStream os = new BufferedOutputStream(new FileOutputStream("D://test1.txt"));
//2.读入写出
byte[] car = new byte[1024];
int len = -1;
while((len = is.read(car))!=-1){
os.write(car,0,len);
}
//3.刷出
os.flush();
//4.关闭
os.close();
is.close();
}
}
public class Class002_BufferedReader {
public static void main(String[] args) throws IOException {
//1.构建流(输入,输出)
BufferedReader rd = new BufferedReader(new FileReader("D://test.txt"));
BufferedWriter rt = new BufferedWriter(new FileWriter("D://dest.txt"));
//2.读写
String msg = null;
while((msg=rd.readLine())!=null){
rt.write(msg);
//rt.newLine(); //换行符
}
//3.刷出
rt.flush();
//4.关闭
rt.close();
rd.close();
}
}
public class Class003_DataInputStream {
public static void main(String[] args) throws IOException {
read("D://test1.txt");
}
//写出
public static void wirte(String path) throws IOException {
//1.构建输出流
DataOutputStream os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
//2.准备数据
int i = 100;
char ch = 'a';
boolean flag = false;
String str = "哈哈";
//3.写出
os.writeInt(i);
os.writeChar(ch);
os.writeBoolean(flag);
os.writeUTF(str);
//4.刷出
os.flush();
//5.关闭
os.close();
}
//读入
public static void read(String path) throws IOException {
//1.构建输入流
DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
//2.读入
int i = is.readInt();
char ch = is.readChar();
boolean flag = is.readBoolean();
String msg = is.readUTF();
//3.处理数据
System.out.println(i);
System.out.println(ch);
System.out.println(flag);
System.out.println(msg);
//4.关闭
is.close();
}
}