java study檔案讀寫

來源:互聯網
上載者:User

標籤:fileinput   fileread   代碼   system   throw   close   冗餘   源碼   ade   

檔案讀寫

如果在代碼中寫入大量的資料,會增加代碼的冗餘度,通過讀取檔案的方式,可以精簡代碼,便於資料的修改和代碼的維護
IO流的分類:位元組流和字元流

字元流
  • 字元輸出資料流:寫文字檔的,抽象基類java.io.Writer。寫的方法write,很多重載形式,寫字元數組、單個字元、字串、字串組一部分、字串的一部分,flush資料流的緩衝,close關閉對象。
  • 字元輸入資料流:讀取文字檔的,抽象基類java.io.Reader。讀的方法read,很多重載形式,讀取單個字元、字元數組、字元數組的一部分。close關閉流對象。

    位元組流
  • 位元組輸出資料流:寫任意的檔案,抽象基類java.io.OutputStream。寫的方法write,很多重載形式,寫單個位元組,字元數組,位元組數組的一部分,close關閉流對象。
  • 位元組輸入資料流:讀取任意檔案,抽象基類java.io.InputStream。讀的方法read,很多重載形式,讀取單個位元組,字元數組,位元組數組的一部分,close關閉流對象。
    IO六種的所有類的命名法則:尾碼都是父類名,前面的都是可以操作的檔案名稱,流向名。
    FileinputStream FileReader ObjectInputStream ObjectOutputStream
#javapackage study1;import org.testng.annotations.Test;import java.io.*;public class IODemo {    @org.junit.Test    public void test() throws IOException {        //如果檔案不存在,會自動建立        FileWriter fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));        fw.write("hello world\n");        fw.write("123\n");        /**         * 寫入檔案後,未執行其他動作,寫入的內容不會儲存         * 寫入檔案後,close流,會自動儲存檔案。         * 寫入資料較多是,使用write寫入後,使用flush儲存寫入的內容,降低對系統壓力         */        fw.flush();        fw.write(123);//此處寫入的是ascii碼值        fw.flush();        fw.close();//釋放流資源    }    @org.junit.Test    public void test2() {        FileWriter fw= null ;        try{            fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));        }catch (IOException e){            e.printStackTrace();        }finally{            try{                fw.close();//此處釋放流資源,必須要先初始化            }catch (IOException e){                e.printStackTrace();            }        }    }    @org.junit.Test    public void append() throws IOException{        //追加內容,而非覆蓋        FileWriter fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"),true);        fw.write("hello python\n");        fw.flush();        fw.close();    }    @org.junit.Test    public void test_read() throws IOException{        //讀取檔案        FileWriter fw = null;        fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));        fw.write("abcd");        fw.flush();        fw.close();        FileReader fr = null;        fr = new FileReader(new File("/Users/chenshanju/Desktop/data/a.txt"));        System.out.println(fr.read());//輸出字元的ASCII碼值,每執行一次就讀取1次        System.out.println((char)fr.read());        System.out.println(fr.read());        System.out.println(fr.read());        System.out.println(fr.read());        System.out.println(fr.read());//沒有內容,輸出-1        fr.close();    }    @org.junit.Test    public void test_read2() throws IOException{        //讀取單個字元        FileWriter fw = null;        fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));        fw.write("abcd");        fw.flush();        fw.close();        FileReader fr = null;        fr = new FileReader(new File("/Users/chenshanju/Desktop/data/a.txt"));        int i=0;        while((i= fr.read())!=-1){            /**             * 擷取字元的ascii碼值,並根據ascii碼值強轉為字元             */            System.out.println((char)i);        }        fr.close();    }    @org.junit.Test    public void test_read3() throws IOException{        //讀取字元數組,一次讀取2個字元        FileWriter fw = new FileWriter(new File("Data/b.txt"));        fw.write("java");        char [] cha = {‘a‘,‘b‘,‘c‘};        fw.write(cha);        fw.close();        FileReader fr = new FileReader(new File("Data/b.txt"));        int i = 0;        char [] ch = new char[2];//一次讀取2個字元        while((i=fr.read(ch))!=-1){            System.out.print(i+"\t");            System.out.println(ch);            /**             * System.out.println(i+"\t"+new String(ch));             * 和int類型同時使用,不加new String,會返回雜湊碼[[email protected]             * 加上new String和上面2行輸出結果一致             * 2    ja             * 2    va             * 2    ab             * 1    cb 最後1次只裝入了1個字元,只將a替換為c,b並未替換             */        }    }    @org.junit.Test    //單個字元的寫入    public void test_cp() throws IOException{        FileReader fr = null;        FileWriter fw = null;        fr = new FileReader(new File("Data/b.txt"));        fw = new FileWriter(new File("Data/b1.txt"));        int i = 0;        while((i=fr.read())!=-1){            fw.write((char)i);        }        fw.close();        fr.close();    }    @org.junit.Test    //字元數組的寫入    public void test_cp1() throws IOException{        FileReader fr = null;        FileWriter fw = null;        fr = new FileReader(new File("Data/b.txt"));        fw = new FileWriter(new File("Data/b2.txt"));        char [] ch = new char[2];        int i = 0;        while((i=fr.read(ch))!=-1){            fw.write(ch,0,i);        }        fw.close();        fr.close();    }    @org.junit.Test    public void test_out() throws IOException{        //位元組輸出資料流        FileOutputStream out = null;        out = new FileOutputStream(new File("Data/c.txt"));        out.write("abcdefgh".getBytes());        out.close();    }    @org.junit.Test    public void test_in() throws IOException{        //位元組輸入資料流        FileInputStream in = null;        in = new FileInputStream(new File("Data/c.txt"));        int i = 0;        while((i=in.read())!=-1){            System.out.println((char)i);        }        in.close();    }    @org.junit.Test    public void test_in_char() throws IOException{        FileInputStream in = new FileInputStream(new File("Data/c.txt"));        int i = 0;        byte [] bt = new byte[10];        while((i=in.read(bt))!=-1){            System.out.print(i+"\t"+new String(bt));            /**             * System.out.println(bt);返回雜湊碼[[email protected]             * 長度不滿10位,會用空位補齊             */        }    }    @org.junit.Test    public void test_IO() throws IOException{        //將某網頁的源碼儲存為檔案        FileInputStream in = new FileInputStream(new File("Data/d.html"));        FileOutputStream out = new FileOutputStream(new File("Data/d1.html"));        int i = 0;        byte [] bt = new byte[1024];        while((i=in.read(bt))!=-1){            out.write(bt,0,i);        }        out.close();        in.close();        System.out.println("success");    }}

java study檔案讀寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.