期待与您交流! ———-
1:字节流(1)继承体系InputStreamFileInputStreamOutputStream FileOutputStream2:高效流(1)字符高效流BufferedReaderBufferedWriter(2)字节高效流BufferedInputStreamBufferedOutputStream(3)字符高效流的特殊功能BufferedReader String readLine()BufferedWriter void newLine()
3:基本字节流和高效字节流的比较:
package com.itheima;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Test11 {public static void main(String[] args) throws IOException {long start = System.currentTimeMillis();method1("C:\\雪.mkv", "test.mkv");long end = System.currentTimeMillis();System.out.println("基本字节流一次读写一个字节共耗时:" + (end – start) + "毫秒");start = System.currentTimeMillis();method2("C:\\雪.mkv", "test.mkv");end = System.currentTimeMillis();System.out.println("基本字节流一次读写一个字节数组共耗时:" + (end – start) + "毫秒");start = System.currentTimeMillis();method3("C:\\雪.mkv", "test.mkv");end = System.currentTimeMillis();System.out.println("高效字节流一次读写一个字节共耗时:" + (end – start) + "毫秒");start = System.currentTimeMillis();method4("C:\\雪.mkv", "test.mkv");end = System.currentTimeMillis();System.out.println("高效字节流一次读写一个字节数组共耗时:" + (end – start) + "毫秒");}// 高效字节流一次读写一个字节数组private static void method4(String srcString, String destString)throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));byte[] bys = new byte[1024];int len = 0;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();bis.close();}// 高效字节流一次读写一个字节private static void method3(String srcString, String destString)throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));int by = 0;while ((by = bis.read()) != -1) {bos.write(by);}bos.close();bis.close();}// 基本字节流一次读写一个字节数组private static void method2(String srcString, String destString)throws IOException {FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(destString);byte[] bys = new byte[1024];int len = 0;while ((len = fis.read(bys)) != -1) {fos.write(bys, 0, len);}fos.close();fis.close();}// 基本字节流一次读写一个字节private static void method1(String srcString, String destString)throws IOException {FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(destString);int by = 0;while ((by = fis.read()) != -1) {fos.write(by);}fos.close();fis.close();}}运行后的结果:基本字节流一次读写一个字节共耗时:95580毫秒基本字节流一次读写一个字节数组共耗时:153毫秒高效字节流一次读写一个字节共耗时:375毫秒高效字节流一次读写一个字节数组共耗时:127毫秒
4:缓冲的字符流(BufferedReader/BufferedWriter)
1、采用缓冲处理是为了提高效率,如果没有缓存,例如FileReader对象,每次调用read()方法进行读操作时,都会直接去文件中读取字节,转换成字符并返回,这样频繁的读 取文件效率很低。
2、缓冲的字符流的出现提高了对流的操作效率,原理就是将数组进行封装。
3、在使用缓冲的字符流对象时,,缓冲的存在是为了增强流的功能,因此在建立缓冲的字符流对象时,要先有流对象的存在。
BufferedReader的特有方法:public String readLine();//一次读一行,到行标记时,将行标记之前的字符数据作为字符串返回。当读到末尾时,返回null。
BufferedWriter的特有方法:publicvoid newLine();//写出平台相关的行分隔符来标记一行的终止。Windows平台下为’\n’。
public class CopyFileDemo {public static void main(String[] args) throws IOException {// 封装数据源BufferedReader br = new BufferedReader(new FileReader("BufferedReaderDemo.java"));// 封装目的地BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java"));// 读写String line = null;while ((line = br.readLine()) != null) {bw.write(line);bw.newLine();bw.flush();}// 释放资源bw.close();br.close();}}
版权声明:本文为博主原创文章,未经博主允许不得转载。
没有口水与汗水,就没有成功的泪水。