JAVA之经常使用I/O操做

在java中,能够从中读入一个字节序列的对象叫作输入流,而能够向其中下入一个字节序列的对象称做输出流。这些字节序列的来源地和目的地一般是文件,但也能够是网络链接,甚至是内存块。java

 

其余编程语言的流类库的缓冲机制和预览细节等都是自动处理的。Java须要将多个流过滤器组合以构建真正有用的流序列能力,它有极大的灵活性。编程

 

缓冲区的做用是在输入流、输出流在写入写出时,不会每次都对设备进行访问。当缓冲区为空时,会读入新的数据块;当缓冲区满或被Flush时,数据被写出。数组

 

    字节流能够处理全部类型数据,如图片、视频等;字符流只能处理纯文本网络

 

读取操做编程语言

一、Readerspa

(1)BufferedReader3d

从字符流中读取文本,缓冲各个字符,实现高效率读取。通常包装FileReader、InputStreamReader等read开销高的类,若没有缓冲区,则read操做会读取字节并转换字符返回,效率低效。缓冲区的大小能够指定,但通常默认够用。code

(2)InputSreamReader视频

是字节流通向字符流的桥梁,read方法在底层输入流中转换一个或多个字节,要想实现有效的转换,外层需套上BufferedReader。它能够指定字符集,不然使用平台默认字符集。对象

(3)FileReader

用来读取字符文件。

二、InputStream

(1)从文件中读取原始字节流,能够使文本、图像、视频等格式。

(2)BufferedInputStream

继承FilterInputStream,提供缓冲输入流功能,在建立BufferedInputStream时,会建立一个内部缓冲数组,默认为8M。与FileInputStream相比,一样的read方法,执行效率却更快。

 

读取操做用例

 

 1 import java.io.BufferedInputStream;  2 import java.io.BufferedReader;  3 import java.io.FileInputStream;  4 import java.io.FileNotFoundException;  5 import java.io.FileReader;  6 import java.io.IOException;  7 import java.io.InputStream;  8 import java.io.InputStreamReader;  9 
10 public class IO读取用例 { 11 
12     public static void main(String[] args) throws IOException { 13         // TODO Auto-generated method stub
14 
15         BufferedReader br=null; 16         BufferedInputStream bis=null; 17         try { 18             //字符流 19             //读取字符流时,只有BufferedReader拥有ReadLine()方法 20             //方式一:直接读取
21             FileReader fr=new FileReader("test.txt"); //2848KB
22             br=new BufferedReader(fr); 23             
24             long t=System.currentTimeMillis(); 25             if(br.ready()){ 26                 while((br.readLine())!=null){ 27                     //TODO:
28  } 29  } 30             t=System.currentTimeMillis()-t; 31             
32             System.out.println("FileReader用时"+t);//50s 33             
34             //方式二:原始字节流转换读取
35             InputStream in=new FileInputStream("test.txt"); 36             InputStreamReader isr=new InputStreamReader(in); //转换
37             br=new BufferedReader(isr); 38             
39             long t2=System.currentTimeMillis(); 40             if(br.ready()){ 41                 while((br.readLine())!=null){ 42                     //TODO:
43  } 44  } 45             t2=System.currentTimeMillis()-t2; 46             
47             System.out.println("FileInputStream用时"+t2); //28s 48             
49             //字节流 50             //方式一:流直接读取
51             InputStream fis=new FileInputStream("test.txt"); 52             byte[] b=new byte[1024]; //存储读取数组
53             
54             long t3=System.currentTimeMillis(); 55             if(fis.available()>0){ 56                 while((fis.read(b))!=-1){ 57                     //TODO:
58  } 59  } 60             t3=System.currentTimeMillis()-t3; 61             System.out.println("BufferedInputStream用时"+t3);//6s 62             
63             //方式二:流套上buffered缓冲读取
64             bis=new BufferedInputStream(new FileInputStream("test.txt")); 65             b=new byte[1024]; 66             
67             long t4=System.currentTimeMillis(); 68             if(bis.available()>0){ 69                 while((bis.read(b))!=-1){ 70                     //TODO:
71  } 72  } 73             t4=System.currentTimeMillis()-t4; 74             System.out.println("BufferedInputStream用时"+t4);//3s
75             
76         } catch (FileNotFoundException e) { 77  e.printStackTrace(); 78         } finally{ 79  br.close(); 80  bis.close(); 81  } 82         
83  } 84 
85 }

 

 

 

 

