java IO流

首先,来看一下 , 流的分类 :

① . 流的方向 :
        输入流 :数据源到程序(InputStream、Reader读进来)
        输出流 : 程序到目的地(OutputStream、Writer写出去)
② . 处理数据单元 :
        字节流 : 按照字节读取数据(InputStream、OutputStream)
        字符流 : 按照字符读取数据(Reader、Writer)
③ . 功能不同 :
        节点流 : 可以直接从数据源或目的地读写数据
        处理流 : 不直接连接到数据源或目的地 , 是处理流的流 , 通过对其他流的处理提高程序的性能 .
节点流和处理流的关系 :
        节点流处于IO操作的第一线 , 所有操作必须通过他们进行 ;处理流在节点流之上,可以对其他流进行处理(提高效率或操作灵活性)

 

eg : 使用缓冲流对文件进行操作 :

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. package example;  
    2.   
    3. import java.io.BufferedInputStream;  
    4. import java.io.BufferedOutputStream;  
    5. import java.io.BufferedReader;  
    6. import java.io.BufferedWriter;  
    7. import java.io.File;  
    8. import java.io.FileInputStream;  
    9. import java.io.FileOutputStream;  
    10. import java.io.FileReader;  
    11. import java.io.FileWriter;  
    12. import java.io.InputStream;  
    13. import java.io.OutputStream;  
    14. /** 
    15.  * 文件的操作,加入缓冲流,提高性能. 
    16.  * @author Miao 
    17.  * 
    18.  */  
    19. public class TestDemo {  
    20.     public static void main(String[] args) throws Exception {  
    21.           
    22.     }  
    23.       
    24.     /** 
    25.      * 用字节缓冲输出流,往指定文件输入内容 
    26.      * @throws Exception 
    27.      */  
    28.     public static void store() throws Exception {  
    29.         String msg = "好好学习,天天向上.";  
    30.         File destFile = new File("e:\\msg\\msg.txt");  
    31.         if(!destFile.getParentFile().exists()) {  
    32.             destFile.getParentFile().mkdirs();  
    33.         }  
    34.         //加入缓冲流,提高性能.因为BufferedOutputStream类中没有新增方法,可以使用向上转型  
    35.         OutputStream is = new BufferedOutputStream(new FileOutputStream(destFile));  
    36.         byte data[] = msg.getBytes();  
    37.         is.write(data);  
    38.         is.flush();  
    39.         is.close();  
    40.     }  
    41.       
    42.     /** 
    43.      * 用字节缓冲输入流,从指定文件中输出内容 
    44.      * @throws Exception 
    45.      */  
    46.     public static void load() throws Exception{  
    47.         File srcFile = new File("e:\\msg\\msg.txt");  
    48.         if(srcFile.exists()) {  
    49.             //加入缓冲流,提高性能.因为BufferedInputStream类中没有新增方法,可以使用向上转型  
    50.             InputStream is = new BufferedInputStream(new FileInputStream(srcFile));  
    51.             byte buf[] = new byte[1024];  
    52.             int len = 0;  
    53.             while((len=is.read(buf)) != -1) {  
    54.                 System.out.println(new String(buf,0,len));  
    55.             }  
    56.             is.close();  
    57.         }  
    58.     }  
    59.       
    60.     /** 
    61.      * 用字符缓冲输出流,往指定文件输入内容 
    62.      * @throws Exception 
    63.      */  
    64.     public static void save() throws Exception{  
    65.         String msg = "好好学习,天天向上.";  
    66.         File destFile = new File("e:\\msg\\msg.txt");  
    67.         if(!destFile.getParentFile().exists()) {  
    68.             destFile.getParentFile().mkdirs();  
    69.         }  
    70.         //加入缓冲流,使用新增方法,不能用向上转型  
    71.         BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));  
    72.         writer.write(msg);  
    73.         //新增的方法newLine()  
    74.         writer.newLine();  
    75.         writer.flush();  
    76.         writer.close();  
    77.     }  
    78.       
    79.     /** 
    80.      * 用字符缓冲输入流,从指定文件中输出内容 
    81.      * @throws Exception 
    82.      */  
    83.     public static void get() throws Exception {  
    84.         File srcFile = new File("e:\\msg\\msg.txt");  
    85.         if(srcFile.exists()) {  
    86.             //加入字符缓冲流,使用新增方法,不能用向上转型  
    87.             BufferedReader reader = new BufferedReader(new FileReader(srcFile));  
    88.             String buf = null;  
    89.             //新增方法readLine()  
    90.             while((buf=reader.readLine()) != null) {  
    91.                 System.out.println(new String(buf));  
    92.             }  
    93.             reader.close();  
    94.         }  
    95.     }  
    96. }  
posted @ 2016-12-06 13:41  天涯海角路  阅读(91)  评论(0)    收藏  举报