Java流机制

流即数据的流向,即数据的输入/输入方向。可以是文件,内存,硬盘的其他的设备。

Java流分为三类:
1.按照处理数据大小:字节流和字符流
2.按照流的方向:输入流和输出流
3.按照功能分为:分为节点流和处理流

字节流和字符流:
    1)字节流:读取的数据以字节为单位(byte),8bit,我们要与InputStream,OutputStream(抽象类)相关联。
       当我们读取文件然后在写入到其它的文件时候,不用过多的关注读取的内容时,通常使用的是字节流,因为这相当于是在处理二进制文件。读取数据效率高,并且保持数据的完整性。
    2)字符流:读取的数据以字符为单位(char),16bit,我们要与InputReader,OutputReader(抽象类)相关联。
       当我们读取文件的内容,并要对文件的内容进行加工时(修改,判断等),通常是使用的字符流,因为我们读取的其实是按照一个字符。
    读取的,文件的最基本单位是字符,同样保持数据的完整性。

输入流和输出流:
    1)输入流:是指从流读入数据。
    2)输出流:是指向流写出数据。

处理流和节点流(重点介绍):

节点流:即从特定的数据源读取写入数据(FileReader,PrintWriter)。

处理流:在已经存在的节点流或者处理流上,进行装饰提供更强大的读写功能。

     1)缓冲流(Buffered)顾名思义就是带缓冲区.如:在节点流之上进行加工,添加缓冲区(FileInputStream=>BufferedInputStream)这样避免读取文件时候,大量进行对硬盘的读写,而是从缓冲区进行读写,提高读写效率。

     2)转换流(StreamReader)即字节流与字符流的相互转换。比如:在进行读写字节流设备时候,我想调用读取字符流的函数,就可以通过转换流。将(字节流=>字符流) InputStreamReader in=new InputStreamReader(new InputStream())注意只能是字节流转换为字符流!!

     3) 数据流(Data) 当读取写入具体的数值数据时候(Long Double)就可以采用DataInputStream和DataOutputStream流进行功能更加强大的写入和读取功能。

    下面请看代码:(很多东西都写到代码里面请仔细阅读

[java]  view plain  copy
  1.        //函数功能:拷贝文件 并在末尾加上helloworld  
  2. public static void copyTofile(String sorFile,String dirFile) throws IOException{  
  3.     //拷贝文件一般是用字节流不考虑文件内容  
  4.     FileInputStream fin=null;            //节点流  
  5.     FileOutputStream fout=null;  
  6.     BufferedInputStream in=null;         //处理流  
  7.     BufferedOutputStream out=null;       
  8.     DataOutputStream dout=null;          //处理流  
  9.     try {  
  10.         //处理流在节点流进行装饰,处理流其是对流的功能的加强实现具体的更强大的功能  
  11.         /* 
  12.          * BufferedInputStream和BufferedOutputStream但缓冲区的输入输出流 
  13.          * 将文件读取到缓冲区,读取的文件其实就是再跟硬盘在进行交互 
  14.          * 读取缓冲区,比硬盘的速度加快,效率提高,且避免进行大量的进行硬盘读写 
  15.          * */  
  16.         fin=new FileInputStream(new File(sorFile));  
  17.         fout=new FileOutputStream(new File(dirFile));  
  18.         in=new BufferedInputStream(fin);  
  19.         out=new BufferedOutputStream(fout);  
  20.         dout=new DataOutputStream(out);  
  21.         byte [] contentArr = new byte[1024];      
  22.         int fileSize=0;  
  23.         while((fileSize=in.read(contentArr))!=-1){  
  24.             out.write(contentArr,0,fileSize);  
  25.         }  
  26.         System.out.println("copy successfully");  
  27.         dout.writeChars("helloworld");    //写入的是h e l l o w o r l d  
  28.         dout.writeBytes("helloworld");    //写入的helloworld(想一下这里区别 byte char)  
  29.     } catch (FileNotFoundException e) {  
  30.         // TODO Auto-generated catch block  
  31.         e.printStackTrace();  
  32.     } finally{  
  33.         dout.close();  
  34.         in.close();  
  35.         out.close();  
  36.         fout.close();  
  37.         fin.close();  
  38.       
  39.     }  
  40.    }  
  41. //函数功能:统计文本中hello个数  
  42. public static void readFileStream(String sorFile) throws IOException{  
  43.     //拷贝文件一般是用字节流不考虑文件内容  
  44.     //统计文本中hello个数  
  45.     FileInputStream fin=null;            //节点流  
  46.     BufferedReader in=null;              //处理流->缓冲流  
  47.        InputStreamReader inStremRead=null;  //处理流->转换流  
  48.     try {  
  49.         fin=new FileInputStream(new File(sorFile));   //字节流  
  50.         inStremRead=new InputStreamReader(fin);       //转换流(字节流->字符流)  
  51.         in=new BufferedReader(inStremRead);           //缓冲流(转换流)  
  52.         String fileStr=null;  
  53.         int count=0;  
  54.         while((fileStr=in.readLine())!=null){  
  55.             if(fileStr.contains("hello"))  
  56.                 count++;  
  57.         }  
  58.         System.out.println(count);  
  59.     } catch (FileNotFoundException e) {  
  60.         // TODO Auto-generated catch block  
  61.         e.printStackTrace();  
  62.     } finally{  
  63.       
  64.     }  
  65.    }  
  66.   
  67. //函数功能:统计文本中hello个数  
  68. public static void countStr(String sorFile) throws IOException{  
  69.     try {  
  70.         //读取文件内容字节流  
  71.         FileReader fin=new FileReader(new File(sorFile));  
  72.         String fileStr=null;  
  73.         int count=0;  
  74.         BufferedReader in=new BufferedReader(fin);  
  75.         while((fileStr=in.readLine())!=null){  
  76.             if(fileStr.contains("hello"))  
  77.                 count++;  
  78.         }  
  79.         System.out.println(count);  
  80.           
  81.     } catch (FileNotFoundException e) {  
  82.         // TODO Auto-generated catch block  
  83.         e.printStackTrace();  
  84.     }  
  85. }  
  86. BufferWriter和DataOutputStream都可以实现数据的输出,那么这2种输出流有什么区别,看下api就知道了。 
    BufferedWriter:可以传int型值,传char数组,传String
    DataOutputStream:可以传8种基本数据类型的值,可以传String.
    如果你要传一个double类型的数,那你得用DateOutputStream传吧。
    建议不管哪个流最好最后都flush()下
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值