java学习笔记30(IO :缓冲流)
生活随笔
收集整理的這篇文章主要介紹了
java学习笔记30(IO :缓冲流)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
緩沖流:
讀取數據大量的文件時,讀取的速度慢,java提供了一套緩沖流,提高IO流的效率;
緩沖流分為字節緩沖流和字符緩沖流;
字節輸入緩沖流和字節輸出緩沖流如下:
package com.zs.Demo;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class Demo {public static void main(String[] args) {try {fun();fun1();} catch (IOException e) {e.printStackTrace();}}//字節緩沖輸入流private static void fun1() throws IOException {//第二種創建緩沖流方式BufferedInputStream bis=new BufferedInputStream(new FileInputStream("d:\\a.txt"));int len=0;while((len=bis.read())!=-1){System.out.println((char)len);}bis.close();}//字節緩沖輸出流private static void fun() throws IOException {//第一種方式FileOutputStream f=new FileOutputStream("d:\\a.txt");BufferedOutputStream bos=new BufferedOutputStream(f);//寫入一個字節bos.write(105);bos.write("hello world".getBytes());//寫入字節數組bos.write("hello world".getBytes(),0,2);//寫入字節數組指定內容 bos.close();} }字符輸入緩沖流和輸出緩沖流如下:
package com.zs.Demo;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;public class Demo2 {public static void main(String[] args) {try {fun();fun1();} catch (IOException e) {e.printStackTrace();}}//字符輸出緩沖流private static void fun() throws IOException {BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\b.txt"));bw.write(111);//自動查碼表,編碼bw.flush();//注意字符輸出流每次操作都要刷新bw.write("hello world".toCharArray());//寫入字符數組 bw.flush();bw.newLine();//newLine()特有方法,換行 bw.flush();bw.write("java");//寫入字符串 bw.flush();bw.close();}//字符輸入緩沖流private static void fun1() throws IOException {//字符輸入緩沖流BufferedReader br=new BufferedReader(new FileReader("d:\\b.txt"));int len=0;while ((len=br.read())!=-1) {System.out.print((char)len);}br.close(); //創建字符數組輸入緩沖流對象BufferedReader br1=new BufferedReader(new FileReader("d:\\b.txt"));int len1=0;char[] c=new char[1024];while((len1=br1.read(c))!=-1){System.out.print(new String(c,0,len1));}br1.close();//字符輸入緩沖流特有的方法readLine() 一次讀取一行BufferedReader br2=new BufferedReader(new FileReader("d:\\b.txt"));String len3=null;//這里注意是字符串類型while((len3=br2.readLine())!=null){System.out.println(len3);}br2.close();} }下面寫一個比較字節流,字節數組流,字節緩沖流,字節數組緩沖流復制文件速度的代碼:
package com.zs.Demo;import java.io.*;public class Demo5 {public static void main(String[] args) {long start = System.currentTimeMillis();try {//這里一個一個方法的運行,比較時間,復制的文件是一個240M的視頻文件 // fun1("h:\\1.mp4","g:\\1.mp4");//字節流 // fun2("h:\\1.mp4","g:\\1.mp4");//字節數組流 // fun3("h:\\1.mp4","g:\\1.mp4");//字節緩沖流fun4("h:\\1.mp4","g:\\1.mp4");//字節數組緩沖流} catch (IOException e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println(end-start);}//字節數組緩沖流private static void fun4(String s, String s1) throws IOException {BufferedInputStream fi=new BufferedInputStream(new FileInputStream(s));BufferedOutputStream fo=new BufferedOutputStream(new FileOutputStream(s1));int len=0;byte[] b=new byte[1024*10];while((len=fi.read(b))!=-1){fo.write(b,0,len);}//1653毫秒 fo.close();fi.close();}//字節緩沖流private static void fun3(String s, String s1) throws IOException {BufferedInputStream fi=new BufferedInputStream(new FileInputStream(s));BufferedOutputStream fo=new BufferedOutputStream(new FileOutputStream(s1));int len=0;while((len=fi.read())!=-1){fo.write(len);}//13015毫秒 fo.close();fi.close();}//字節數組流private static void fun2(String s, String s1) throws IOException {FileInputStream fi=new FileInputStream(new File(s));FileOutputStream fo=new FileOutputStream(new File(s1));byte[] b=new byte[1024*10];int len=0;while((len=fi.read(b))!=-1){fo.write(b,0,len);}//6979毫秒 fo.close();fi.close();}//字節流private static void fun1(String s, String s1) throws IOException {FileInputStream fi=new FileInputStream(new File(s));FileOutputStream fo=new FileOutputStream(new File(s1));int len=0;while((len=fi.read())!=-1){fo.write(len);}//太慢了,等不下去了,時間十分鐘以上 fo.close();fi.close();} }可以看出字節數組緩沖流速度最快,
轉載于:https://www.cnblogs.com/Zs-book1/p/10600290.html
總結
以上是生活随笔為你收集整理的java学习笔记30(IO :缓冲流)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oppo设备怎么样无需root激活XPO
- 下一篇: 十种常用编程语言特点