IO字節流和字符流及測試代碼

字節流

可以處理一切文件包括二進制、音頻、視頻、doc等

節點流 InputStream FileInputStream

​ OutputStream FileOutputStream

一、讀取文件

建立聯繫

File對象

選擇流

文件輸入流 InputStream FileInputStream

操作

byte[] car = new byte[1024] + read + 讀取大小

​ 輸出

釋放資源

關閉

Code

package com.iotest.byteIO;

import java.io.*;

public class Demo01 {
    /**
     * 文件的讀取2
     * @param args
     */
    public static void main(String[] args) {
        File src = new File("D:/Code/Java/IO/src/main/resources/2.txt");

        InputStream is = null;
        try {
            is = new FileInputStream(src);
            byte[] car = new byte[10];
            int len = 0;
            while ((len = is.read(car)) != -1) {
                String info = new String(car, 0, len);
                System.out.println(info);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("Not found");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("read fail");
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("file close");
            }
        }
    }
}

二、寫出文件

建立聯繫

File對象

選擇流

文件輸出流 OutputStream FileOutputStream

操作

write() + flush

釋放資源

關閉

Code

package com.iotest.byteIO;

import java.io.*;

public class Demo02 {

    public static void main(String[] args) {
        File dest = new File("D:/Code/Java/IO/src/main/resources/2.txt");

        OutputStream os = null;

        try {
            os = new FileOutputStream(dest, true);
            String str = "hello hello hello \r\n";
            byte[] data = str.getBytes();
            os.write(data, 0, data.length);

            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("not found");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("write fail");
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (Exception e2) {
                System.out.println("close fail");
            }
        }
    }
}

三、文件拷貝

程序爲橋樑

建立聯繫

File對象 源頭 目的地

選擇流

文件輸入流 InputStream FileInputStream

文件輸出流 OutputStream FileOutputStream

操作

拷貝

byte[] flush = new byte[1024];
int len = 0;
while(-1 != (len=輸入流.read(flush))){
  輸出流.write(flush,0,len)
}

釋放資源

關閉 兩個流

Code

package com.iotest.byteIO;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.*;

public class Demo03CopyFile {

    public static void main(String[] args) {
        File src = new File("D:/Code/Java/IO/src/main/resources/test.png");
        File dest = new File("D:/Code/Java/IO/src/main/resources/100.png");
        try {
            copyFile(src,dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(File src,File dest) throws IOException {
        if (!src.isFile()) {
            System.out.println("only file");
            throw new IOException("only file");
        }
        // 建立聯繫 源(存在且爲文件) + 目的地(文件可以不存在)
        // 選擇流
        InputStream is = new FileInputStream(src);
        OutputStream os = new FileOutputStream(dest);
        if (!src.isFile()) {
            System.out.println("only file");
        }
        // 文件拷貝 循環+讀取+寫出
        byte[] flush = new byte[1024];
        int len = 0;
        while (-1 != (len = is.read(flush))) {
            os.write(flush, 0, len);
        }
        os.flush();

        os.close();
        is.close();
    }
}

四、文件夾拷貝

  1. 遞歸查找子孫級文件|文件夾

  2. 文件 複製(IO流複製)

    文件夾 創建

Code

package com.iotest.byteIO;

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

public class Demo04CopyDir {

    public static void main(String[] args) {
        // 源目錄
        String srcPath = "D:/Code/Java/IO/src/main/resources/testfile";
        // 目標目錄
        String destPath = "D:/Code/Java/IO/src/main/resources/1";

        File src = new File(srcPath);
        File dest = new File(destPath);
        copyDir(src,dest);
    }

    /**
     * 拷貝文件夾
     * @param src 源File對象
     * @param dest 目標File對象
     */
    public static void copyDir(File src, File dest) {
        if (src.isDirectory()) {
            dest = new File(dest,src.getName());
        }
        copyDirDetail(src,dest);
    }


    /**
     * 拷貝文件夾細節
     * @param src
     * @param dest
     */
    private static void copyDirDetail(File src, File dest) {
        if (src.isFile()){
            try {
                Demo03CopyFile.copyFile(src,dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if (src.isDirectory()) {
            // 確保目標文件夾存在
            dest.mkdirs();
            // 獲取下一級目錄|文件
             for (File sub:src.listFiles()) {
                 copyDirDetail(sub,new File(dest,sub.getName()));
             }
        }
    }
}

字符流

智能處理純文本、全部爲可見字符 .txt .html

節點流 Reader FileReader

​ Writer FileWriter

一、純文本讀取

建立聯繫

選擇流

Reader FileReader

讀取

char[] flush = new char[1024]

關閉

code
package com.iotest.charIO;

import java.io.*;

public class Demo01 {
    public static void main(String[] args) {
        File src = new File("D:/Code/Java/IO/src/main/resources/2.txt");

        Reader reader =null;

        try {
            reader = new FileReader(src);
            char[] flush = new char[10];
            int len  =0 ;
            while (-1 != (len = reader.read(flush))) {
                String str = new String(flush,0,len);
                System.out.println(str);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("not found");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("read fail");
        }finally {
            if (null!=reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

二、純文本寫出

建立聯繫

選擇流

Writer FileWriter

讀取

write(字符數組,0,長度) + flush

write(字符串)

append(字符串)

關閉

Code

package com.iotest.charIO;

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

public class Demo02 {
    public static void main(String[] args) {
        File dest = new File("D:/Code/Java/IO/src/main/resources/2.txt");

        Writer wr = null;

        try {
            wr = new FileWriter(dest);
            String msg = "重寫:哈哈哈哈哈哈哈哈哈 啊哈哈哈哈 \r\n嘻嘻嘻嘻嘻嘻 \r\nh和急吼吼";
            wr.write(msg);
            wr.append("append");

            wr.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (wr != null) {
                try {
                    wr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章