欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中IO流解析及代碼實例

 更新時間:2021年07月27日 11:40:32   作者:血蓮丹  
下面小編就為大家?guī)硪黄P(guān)于Java中的IO流總結(jié)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

I/O簡介

I/O是Input/output的縮寫,在java中,對于數(shù)據(jù)的輸入和輸出以流的方式進行。java.io包下提供了各種“流”類和接口,用以獲取不同種類的數(shù)據(jù),并通過標(biāo)準(zhǔn)的方法輸入或輸出數(shù)據(jù)。 

輸入輸出都是基于內(nèi)存的角度來說的。輸入:讀取外部數(shù)據(jù)(磁盤、光盤等存儲設(shè)備的數(shù)據(jù))到程序(內(nèi)存)中。 輸出:輸出output:將程序(內(nèi)存)數(shù)據(jù)輸出到磁盤、光盤等存儲設(shè)備中。

流的分類

操作數(shù)據(jù)單位不同分為:字節(jié)流(8 bit),字符流(16 bit)

數(shù)據(jù)流的流向不同分為:輸入流,輸出流

流的角色的不同分為:節(jié)點流,處理流  

節(jié)點流:直接從數(shù)據(jù)源或目的地讀寫數(shù)據(jù)。  

處理流:不直接連接到數(shù)據(jù)源或目的地,而是“連接”在已存在的流(節(jié)點流或處理流)之上,通過對數(shù)據(jù)的處理為程序提供更為強大的讀寫功能。 相當(dāng)于是二次包裝。

抽象基類 字節(jié)流 字符流
輸入流 InputStream Reader
輸出流 OutputStream Writer

Java的IO流共涉及40多個類,實際上非常規(guī)則,都是從這4個抽象基類派生的。由這四個類派生出來的子類名稱都是以其父類名作為子類名后綴。

字節(jié)流和字符流常用API

InputStream

  • int read():從輸入流中讀取數(shù)據(jù)的下一個字節(jié)。返回0到255范圍內(nèi)的int字節(jié)值。如果讀到流的末尾,則返回 -1。 此方法一次只讀一個字節(jié),效率太低,一般不用
  • int read(byte[] b):從此輸入流中將最多b.length個字節(jié)的數(shù)據(jù)保存到一個byte數(shù)組中。返回實際讀取字節(jié)長度,如果讀到流的末尾,也返回 -1。
  • int read(byte[] b,int off,int len):將輸入流中最多l(xiāng)en個數(shù)據(jù)字節(jié)讀入byte數(shù)組。嘗試讀取len個字節(jié),但讀取的字節(jié)也可能小于該值,實際讀取個數(shù)以讀取到的為準(zhǔn),比如長度為23個字節(jié)的內(nèi)容,每次讀取5個字節(jié),則第五次讀取流并沒處于末尾,還剩長度是3,故返回3。第六次讀取發(fā)現(xiàn)是末尾,則返回 -1 。一般都用此方法。
  • public void close() throws IOException:關(guān)閉此輸入流并釋放與該流關(guān)聯(lián)的所有系統(tǒng)資源,一定要關(guān)閉。

Reader

  • Reader和InputStream類似,就是將字節(jié)數(shù)組換成了字符數(shù)組
  • int read(); 效果與字節(jié)流一致
  • int read(char[] cbuf); 效果與字節(jié)流一致
  • int read(char[] cbuf,int off,int len); 效果與字節(jié)流一致
  • public void close() throws IOException;關(guān)閉此輸入流并釋放與該流關(guān)聯(lián)的所有系統(tǒng)資源,字符流的必須要關(guān)閉,不然會出問題

