zoukankan      html  css  js  c++  java
  • Java IO流

    1. 流的分类

    • 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
    • 按数据流的流向不同分为:输入流,输出流
    • 按流的角色的不同分为:节点流,处理流

    2. 示例代码

      1 package com.windy.io;
      2 
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileOutputStream;
      6 import java.io.FileReader;
      7 import java.io.FileWriter;
      8 import java.io.IOException;
      9 
     10 import org.junit.Test;
     11 
     12 public class IOTest {
     13 
     14     @Test
     15     public void FileStreamTest(){
     16         
     17             File f1 = new File("001.jpg");
     18             File f2 = new File("001-copy.jpg");
     19             
     20             FileInputStream fis = null;
     21             FileOutputStream fos = null;
     22             
     23             try {
     24                     fis = new FileInputStream(f1);
     25                     fos = new FileOutputStream(f2);
     26                     
     27                     byte [] b = new byte[512];
     28                     int len = 0; 
     29                     
     30                     while((len = fis.read(b)) != -1){
     31                             fos.write(b, 0, len);
     32                     }
     33                     
     34                     
     35             } catch (Exception e) {
     36                     e.printStackTrace();
     37             }finally{
     38                 
     39                     if(fis != null){
     40                         try {
     41                             fis.close();
     42                         } catch (IOException e) {
     43                             e.printStackTrace();
     44                         }                        
     45                     }
     46                     if(fos != null){
     47                         try {
     48                             fos.close();
     49                         } catch (IOException e) {
     50                             e.printStackTrace();
     51                         }                        
     52                     }
     53             }
     54         
     55     }
     56     
     57     @Test
     58     public void FileStringTest(){
     59             File f1 = new File("001.txt");
     60             File f2 = new File("001-copy.txt");
     61             
     62             FileReader fis = null;
     63             FileWriter fos = null;
     64             
     65             try {
     66                     fis = new FileReader (f1);
     67                     fos = new FileWriter (f2);
     68                     
     69                     char [] b = new char[10];
     70                     int len = 0; 
     71                     
     72                     while((len = fis.read(b)) != -1){
     73 
     74                             fos.write(b, 0, len);
     75                     }
     76                     
     77                     
     78             } catch (Exception e) {
     79                     e.printStackTrace();
     80             }finally{
     81                 
     82                     if(fis != null){
     83                         try {
     84                             fis.close();
     85                         } catch (IOException e) {
     86                             e.printStackTrace();
     87                         }                        
     88                     }
     89                     if(fos != null){
     90                         try {
     91                             fos.close();
     92                         } catch (IOException e) {
     93                             e.printStackTrace();
     94                         }                        
     95                     }
     96             }
     97         }
     98     
     99     @Test
    100     public void BufferTest(){
    101         File f1 = new File("001.txt");
    102         File f2 = new File("001-copy.txt");
    103         
    104         FileReader fis = null;
    105         FileWriter fos = null;
    106         
    107         try {
    108                 fis = new FileReader (f1);
    109                 fos = new FileWriter (f2);
    110                 
    111                 char [] b = new char[10];        
    112                 int len = 0;  
    113                 
    114                 while((len = fis.read(b)) != -1){
    115 
    116                         fos.write(b, 0, len);
    117                 }
    118                 
    119                 
    120         } catch (Exception e) {
    121                 e.printStackTrace();
    122         }finally{
    123             
    124                 if(fis != null){
    125                     try {
    126                         fis.close();
    127                     } catch (IOException e) {
    128                         e.printStackTrace();
    129                     }                        
    130                 }
    131                 if(fos != null){
    132                     try {
    133                         fos.close();
    134                     } catch (IOException e) {
    135                         e.printStackTrace();
    136                     }                        
    137                 }
    138         }
    139         
    140         
    141     }
    142     
    143 }
    View Code
  • 相关阅读:
    力扣 136. 只出现一次的数字
    剑指 Offer 24. 反转链表
    javaWeb8——jdbc总结,JDBC调用存储过程和存储函数:CallableStatement
    剑指 Offer 53
    剑指 Offer 53
    使用 .NET Core 3.x 构建 RESTFUL Api
    SqlServer 多表连接、聚合函数、模糊查询、分组查询应用总结(回归基础)
    SqlServer 查询的几种方式以及数字函数、时间函数的应用总结(回归基础)
    关于SqlServer表结构 2(回归基础)
    关于SqlServer那些事1(回归基础)
  • 原文地址:https://www.cnblogs.com/fengze/p/6617763.html
Copyright © 2011-2022 走看看