9.Java----IO流

1.png

1.IO流的分類

  • 根據(jù)處理數(shù)據(jù)類型的不同分為:字符流和字節(jié)流
  • 根據(jù)數(shù)據(jù)流向不同分為:輸入流和輸出流


    2.png

2.節(jié)點(diǎn)流和處理流

3.jpg

3.常用例子

3.1基本分為4個(gè)步驟:

1.創(chuàng)建File對(duì)象
2.創(chuàng)建流對(duì)象
3.進(jìn)行讀/寫操作
4.關(guān)閉流對(duì)象

//抽象基類         節(jié)點(diǎn)流                                   緩沖流(處理流的一種)
//InputStream      FileInputStream(字節(jié)流,圖片,視頻等)       BufferedInputStream
//OutputStream     FileOutputStream                             BufferedOutputStream
//Reader           FileReader(字符流,處理txt等文本文件)       BufferedReader
//Writer           FileWriter                                   BufferedWriter
public class FileTest {
    //FileReader
    @Test
    public void test1() {
        FileReader reader = null;
        try {
            //實(shí)例化文件
            File file = new File("./helloworld.txt");
            //創(chuàng)建讀入/寫入對(duì)象
            reader = new FileReader(file);
            //讀入/寫入
            //int read = reader.read();
            //方式一:
//            int read;
//            while ((read = reader.read()) != -1){
//                System.out.print((char)read);
//            }
            //方式二:
            int len;
            char[] chars = new char[5];
            while ((len = reader.read(chars)) != -1) {
                //len表示讀取的字符數(shù),保存在chars數(shù)組中
                for (int i = 0; i < len; i++) {
                    System.out.print(chars[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //關(guān)閉
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //FileWriter
    @Test
    public void test2() {
        FileWriter fw = null; //如果append為true,則在原文件中追加,否則覆蓋文件
        try {
            File file = new File("./helloword2.txt");
            fw = new FileWriter(file, false);
            Writer a = fw.append('a');
            fw.write("helloworld2!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //復(fù)制
    @Test
    public void test3() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            File file = new File("helloworld.txt");
            File file1 = new File("copy_helloword.txt");

            fr = new FileReader(file);
            fw = new FileWriter(file1);

            char[] chars = new char[1];
            int len;
            while ((len = fr.read(chars)) != -1) {
                System.out.println(chars[0]);
                fw.write(chars, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //圖片復(fù)制 FileOutputWriter,FileInputReader
    @Test
    public void test4() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File src = new File("./pic.png");
            File dest = new File("./pic2.png");

            fis = new FileInputStream(src);
            fos = new FileOutputStream(dest);

            byte[] bytes = new byte[1024];
            int len;

            while ((len = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //使用緩沖流(內(nèi)部有個(gè)有容器,當(dāng)讀取到一次數(shù)量的數(shù)據(jù)時(shí),一次性寫入)
    @Test
    public void test5(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        FileInputStream fis;
        FileOutputStream fos;
        try {
            File src = new File("./pic.png");
            File dest = new File("./pic3.png");
            //創(chuàng)建節(jié)點(diǎn)流
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dest);
            //使用處理流來(lái)包裹節(jié)點(diǎn)流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] bytes = new byte[1024];
            int len;

            while((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //只要關(guān)閉外層的流,內(nèi)層的流會(huì)自動(dòng)關(guān)閉
            if(bis !=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //使用對(duì)象流
    @Test
    public void test6(){
        try {
            File file = new File("object.txt");
            //序列化,將對(duì)象寫入文件中
            //1.類必須實(shí)現(xiàn)Serializable接口
            //2.需要一個(gè)UID,如果沒(méi)有的話,運(yùn)行時(shí)會(huì)自動(dòng)生成一個(gè),但是當(dāng)類改變時(shí),反序列化時(shí)會(huì)出現(xiàn)錯(cuò)誤
            // private static final long serialVersionUID = -6849794470754667710L;
            FileOutputStream fos = new FileOutputStream(file,false);

            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(new Person("yang",18));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test7(){
        try {
            File file = new File("./object.txt");
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Person person = (Person) ois.readObject();
            System.out.println(person);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class Person implements Serializable{
    String name;
    int age;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


}

4.自我總結(jié)

要進(jìn)行IO的操作,首要要確定是要進(jìn)行讀操作還是寫操作,讀的話就是Input/Reader,寫的話就是Output/Writer,其次確定操作的是字符還是字節(jié),字符流的話就是使用Reader,字節(jié)流的話就是使用Stream,同時(shí)在使用readf()進(jìn)行讀操作時(shí),使用char[] 或 byte[] 數(shù)組來(lái)存儲(chǔ)每一次讀入的數(shù)據(jù),然后使用write()將char[] 或 byte[]中的數(shù)據(jù)寫入指定文件中。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 五、IO流 1、IO流概述 (1)用來(lái)處理設(shè)備(硬盤,控制臺(tái),內(nèi)存)間的數(shù)據(jù)。(2)java中對(duì)數(shù)據(jù)的操作都是通過(guò)...
    佘大將軍閱讀 585評(píng)論 0 0
  • tags:io categories:總結(jié) date: 2017-03-28 22:49:50 不僅僅在JAVA領(lǐng)...
    行徑行閱讀 2,301評(píng)論 0 3
  • 一、基礎(chǔ)知識(shí):1、JVM、JRE和JDK的區(qū)別:JVM(Java Virtual Machine):java虛擬機(jī)...
    殺小賊閱讀 2,563評(píng)論 0 4
  • 《夜靜思》 —春節(jié)又逢元宵佳節(jié)有感 耳沖煙花爆竹聲, 游人踏雪賞花燈。 佳節(jié)逢春元宵夜, 親朋賀歲訴衷情。...
    雙一悅閱讀 222評(píng)論 0 1
  • 3月上中旬堅(jiān)持練習(xí)數(shù)學(xué)題,發(fā)現(xiàn)很多的數(shù)學(xué)問(wèn)題自己都繞不過(guò)來(lái),數(shù)學(xué)計(jì)算容易錯(cuò),因此以后要多進(jìn)行計(jì)算練習(xí)。 完成了“小...
    冰清玉潔志高人杰閱讀 337評(píng)論 0 8

友情鏈接更多精彩內(nèi)容