OutputStream

  • void write(int b) :將指定的字節(jié)寫入此輸出流。
  • void write( byte[] b) :將b.length個字節(jié)從指定的byte數(shù)組寫入此輸出流。
  • void write(byte[] b,int off,int len):將指定byte數(shù)組中從偏移量off開始的len個字節(jié)寫入此輸出流。
  • public void flush() throws IOException:刷新此輸出流并強制寫出所有緩沖的輸出字節(jié),調(diào)用此方法指示應(yīng)將這些字節(jié)立即寫入它們預(yù)期的目標(biāo)。就是將緩沖的字節(jié)全部寫出到字節(jié)或字符數(shù)組中。
  • public void close() throws IOException:關(guān)閉此輸入流并釋放與該流關(guān)聯(lián)的所有系統(tǒng)資源

Writer

  • void write(int c):寫入單個字符。
  • void write(char[] cbuf):寫入字符數(shù)組。
  • void write(char[] cbuf,int off,int len) : 寫入字符數(shù)組的某一部分。從off開始,寫入len個字符
  • void write(String str):寫入字符串。
  • void write(String str,int off,int len):寫入字符串的某一部分。
  • void flush():刷新該流的緩沖,則立即將它們寫入預(yù)期目標(biāo)。
  • public void close() throws IOException:關(guān)閉此輸入流并釋放與該流關(guān)聯(lián)的所有系統(tǒng)資源

字節(jié)字符流相關(guān)操作

字節(jié)流讀取文本內(nèi)容

