1. 转换流(把字符流转成了字节流所以是桥梁):OutputStreamWriter;是字符输出流;先用FileOutputStream确定要写入的地址;然后再用转换流抓取要写入的地址和码表
2.InputStreamReader:是FileReader的父类并且是它的转换流
3.InputStreamReader和OutputStreamWriter的转换原理(字节流+码表)
4.字符转成字节后再写入:明确目的地:FileOutputStream fos=new FileOutputStream("地址");
创建转换流对象(括号里面传的是字节流对象和码表,这里起到写入的还是字节流对象,转换流只是借用码表进行了翻译):OutputStreamWriter osw=new OutputStreamWriter (fos,utf-8);
osw.writer("写入的内容")这里虽然是转换流对象ows调用的.write()方法但实际上还是字节流调用的.writer()方法进行写入
osw.close;
写入转换流:先new一个字节流对象确定要写入的地址FileOutputWriter,再确定转换流对象确定码表并把字节流对象一起传递,OutputStreamWriter(字节对象,码表)
然后再用转换流对象调用.writer()方法,这里虽然是转换流对象调用写入方法但实际上还是字节流调用的.writer(),最后关闭资源,直接关闭转换流对象就行,字节流跟随转换流对象 一起关闭
5.读取转换流:明确数据源,要读取的地址:FileInputStream fis=new FileInputStream("读取的地址");
创建读取转换流对象:InputStreamReader isr=new InputStreamReader(fis,utf-8); 创建char数组确定一次要读取的个数char ch=new char[1024];确定判定变量:int len=0;
开始读取:while((len=isr.read())!=-1){system.out.println(new String(ch,0,len))} iso.cloese();
缓冲流:字节缓冲流:写入字节缓冲流:BufferedOutputStream;读取字节缓冲流:BufferedInputStream;
单个字节复制:FileOutputStream fos=new FileOutputStream("地址");FileInputStream fis=new FileInputStream("地址");
int len=0;whlie((len=fis.read())!=-1){fos.writer(len)};fis.cloes()‘fos.close();
单个字节缓冲流复制
FileOutputStream fos=new FileOutputStream("地址");FileInputStream fis=new FileInputStream("地址");
BufferedOutputStream bos=new BufferedOutputStream (fos); BufferedInputStream bis=new BufferedInputStream(fis);
int len=0;while((len=bis.read())!=-1){bos.write(len)};bos.close();bis.close();
一个字节数组复制:
FileOutputStream fos=new FileOutputStream(地址); FileInputStream fis=new FileInputStream("地址")
int len=0;byte[] bytes=new byte[1024];while((len=fis.read(bytes))!=-1){fos.write(bytes,0,len)};fis.close();fos.close();
缓冲流一个字节数组复制:
FileOutputStream fos=new FileOutputStream("地址") FileInputStream fis=new FileInputStream("地址")\
BufferedOutputStream bos=new BufferedOutputStream(fos);BufferedInputStream bis=new BufferedInputStream(fis);
int len=0;byte[] bytes=new byte[1024]; while((len=bis.read(bytes))!=-1){bos.write(bytes,0,len)}bis.close();bos.close();
缓冲流:字符缓冲流:
1.单个字符复制:FileWriter fw=new FileWriter("地址");FileReader fr=new FileReader("地址"); int len=0;while((len=fr.read())!=-1){fw.writer(len),fw.flush()}
2.缓冲流单个字节复制:FileWriter fw=new FileWriter("地址");FileReader fr=new FileReader("地址");BufferedWriter bw=new BufferedWriter(fw);
BufferedReader br=new BufferedReader(fr);String line=null;while((line=br,readline())!=null){bw.write(line);br.newline();br.flush()}br.close();bw.close();
3.一个字节数组复制:FileWriter fw=new FileWriter("地址");FileReader fr=new FileReader("地址");char ch=new char[1024];int len=0;
while((len=fr,read(ch))!=-1){fw.write(ch,0,len);fw.flush();};fr.close();fw.close();
4.缓冲流一个字节数组复制:FileWriter fw=new FileWriter("地址");FileReader fr=new FileReader("地址");BufferedWriter bw=new BufferedWriter(fw);
BufferedReader br=new BufferedReader(fr);String line=null;char ch=new char[1024];while((line=br.read(ch))!=-1){bw.write(ch,0,len);bw.flush();};br.close();
原文地址:https://www.cnblogs.com/hankai2735/p/11438797.html