小白学Java18:IO流(下):字节流,字符流,转换流和对象流

字节流

父类:字节输入流和字节输出流

  • InputStream:所有字节输入流的父类
  • OutputStream:所有字节输出流的父类

低级流:文件字节流

文件字节输入流:FileInputStream

  • 构造方法
FileInputStream fis = new FileInputStream(File);
FileInputStream fis = new FileInputStream(puth);
  • 常用方法
int read():读取一字节的数据并返回
    int read(byte[] b );读取一定量的数据到给定的字节数组中,并且返回读到的长度
    int read(byte[],int off,int len) 读取一定量的数据到给定的字节数组中,并且指定从数组的off位置处写入指定len字节的数据

文件字节输出流:FileOutputStream

  • 构造方法
FileOutputStream fos = new FileOutputStream(File/puth);
FileOutputStream fos = new FileOutputStream(File/puth,boolean append);//true:追加写
  • 常用方法
write(int d) 写出一字节的数据
    write(byte[])写出给定字节数组中的所有数据
    write(byte[]byte,int ofer,int len)从给定字节数组的指定位置出写出len个字节的数据

高级流:缓冲字节流

字节缓冲输入流:BufferedInoutStream

  • 构造方法
BufferedInputStream bis = new BufferedInputStream(InputStream is);

字节缓冲输出流:BufferedOutoutStream

  • 构造方法
BufferedOutputStream bos = new BufferedOutputStream(OutputStream os);
  • 常用方法:
bos.flush();
就算缓冲区没有满,直接写出数据,在关流操作的时候自动调用

使用缓冲流来实现文件的复制

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src/测试.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src/测试01.txt"));
		
byte[] b = new byte[1024*10];
int len = -1;
while((len = bis.read(b))!=-1) {
	bos.write(b,0,len);
	}
	bos.close();
	bis.close();
	}

字符流

父类:字符输入流和字符输出流

  • Reader:所有字符输入流的父类
  • Writer:所有字符输出流的父类

文件字符输入流:FileReader:继承自InputStreamReader

  • 构造方法
FileReader fr = new FileReader(File/path);
  • 常用方法
int read():返回一字符的数据,以int的形式返回
    read(char[] chbuffer):将数据读到给定的字符数组中,并返回读到的长度

文件字符输出流:FileWriter:继承自OutputStreamWrite

  • 构造方法
FileWrite fw = new Filewrite(File/path);
FileWrite fw = new FileWrite(File/path,boolean,append);//追加写操作
  • 常用方法
write(String) ;允许直接写入String

使用文件字符流实现文件的复制

FileReader fr = new FileReader("src/测试.txt");
FileWriter fw = new FileWriter("src/测试01.txt");
char[] c = new char[10];
int len =-1;
while((len = fr.read(c))!=-1) {
	fw.write(c, 0, len);
	}
fw.close();
fr.close();

字符缓冲输入流:BufferedReader

  • 构造函数
FileInputStream fis = new FileInputStream(path);//创建文件输入流
InputStreamReader isr = new InputStreamReader(fis);//创建转换流
BufferedReader br = new BufferedReader(isr);//创建字符缓冲输入流

字符缓冲输出流:BufferedWriter

  • 构造函数
FileOutputStream fos = new FileOutputStream(path);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
String str ="测试字符缓冲输出流";
bw.write(str);
bw.flush();
System.out.println("写入成功");

另一种缓冲输出流:PrintWrite

  • 构造函数
PrintWrite pw = new PrintWrite(Paht/file/OutputStream)
PrintWrite pw = new PrintWrite(Paht/file/OutputStream,boolean,autoFlush)//带自动行刷新
  • 常用方法
println():带自动行刷新的写入
    print():不带自动行刷新的写入

转换流

字符流和字节流相互转换的桥梁

字符输入转换流:InputStreamReader:

字节流——————>字符流

  • 构造函数
FileInputStream fis = new FileInputStream("path"/file);
FileInputStream fis = new FileInputStream("path"/file,指定编码格式)

字符输出转换流:OutputStreamWrite

字符流——————>字节流

FileOutputStream fos = new FileOutputStream("src\\day0324\\cf2.txt",true);
OutputStreamWriter osw = new OutputStreamWriter(fos);

测试使用转换流读写数据

		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("src/a.txt"));
		osw.write("测试使用转换流写入数据");
		osw.close();
		
		InputStreamReader isr = new InputStreamReader(new FileInputStream("src/a.txt"));
		char[] c = new char[100];
		int len = isr.read(c);
		isr.close();
		System.out.println(new String(c,0,len));

对象流

将某个类的实例以对象的形式存入磁盘

对象输入流:ObjectInputStream

ObjectInputStream ois = new ObjectInputStream(InputStream in);

对象输出流:ObjectOutputStream

ObjectOutputStream oos = new ObjectOutputStream(OutputStream out)

对象的序列化:Serializable接口

将对象转成一组字节数组的过程叫做对象序列化

被写入文件的对象的类一定要实现Serializable接口,用于在类中添加一个不可变的序列版本号,还原的时候将会检测,如果版本号不一致则还原失败

  • 实现该接口后,会在类内部生成一个私有的serialVersionUID(序列化版本号)
private static final long serialVersionUID = XXXL;

关键字:transient

被该关键字修饰的类属性,在序列化的时候会被忽略,达到瘦身的目的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值