// 讀取文本文件內(nèi)容,用字節(jié)流去讀,可能會亂碼
    public void test1() {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("hello.txt");//相對路徑是工程路徑下
            int len;
            byte[] bytes = new byte[5];// 定義5字節(jié)長度的byte數(shù)組
            while ((len = inputStream.read(bytes)) != -1) {// 將數(shù)據(jù)讀到byte數(shù)組中
                System.out.println(new String(bytes, 0, len)); //打印到控制臺
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}

字符流讀取文本內(nèi)容

// 讀取文本文件內(nèi)容,用字符流去讀,不會亂碼,最多重復(fù)	
    public void test2() {
        Reader reader = null;
        try {
            reader = new FileReader("hello.txt");
            int len;
            char[] charBuff = new char[5];
            while ((len = reader.read(charBuff)) != -1) {
//                System.out.println(new String(charBuff)); // 注意這兩行的代碼區(qū)別
                System.out.println(new String(charBuff, 0, len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

字節(jié)流讀取文件到輸出到指定位置

public void test3() {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = new FileInputStream("hello.txt"); // 讀取文件
            outputStream = new FileOutputStream("hello2.txt"); // 相對路徑,默認是工程路徑下
            int len;
            byte[] bytes = new byte[5];
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null){
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

字符流讀取文件到輸出到指定位置

字符流讀取文件到輸出到指定位置時,如果沒有手動關(guān)閉流,則不會輸出到指定位置,需要手動flush。但是如果在finally塊中關(guān)閉了流,則會自動flush。在close()操作中,會幫我們flush。

public void test4() {
        Reader reader = null;
        Writer writer = null;
        try {
            reader = new FileReader("hello2.txt");
            writer = new FileWriter("hello3.txt");
            int len;
            char[] charBuff = new char[5];
            while ((len = reader.read(charBuff)) != -1) {
                writer.write(charBuff, 0, len);
//                writer.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (writer != null) { // 一定要關(guān)閉字符流,否則要手動flush
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

注意,字符流用來處理字符串很便捷,并且只能操作普通的文本文件,例如:.txt,.java,.c,.cpp 等語言的源代碼。尤其注意.doc,excel,ppt這些不是文本文件。字節(jié)流既可以操做文本文件,也可以操作字節(jié)文件,比如.mp3,.avi,.rmvb,mp4,.jpg,.doc,.ppt。如果用字符流來操作圖片等字節(jié)文件,生成的文件是無法打開的!

緩沖流

為了提高數(shù)據(jù)讀寫的速度,Java API提供了帶緩沖功能的流類,在使用這些流類時,會創(chuàng)建一個內(nèi)部緩沖區(qū)數(shù)組,缺省使用 8192個字節(jié)(8Kb) 的緩沖區(qū)。

緩沖流要“套接”在相應(yīng)的節(jié)點流之上,根據(jù)數(shù)據(jù)操作單位可以把緩沖流分為BufferedInputStream和ufferedOutputStream以及BufferedReader和BufferedWriter。分別對應(yīng)字節(jié)緩沖流和字符緩沖流。相當(dāng)于在字節(jié)和字符流上包裝了一下。

BufferedInputStream和BufferedOutputStream

public void test() {
        InputStream inputStream ;
        OutputStream outputStream ;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            inputStream = new FileInputStream("modify.txt"); //小于8KB
            outputStream = new FileOutputStream("modify2.txt");
            bis = new BufferedInputStream(inputStream);
            bos = new BufferedOutputStream(outputStream);
            int len;
            byte[] bytes = new byte[5];
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null){
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

BufferedReader和BufferedWriter

public void test2() {
        Reader reader ;
        Writer writer ;
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            reader = new FileReader("modify.txt");
            writer = new FileWriter("modify2.txt");
            br = new BufferedReader(reader);
            bw = new BufferedWriter(writer);
            int len;
            char[] bytes = new char[5];
            while ((len = br.read(bytes)) != -1) {
                bw.write(bytes, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null){
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

**緩沖流如果沒有手動關(guān)閉流,且讀取的文件小于底層緩存大小8KB,是不會自動寫到目標(biāo)中去的,需要手動flush。**在關(guān)閉流時,只需關(guān)閉緩沖流即可,它會自動關(guān)閉它包裝的底層節(jié)點流。

數(shù)據(jù)流

為了方便地操作Java語言的基本數(shù)據(jù)類型和String的數(shù)據(jù),可以使用數(shù)據(jù)流。數(shù)據(jù)流有兩個類:DataInputStream和DataOutputStream, 別“套接”在InputStream和OutputStream子類的流上。

使用如下:

 public void test() {
        DataOutputStream dos = null;
        try {
            OutputStream outputStream = new FileOutputStream("data.txt");
            dos = new DataOutputStream(outputStream);
            dos.writeUTF("熱烈慶祝中國共產(chǎn)黨成立一百周年");
            dos.writeInt(100);
            dos.writeBoolean(true);
            System.out.println("成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void test2(){
        DataInputStream dis =  null;
        try {
            InputStream inputStream = new FileInputStream("data.txt");
            dis = new DataInputStream(inputStream);
            System.out.println(dis.readUTF()); //讀取時要按照寫入順序讀取
            System.out.println(dis.readInt());
            System.out.println(dis.readBoolean());
            System.out.println("成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dis != null) {
                    dis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

對象流

用于存儲和讀取基本數(shù)據(jù)類型數(shù)據(jù)或?qū)ο蟮奶幚砹鳌?strong>它的強大之處就是可以把Java中的對象寫入到數(shù)據(jù)源中,也能把對象從數(shù)據(jù)源中還原回來。

  • 序列化:用ObjectOutputStream類保存基本類型數(shù)據(jù)或?qū)ο蟮臋C制
  • 反序列化:用ObjectInputStream類讀取基本類型數(shù)據(jù)或?qū)ο蟮臋C制
  • ObjectOutputStream和ObjectInputStream不能序列化static和transient修飾的成員變量

序列化與反序列化的演示

先定義一個Person類,該類必須實現(xiàn)Serializable接口,否則序列化時會報java.io.NotSerializableException 的錯誤

package day07;
import java.io.Serializable;
import java.util.Date;
public class Person implements Serializable {
    private static final long serialVersionUID = -5858950242987134591L;
    private String name;
    private Integer age;
    private Date date;
    public Person(){}
    public Person(String name, Integer age, Date date) {
        this.name = name;
        this.age = age;
        this.date = date;
    }
  // getter、setter略
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", date=" + date +
                '}';
    }
}

序列化

    public void test() {
        ObjectOutputStream oos = null;
        try {
            OutputStream outputStream = new FileOutputStream("person.txt"); // 創(chuàng)建輸出流對象,指定輸出位置
            oos = new ObjectOutputStream(outputStream); // 包裝輸出流
            oos.writeObject(new Person("張三",22,new Date())); // 序列化對象
            System.out.println("序列化成功!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

反序列化

public void test2() {
        ObjectInputStream ois = null;
        try {
            InputStream inputStream = new FileInputStream("person.txt");
            ois = new ObjectInputStream(inputStream);
            Person person = (Person) ois.readObject();
            System.out.println("person姓名為:" + person.getName());
            System.out.println("person年齡為:" + person.getAge());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh時mm分ss秒");
            System.out.println("創(chuàng)建時間為:" + sdf.format(person.getDate()));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

在這里插入圖片描述

無論如何反序列化,時間都是不變的。

總結(jié)

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • SpringBoot + SpringSecurity 短信驗證碼登錄功能實現(xiàn)

    SpringBoot + SpringSecurity 短信驗證碼登錄功能實現(xiàn)

    這篇文章主要介紹了SpringBoot + SpringSecurity 短信驗證碼登錄功能實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • IDEA提示:Boolean method ‘xxx‘ is always inverted問題

    IDEA提示:Boolean method ‘xxx‘ is always&nb

    這篇文章主要介紹了IDEA提示:Boolean method ‘xxx‘ is always inverted問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Feign之Multipartfile文件傳輸填坑

    Feign之Multipartfile文件傳輸填坑

    這篇文章主要介紹了Feign之Multipartfile文件傳輸埋坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringBoot使用RESTful接口詳解

    SpringBoot使用RESTful接口詳解

    RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個資源定位(url)及資源操作的風(fēng)格,這篇文章主要介紹了SpringBoot使用RESTful接口
    2022-10-10
  • Java?9?中的模塊Module系統(tǒng)

    Java?9?中的模塊Module系統(tǒng)

    Java?9?引入的模塊是在Java包(package)的基礎(chǔ)上又引入的一個新的抽象層,基于package這一點很重要,這里需要強調(diào)一下,接下來通過本文給大家介紹Java?9?中的模塊Module系統(tǒng),感興趣的朋友一起看看吧
    2022-03-03
  • Spring Boot接口設(shè)計防篡改、防重放攻擊詳解

    Spring Boot接口設(shè)計防篡改、防重放攻擊詳解

    這篇文章主要給大家介紹了關(guān)于Spring Boot接口設(shè)計防篡改、防重放攻擊的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Java實現(xiàn)簡單的彈球游戲

    Java實現(xiàn)簡單的彈球游戲

    這篇文章主要為大家詳細介紹了Java實現(xiàn)簡單的彈球游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Java之map的常見用法講解與五種循環(huán)遍歷實例代碼理解

    Java之map的常見用法講解與五種循環(huán)遍歷實例代碼理解

    map是一組鍵值對的組合,通俗理解類似一種特殊的數(shù)組,a[key]=val,只不過數(shù)組元素的下標(biāo)是任意一種類型,而且數(shù)組的元素的值也是任意一種類型。有點類似python中的字典。通過"鍵"來取值,類似生活中的字典,已知索引,來查看對應(yīng)的信息
    2021-09-09
  • SpringBoot整合SpringCloud的過程詳解

    SpringBoot整合SpringCloud的過程詳解

    SpringCloud專注于為典型的用例和擴展機制提供良好的開箱即用體驗,在介紹SpringCloud默認使用的注冊中心前,先給大家介紹下分布式系統(tǒng)的三個指標(biāo),具體內(nèi)容詳情跟隨小編一起看看吧
    2021-09-09
  • springboot統(tǒng)一返回json數(shù)據(jù)格式并配置系統(tǒng)異常攔截方式

    springboot統(tǒng)一返回json數(shù)據(jù)格式并配置系統(tǒng)異常攔截方式

    這篇文章主要介紹了springboot統(tǒng)一返回json數(shù)據(jù)格式并配置系統(tǒng)異常攔截方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評論