写入操做

一、Writer

(1)BufferedWriter

写入字符输出流,缓冲字符。利用缓冲的包装,能够实现高效率的写入。

(2)OutputStreamWriter

是写入流中字符向字节转换的桥梁

(3)FileWriter

用于写入已过滤的字符流的抽象类

二、OutputStream

 (1)FileOutputStream

文件输出流是用于将图像数据之类的原始字节流写入File或FileDescriptor的输出流。而字符流通常使用FileWriter

(2)BufferedOutputStream

缓冲的输出流

写入操做用例

 

 1 import java.io.BufferedInputStream;  2 import java.io.BufferedOutputStream;  3 import java.io.BufferedReader;  4 import java.io.BufferedWriter;  5 import java.io.FileInputStream;  6 import java.io.FileNotFoundException;  7 import java.io.FileOutputStream;  8 import java.io.FileReader;  9 import java.io.FileWriter;  10 import java.io.IOException;  11 import java.io.OutputStreamWriter;  12 
 13 public class IO写入用例 {  14 
 15     public static void main(String[] args) throws IOException {  16         
 17         BufferedReader br=null;  18         BufferedInputStream bis=null;  19         
 20         BufferedWriter bw=null;  21         BufferedOutputStream bos=null;  22         try {  23             FileReader fr=new FileReader("read.txt"); //51,420KB
 24             br=new BufferedReader(fr);  25  String line;  26             //文本同条件写入  27             
 28             //方式一 :FileWriter直接写入 
 29             FileWriter fw=new FileWriter("write.txt");  30             
 31             long t=System.currentTimeMillis();  32             if(br.ready()){  33                 while((line=br.readLine())!=null){  34                     //TODO:
 35  fw.write(line);  36  }  37  }  38             t=System.currentTimeMillis()-t;  39             
 40             System.out.println("BufferedWriter用时"+t);//910s  41             
 42             //方式二 :BufferedWriter缓冲包装 
 43             br=new BufferedReader(fr);  44             bw=new BufferedWriter(new FileWriter("write.txt"));  45             
 46             long t2=System.currentTimeMillis();  47             if(br.ready()){  48                 while((line=br.readLine())!=null){  49                     //TODO:
 50  bw.write(line);  51  }  52  }  53             t2=System.currentTimeMillis()-t2;  54             
 55             System.out.println("BufferedWriter用时"+t2); //0s  56             
 57             //方式三:原始字节流转换写入
 58             br=new BufferedReader(fr);  59             bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("write.txt")));  60             
 61             long t3=System.currentTimeMillis();  62             if(br.ready()){  63                 while((line=br.readLine())!=null){  64                     //TODO:
 65  bw.write(line);  66  }  67  }  68             t3=System.currentTimeMillis()-t3;  69             
 70             System.out.println("BufferedWriter用时"+t3); //0s  71             
 72             //字节流  73             
 74             //方式一:流直接写入
 75             bis=new BufferedInputStream(new FileInputStream("read.txt"));  76             FileOutputStream fos=new FileOutputStream("write");  77             byte[] b=new byte[10*1024];  78             
 79             long t4=System.currentTimeMillis();  80             if(bis.available()>0){  81                 while((bis.read(b))!=-1){  82                     //TODO:
 83  fos.write(b);  84  }  85  }  86             t4=System.currentTimeMillis()-t4;  87             
 88             System.out.println("InputStream用时"+t4); //143s  89             
 90             //方式二:流套上缓冲写入
 91             bis=new BufferedInputStream(new FileInputStream("read.txt"));  92             b=new byte[10*1024]; //存储读取数组
 93             bos=new BufferedOutputStream(new FileOutputStream("write.txt"));  94                         
 95             long t5=System.currentTimeMillis();  96             if(bis.available()>0){  97                 while((bis.read(b))!=-1){  98                     //TODO:
 99  bos.write(b); 100  } 101  } 102             t5=System.currentTimeMillis()-t5; 103             
104             System.out.println("InputStream用时"+t5); //137s
105             
106         } catch (FileNotFoundException e) { 107  e.printStackTrace(); 108         } finally{ 109  br.close(); 110  bis.close(); 111  bw.close(); 112  bos.close(); 113  } 114         
115 
116  } 117 
118 }
相关文章
相关标签/搜索