OutputStreamWriter类:
OutputStreamWriter 是字符流通向字节流的桥梁
可使用指定的字符编码表,将要写入流中的字符编码成字节
public static void main(String[] args) throws IOException {
//明确目的地 FileOutputStream fos=new FileOutputStream("D:\\java\\a.txt"); //创建转换流 OutputStreamWriter osw=new OutputStreamWriter(fos, "UTF-8"); //写入字符 osw.write("你好"); osw.flush(); //释放资源 osw.close(); }InputStreamReader类
InputStreamReader 是字节流通向字符流的桥梁
它使用指定的字符编码表读取字节并将其解码为字符
public static void main(String[] args) throws IOException {
//明确数据源 FileInputStream fis=new FileInputStream("D:\\java\\a.txt"); //创建转换流 InputStreamReader isr=new InputStreamReader(fis,"UTF-8"); char[] c=new char[1024]; int len=0; while((len=isr.read(c))!=-1){ System.out.println(new String(c,0,len)); } isr.close(); }
转换流和子类区别:
OutputStreamWriter: UTF-8 GBK
?FileWriter: GBK
OutputStreamWriter和InputStreamReader是字符和字节的桥梁:也可以称之为字符转换流。字符转换流原理:字节流+编码表。
缓冲流:
字节缓冲流:
字节缓冲输出流 BufferedOutputStream:
通过字节缓冲流,进行文件的读写操作 写数据到文件的操作
public static void main(String[] args) throws IOException {
//明确目的地 FileOutputStream fos=new FileOutputStream("D:\\java\\hello.txt"); //给字节输出流添加缓冲区 BufferedOutputStream bos=new BufferedOutputStream(fos); //写入字节 bos.write("abc123".getBytes()); //释放资源 bos.close(); }
字节缓冲输入流 BufferedInputStream:
public static void main(String[] args) throws IOException {
//数据源 FileInputStream fis=new FileInputStream("D:\\java\\hello.txt"); //缓冲流 BufferedInputStream bis=new BufferedInputStream(fis); int len=0; while((len=bis.read())!=-1){ System.out.print((char)len); } bis.close(); }复制文件
public static void main(String[] args) throws IOException {
//数据源(字节)(单个) FileInputStream fis=new FileInputStream("D:\\software\\eclipse\\a.txt"); BufferedInputStream bis=new BufferedInputStream(fis); //目的地 FileOutputStream fos=new FileOutputStream("D:\\software\\eclipse\\b.txt"); BufferedOutputStream bos=new BufferedOutputStream(fos); int len=0; Date d1=new Date(); System.out.println(d1.getTime()); while((len=bis.read())!=-1){ bos.write(len); } //释放资源 bis.close(); bos.close(); Date d2=new Date(); System.out.println(d2.getTime()); System.out.println(d2.getTime()-d1.getTime()); }public static void main(String[] args) throws IOException { //数据源(数组) FileInputStream fis=new FileInputStream("D:\\software\\eclipse\\a.txt"); BufferedInputStream bis=new BufferedInputStream(fis); //目的地 FileOutputStream fos=new FileOutputStream("D:\\software\\eclipse\\c.txt"); BufferedOutputStream bos=new BufferedOutputStream(fos); byte[] bytes=new byte[1024]; int len=0; Date d1=new Date(); System.out.println(d1.getTime()); while((len=bis.read(bytes))!=-1){ bos.write(bytes, 0, len); } //释放资源 bis.close(); bos.close(); Date d2=new Date(); System.out.println(d2.getTime()); System.out.println(d2.getTime()-d1.getTime()); }字符缓冲流:
字符缓冲输入流 BufferedReader:
将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入
public static void main(String[] args) throws IOException {
//数据源 FileReader fr=new FileReader("D:\\java\\hello.txt"); //缓冲流 BufferedReader br=new BufferedReader(fr); String len=null; while((len=br.readLine())!=null){ System.out.println(len); } br.close(); }字符缓冲输出流 BufferedWriter:
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取
public static void main(String[] args) throws IOException {
//目的地 FileWriter fw=new FileWriter("D:\\java\\hello.txt",true); //缓冲流 BufferedWriter bw=new BufferedWriter(fw); bw.write("你好啊熊大"); //换行 bw.newLine(); bw.write("我很好光头强"); //释放资源 bw.close(); }readLine():每次读取一行,如果已到达流末尾,则返回 null
public static void main(String[] args) throws IOException {
//数据源 FileReader fr=new FileReader("D:\\java\\hello.txt"); BufferedReader br=new BufferedReader(fr); //目的地 FileWriter fw=new FileWriter("D:\\java\\nihao.txt"); BufferedWriter bw=new BufferedWriter(fw); String line=null; //开始复制 while((line=br.readLine())!=null){ bw.write(line); bw.newLine(); bw.flush(); } //释放资源 br.close(); bw.close(); }
复制文件:
public static void main(String[] args) throws IOException {
//数据源(字符)(单个) FileReader fr=new FileReader("D:\\software\\eclipse\\a.txt"); BufferedReader br=new BufferedReader(fr); //目的地 FileWriter fw=new FileWriter("D:\\software\\eclipse\\d.txt"); BufferedWriter bw=new BufferedWriter(fw); int len=0; Date d1=new Date(); System.out.println(d1.getTime()); while((len=br.read())!=-1){ bw.write(len); bw.flush(); } br.close(); bw.close(); Date d2=new Date(); System.out.println(d2.getTime()); System.out.println(d2.getTime()-d1.getTime()); }public static void main(String[] args) throws IOException { //数据源(数组) FileReader fr=new FileReader("D:\\software\\eclipse\\a.txt"); BufferedReader br=new BufferedReader(fr); //目的地 FileWriter fw=new FileWriter("D:\\software\\eclipse\\e.txt"); BufferedWriter bw=new BufferedWriter(fw); char[] c=new char[1024]; int len=0; Date d1=new Date(); System.out.println(d1.getTime()); while((len=br.read(c))!=-1){ bw.write( c, 0, len); bw.flush(); } //释放资源 br.close(); bw.close(); Date d2=new Date(); System.out.println(d2.getTime()); System.out.println(d2.getTime()-d1.getTime()); } public static void main(String[] args) throws IOException { //数据源(行) FileReader fr=new FileReader("D:\\software\\eclipse\\a.txt"); BufferedReader br=new BufferedReader(fr); //目的地 FileWriter fw=new FileWriter("D:\\software\\eclipse\\f.txt"); BufferedWriter bw=new BufferedWriter(fw); String line=null; Date d1=new Date(); System.out.println(d1.getTime()); while((line=br.readLine())!=null){ bw.write(line); bw.flush(); } //释放资源 br.close(); bw.close(); Date d2=new Date(); System.out.println(d2.getTime()); System.out.println(d2.getTime()-d1.getTime()); }流的操作规律:
明确一:要操作的数据是数据源还是数据目的。
明确二:要操作的数据是字节还是文本。
明确三:明确数据所在的具体设备。
明确四:是否需要额外功能。