從IO 到框架(1)-IO流

學習Java Web 編程的歷程,就像生物的演化,一步步從30億年前的單細胞到今天的各種高級生物。

此係列文章嘗試梳理這一歷程。本篇爲第一篇,IO流 - 設備內的數據傳輸。

1)代碼:從硬盤上讀入index.html, 打印在控制檯並輸出到硬盤另一位置。

import org.junit.Test;
import java.io.*;

public class Client {
    
    @Test
    public void getIndexPage(){
        String path = "E:\\IDEA Projects\\Itrocks_01_IO\\src\\webapp\\index.html";
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
                //輸出文件名可爲相對路徑(項目根目錄)或絕對路徑
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("index.html"))){
            byte[] bytes = new byte[1024];
            int read;
            while ((read = bis.read(bytes)) > 0){
                bos.write(bytes, 0, read);
                System.out.println(new String(bytes));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2)主要IO流對象繼承關係


PS: 單細胞到人類30億年的簡單動態圖



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