I/O流之文件流

文件流可以分為字節流和字符流
字節流
字節流可以對任何文件進行操作 ,但效率不如字符流高
字節流分為字節輸入流和字節輸出流
輸入輸出是相對于電腦屏幕來說
輸入流FileInputStream 的read(byte b[])函數是從指定文件中讀出數據字符數組b[]中
用字節輸入流向文件輸入數據之前,此文件必須存在,否則會拋異常

package com.qf.demo3;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File("abc.txt"));     
            int  num = fis.available();// 查看剩余的   字節個數
            System.out.println(num);    
            // 效率高
            byte[] b = new  byte[10];   
            // 文件內容     
            // 偏移量+ 讀取的個數  <= 數組的長度
            int num2 = fis.read(b, 5, 6);// p偏移量 偏移的是數組的  幾個
            System.out.println(Arrays.toString(b));
            System.out.println(num2);   
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

字節輸出流FileOutputStream 的write()函數

package com.qf.demo3;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test2 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {                       // 輸出流 沒有文件會自動幫助創建, 輸入流必須要求讀取文件存在
            fos = new FileOutputStream(new File("def.txt"));// 只要輸出流與文件關聯, 就會自動的幫助創建文件
            
            fos.write(97);      
            fos.write(98);      
            fos.write(new byte[]{97,98,99,100});
                                    // 偏移量是便宜的數組的       偏移量+ 長度 <= 數組長度
            fos.write(new byte[]{97,98,99,100}, 2, 2);          
            // 解決   執行了 write 文件中有可能不能里面將內容寫進去
            fos.flush();// 刷新       
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

  • 復制文件
  • 1 創建流對象
  • 2 讀
  • 3 寫
  • 4 關流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test3 {

    public static void main(String[] args) {
//       1  創建流對象
        FileInputStream fis =  null;
        FileOutputStream fos =null;
         try {
             fis = new FileInputStream(new File("abc.txt"));
             fos = new FileOutputStream(new File("aaa.txt"),true);// boolean append   true 追加    false 覆蓋
            
            // 2  讀
            byte[] bs = new  byte[10];
            int num = 0;
            while((num = fis.read(bs))!=-1){
                // 3 寫
                fos.write(bs,0,num);
                fos.flush();
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        
    }
}

字符輸入流FileReader 以字節的形式將數據從文件中讀出來

package com.qf.demo4;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        FileReader reader = null;
        try {
            reader = new FileReader(new File("二狗.txt"));
            
            int num = reader.read();
            System.out.println(num);
            
            char[] cbuf = new char[10];
            // 返回值 代表 讀取的數據個數    讀取出來的內容 放到char數組中
            int num2 = reader.read(cbuf);
            System.out.println(Arrays.toString(cbuf));
    
            int num3 = reader.read(cbuf, 3, 7);
            System.out.println(Arrays.toString(cbuf));
            System.out.println("有效個數"+num3);
            // 前面只讀了18 
    
            // 還剩下28個
            reader.skip(28);// 跳過28個
            
            int num4 = reader.read();
            System.out.println(num4);
    
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
}

字符輸出流 FileWriter

package com.qf.demo4;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Test2 {

    public static void main(String[] args) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(new File("副班長"),true);
            
            writer.write("明天會更好");

            writer.write(new char[]{'后','天','呵','呵'});
            
            writer.write(new char[]{'大','大','大','大','后','天','放','假'},0 , 6);
            
            writer.write("開心就好", 1, 3);
        
            writer.append("說的太對了");
            writer.append('錯');
            writer.append("呵呵呵呵呵額呵呵哈", 2, 6);// 左閉右開
            writer.flush();//  刷新
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容