Java流机制详解

来源:互联网 发布:边伯贤直播软件 编辑:程序博客网 时间:2024/06/07 07:30


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

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流进行功能更加强大的写入和读取功能。

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

        //函数功能:拷贝文件 并在末尾加上helloworldpublic static void copyTofile(String sorFile,String dirFile) throws IOException{//拷贝文件一般是用字节流不考虑文件内容FileInputStream fin=null;            //节点流FileOutputStream fout=null;BufferedInputStream in=null;         //处理流BufferedOutputStream out=null;     DataOutputStream dout=null;          //处理流    try {    //处理流在节点流进行装饰,处理流其是对流的功能的加强实现具体的更强大的功能/* * BufferedInputStream和BufferedOutputStream但缓冲区的输入输出流 * 将文件读取到缓冲区,读取的文件其实就是再跟硬盘在进行交互 * 读取缓冲区,比硬盘的速度加快,效率提高,且避免进行大量的进行硬盘读写 * */fin=new FileInputStream(new File(sorFile));fout=new FileOutputStream(new File(dirFile));in=new BufferedInputStream(fin);out=new BufferedOutputStream(fout);dout=new DataOutputStream(out);byte [] contentArr = new byte[1024];int fileSize=0;while((fileSize=in.read(contentArr))!=-1){out.write(contentArr,0,fileSize);}System.out.println("copy successfully");dout.writeChars("helloworld");    //写入的是h e l l o w o r l ddout.writeBytes("helloworld");    //写入的helloworld(想一下这里区别 byte char)} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{dout.close();in.close();out.close();fout.close();fin.close();}    }//函数功能:统计文本中hello个数public static void readFileStream(String sorFile) throws IOException{//拷贝文件一般是用字节流不考虑文件内容//统计文本中hello个数FileInputStream fin=null;            //节点流BufferedReader in=null;              //处理流->缓冲流        InputStreamReader inStremRead=null;  //处理流->转换流    try {        fin=new FileInputStream(new File(sorFile));   //字节流        inStremRead=new InputStreamReader(fin);       //转换流(字节流->字符流)        in=new BufferedReader(inStremRead);           //缓冲流(转换流)        String fileStr=null;    int count=0;    while((fileStr=in.readLine())!=null){    if(fileStr.contains("hello"))    count++;    }    System.out.println(count);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{}    }//函数功能:统计文本中hello个数public static void countStr(String sorFile) throws IOException{try {//读取文件内容字节流FileReader fin=new FileReader(new File(sorFile));    String fileStr=null;    int count=0;    BufferedReader in=new BufferedReader(fin);    while((fileStr=in.readLine())!=null){    if(fileStr.contains("hello"))    count++;    }    System.out.println(count);    } catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

   

1 0
原创粉丝点击