01.定义
02.IO中的输入和输出的划分
03.流的分类
IO流的所有类中,最先分野的是字节流和字符流。
字节流包括:输入流和输出流
InputStream
public abstract class InputStream implements Closeable {
}
OutputStream
public abstract class OutputStream implements Closeable, Flushable {
}
都是抽象类
字符流包括:Reader 和writer
Reader
public abstract class Reader implements Readable, Closeable {
}
Writer
public abstract class Writer implements Appendable, Closeable, Flushable {
}
也都是抽象类
IO流和文件之间的区别:
文件就好像是物流中心的物品
IO流就好像是外卖小哥
04.字节流中的inputstream
各种输入流子类和父类,接口的关系:
FileInputStream
public class FileInputStream extends InputStream
{
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
}
BufferedInputStream :
public
class BufferedInputStream extends FilterInputStream {
}
public class FilterInputStream extends InputStream {
/**
* The input stream to be filtered.
*/
protected volatile InputStream in;//内置了一个inputstream的输入流
}
FileInputStream 的方法:
public void fileinputstream01() throws IOException {
//读数据的文件的地址
String filepath="d:\\news1.txt";
//创建一个输入流
FileInputStream fileInputStream=null;
//read函数的标识位和数据接受
int readlen=0;
//用输入流构造函数把文件和输入流联合
fileInputStream=new FileInputStream(filepath);
//fileInputStream的读函数,read(),用readlen来接受read()数据,没有数据的read函数会返回一个-1
while((readlen = fileInputStream.read())!=-1){
System.out.println((char)readlen);
}
fileInputStream.close();
public void fileOutputStream01() throws IOException {
//写数据的目标文件的地址
String destpath="d:\\hello.txt";
//创建一个输出流
FileOutputStream fileOutputStream=null;
//用输出流构造函数把文件和输出流联合
fileOutputStream=new FileOutputStream(destpath);
//要写的字符串
String str="hello ,nihao";
//写入数据
fileOutputStream.write(str.getBytes());
fileOutputStream.close();
}
FileOutputStream 和 FileInputStream 联合
例子:
public void fileOutputStream01() throws IOException {
//读数据的文件的地址
String srctpath="d:\\text.txt";
//写数据的目标文件的地址
String destpath="d:\\hello.txt";
//用输入流构造函数把文件和输入流联合
FileInputStream fileInputStream=new FileInputStream(srctpath);
//用输出流构造函数把文件和输出流联合
FileOutputStream fileOutputStream=new FileOutputStream(destpath);
//read函数的标识位和数据接受的长度
int readlen=0;
//缓存
byte[] bytes=new byte[1024];
//fileInputStream的这个读函数,read(byte[]),返回的是读入缓冲区的总字节数,如果由于已到达流的末尾而没有更多数据,则返回-1。
while((readlen = fileInputStream.read(bytes))!=-1){
//写函数
fileOutputStream.write(bytes,0,readlen);
}
fileOutputStream.close();
}
用到的方法:
01.
FileReader.read(bytes[])
将字符读入数组。 返回:读取的字符数,或-1如果流的末端已经到达。
02.
new String(buf,0,readlen)
分配一个新的字符串,它包含字符数组参数的子数组中的字符。
偏移量参数是子数组的第一个字符的索引,计数参数指定子数组的长度。
子数组的内容被复制;
后续的字符数组的修改不会影响新创建的字符串。
参数:
value - 字符数组的源 offset - 初始偏移量 count - 长度
抛出:
IndexOutOfBoundsException - 如果偏移量和计数参数索引字符超出了值数组的边界
public void file01() throws IOException {
//读取数据的文件路径
String filepath="d:\\text.txt";
//创建读对象
FileReader fileReader=null;
//接收读取数据时候读到的数据长度
int readlen=0;
//存放数据的数组
char[] bytes = new char[8];
fileReader=new FileReader (filepath);
//将字符读入数组。 返回:读取的字符数,或-1如果流的末端已经到达。
while((readlen = fileReader.read(bytes))!=-1){
//分配一个新的字符串,它包含字符数组参数的子数组中的字符。
//偏移量参数是子数组的第一个字符的索引,计数参数指定子数组的长度。
//子数组的内容被复制;
System.out.println(new String(bytes,0,readlen));
}
fileReader.close();
}
public void FileWriter01() throws IOException {
String destpath="d:\\writethings.txt";
FileWriter fileWriter=null;
fileWriter=new FileWriter(destpath);
fileWriter.write("你好");
//关闭才能写入,因为close方法内含有frush方法,frush方法才是将内存中的内容传到磁盘
fileWriter.close();
}
节点流:
处理流:
例子:
例子:
代理模式
01.
总抽象类
public abstract class Read_ {
public void readFile(){};
public void readString(){};
}
02.抽象类的实现类
public class StringRead_ extends Read_{
public void readString(){
System.out.println("读字符串函数运行");
}
}
public class FileRead_ extends Read_{
public void readFile(){
System.out.println("读文件函数运行");
}
}
03.包装类
public class BuffRead_ extends Read_{
Read_ read;
public BuffRead_(Read_ read) {
this.read = read;
}
public void readFile(){
read.readFile();
};
public void readString(){
read.readString();
};
public void readStrings(int num) {
for (int i=0;i<num;i++){
read.readString();
}
}
public void readFiles(int num) {
for (int i=0;i<num;i++){
read.readFile();
}
}
}