各類流

字節流:操做文件中的數據,通常用byte數組java

outputstream:字節輸出流的超類 抽象類數組

fileoutputstream工具

構造方法:能夠傳字符串路徑   能夠傳file對象編碼

沒有文件的話 直接建立spa

FileOutputStream fos = new FileOutputStream("d:\\12\\output.txt");

方法:code

write():能夠直接傳int數值,能夠傳byte數組,能夠控制數組的寫入位置和寫入個數對象

//write(int i) 一次只能操做一個字節   轉成了二進制字節
        fos.write(97);
        //往文件裏寫入100
        fos.write(49);
        fos.write(48);
        fos.write(48);
        fos.close();
        //write(byte[] byte)
        byte[] b= {-65,-66,-67,-68};
        fos.write(b);
        fos.close();

文件的續寫和換行:構造方法中加上true表示續寫,\r\n表示換行blog

outputstream:字節輸入流的超類 抽象類接口

fileoutputstreamutf-8

構造方法:能夠傳字符串路徑,能夠傳file對象

FileInputStream fis = new FileInputStream("d:\\12\\output.txt");

方法:

read():有返回值 空參返回讀取值的數,沒有讀取到數就返回-1   傳入byte數組,返回讀取的有效個數,沒有有效讀取的數就返回-1,能夠控制傳入數組的個數

FileInputStream fis = new FileInputStream("d:\\12\\output.txt");
        int len = 0;
        while((len = fis.read())!=-1){
            System.out.println((char)len);
        }
        fis.close();
FileInputStream fis = new FileInputStream("d:\\12\\output.txt");
        byte[] bytes = new byte[10];
        fis.read(bytes,2,5);
        fis.close();
        for(byte b:bytes){
            System.out.println(b);
        }
FileInputStream fis = new FileInputStream("d:\\12\\output.txt");
        int len = 0;
        byte[] bytes = new byte[2];
        while((len = fis.read(bytes))!=-1){
            
                System.out.println(new String(bytes,0,len));
        }
        fis.close();

文件的複製:通常用write和read方法中的byte數組的方法   注意關閉資源

FileInputStream fis = new FileInputStream("d:\\0328");
        FileOutputStream fos = new FileOutputStream("d:\\12\\0328");
        byte[] bytes = new byte[1024*1024];
        int len = 0;
        while((len=fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
        }
        fis.close();
        fos.close();

 字符流操做字符的流,通常用char數組

字節流是一個個字節去讀取,中文一個字是一個字符,表明兩個字節,用字節流讀取很不方便

writer:全部字符輸出流的超類

filewriter:

構造方法:能夠傳入字符串表示的路徑,能夠傳入file對象,同時能夠傳入true表示是否續寫

FileWriter fw = new FileWriter("d:\\12\\writer.txt",true);

write()方法:能夠傳入int數值類型,一個個的寫;能夠傳入char數組,寫入數組中的內容,並控制寫入多少;能夠傳字符串,並控制寫入多少

字符流的寫入必須有flush()方法,刷新

FileWriter fw = new FileWriter("d:\\12\\writer.txt",true);
        fw.write(100);
        fw.flush();
        fw.close();
        char[] ch = {'你','好','嗎'};
        fw.write(ch, 0, 2);
        fw.write("崔永元實話實說",0,3);
        fw.flush();
        fw.close();

reader:全部字符輸入流的超類

filereader:

構造方法:能夠傳字符串表示路徑,能夠傳file對象

FileReader fr = new FileReader("d:\\12\\writer.txt");

read()方法:有返回值,空參返回讀取的字符的數(這個是系統默認的gbk編碼表,返回一個多位的數);char數組,返回的是有效讀取個數

字符流的複製:通常用read()和write()方法的char數組方法,注意刷新,關閉資源

FileReader fr = new FileReader("d:\\12\\writer.txt");
        FileWriter fw = new FileWriter("d:\\12\\writer1.txt");
        int len =0;
        char[] ch = new char[2];
        System.out.println(fr.read());
        while((len=fr.read(ch))!=-1){
        fw.write(ch, 0, len);
        fw.flush();
        }
        fr.close();
        fw.close();

轉換流:在gbk(系統默認編碼)和utf-8編碼的轉換,本質仍是一個字符流

outputstreamwriter:字符流向字節流轉換的橋樑

OutputStreamWriter流中有本身的緩衝區,用OutputStreamWriter對象使用write方法時,

拿着字符到指定的碼錶中進行查詢,把查到的字符編碼值轉成字節數據存放到OutputStreamWriter緩衝區中,

而後再調用刷新功能,或者關閉流,或者緩衝區存滿後會把緩衝區中的字節數據使用字節流寫到指定的文件中

構造方法:有outputstreamwriter就必定要先構造一個fileoutputstream對象,把此對象傳入到構造方法中,並指定寫入的編碼

方法:write()和字符輸出流同樣

 

FileOutputStream fos = new FileOutputStream("d:\\12\\writer.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
        osw.write("小豬佩奇");
        osw.flush();
        osw.close();

 

inputstreamreader:字節流向字符流轉換的橋樑

使用指定的編碼表讀取字節並將其解碼爲字符

構造方法:有inputstreamreader就必定要先構造一個fileinputstream對象,把此對象傳入到構造方法中,並指定解碼的編碼

方法:read()和字符輸入流同樣

 

FileInputStream fis = new FileInputStream("d:\\12\\writer.txt");
        InputStreamReader isr = new InputStreamReader(fis,"utf-8");
        int len = 0;
        while((len=isr.read())!=-1){
            System.out.print((char)len);
        }
        isr.close();

轉換流的複製:

// TODO Auto-generated method stub
        FileInputStream fis = new FileInputStream("d:\\12\\writer.txt");
        InputStreamReader isr = new InputStreamReader(fis,"utf-8");
        FileOutputStream fos = new FileOutputStream("d:\\12\\writer1.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
        int len = 0;
        while((len=isr.read())!=-1){
            osw.write(len);
            osw.flush();
        }
        isr.close();
        osw.close();

緩衝流:加速流,能夠把字節流和字符流加速,由於有個緩衝區,先把數據存到緩衝區,而後再一次性讀取或者寫入,通常使用默認的緩衝區大小(8KB)就能夠

字節緩衝流:bufferedoutputstream   字節緩衝輸出流;bufferedinputstream   字節緩衝輸入流

構造方法:由於是加速字節流的,因此必須傳入一個字節輸出流或者字節輸入流對象

方法:和字節流的方法同樣

// TODO Auto-generated method stub
        FileInputStream fis = new FileInputStream("d:\\12\\output.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos = new FileOutputStream("d:\\12\\put.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len=bis.read(bytes))!=-1){
            bos.write(bytes, 0, len);
            bos.flush();
        }
        bos.close();
        bis.close();

字符緩衝流:bufferedwriter   字符緩衝輸出流;bufferedreader   字符緩衝輸入流

構造方法:由於是加速字符流的,因此必須傳入一個字符輸出流或者字符輸入流

方法:和字符流的方法同樣;可是在字符緩衝輸出流中,有一個newline()方法,換行;在字符緩衝輸入流中,有一個readline()方法,讀取一個文本行,返回值是一個字符串

FileWriter fw = new FileWriter("d:\\12\\output.txt",true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("你好嗎");
        bw.newLine();
        bw.flush();
        bw.write("我很好");
        bw.close();
FileReader fr = new FileReader("d:\\12\\output.txt");
        BufferedReader br = new BufferedReader(fr);
        String str = "";
        while((str=br.readLine())!=null){
            System.out.println(str);
        }

複製:加速流複製大文件速度快

FileReader fr = new FileReader("d:\\12\\output.txt");
        BufferedReader br = new BufferedReader(fr);
        FileWriter fw = new FileWriter("d:\\12\\puttwo.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        String str = "";
        while((str = br.readLine())!=null){
            bw.write(str);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();

properties

一個hashtable的子類,只能存儲字符串類型的鍵值對,持久化的屬性集,有方法和流結合

有properties擴展名的文件

構造方法:使用空參構造

方法:store()方法,傳入一個字節輸出流或者字符輸出流;load()方法,出入一個字節輸入流或者字符輸入流

 setproperties()方法,設置鍵值對;stringPropertyNames()方法,遍歷鍵值,返回set集合;getproperties()方法,得到值

Properties pro = new Properties();
        pro.setProperty("a", "1");
        pro.setProperty("b", "2");
        pro.setProperty("c", "3");
//        String str = pro.getProperty("a");
//        System.out.println(str);
        Set<String> set = pro.stringPropertyNames();
        for(String s:set){
            System.out.println(pro.getProperty(s));

傳入字節流:

Properties pro = new Properties();
        FileInputStream fis = new FileInputStream("d:\\12\\pro.properties");
        pro.load(fis);
        Set<String> set = pro.stringPropertyNames();
        for(String s:set){
            System.out.println(pro.getProperty(s));
        }
        fis.close();
Properties pro = new Properties();
        pro.setProperty("school", "qinghua");
        pro.setProperty("father", "ligang");
        FileOutputStream fos = new FileOutputStream("d:\\12\\pro.properties",true);
        pro.store(fos, "message");
        fos.close();

 

複製:

Properties pro = new Properties();
        FileReader fr = new FileReader("d:\\12\\pro.properties");
        FileWriter fw = new FileWriter("d:\\12\\proone.properties");
        pro.load(fr);
        //Set<String> set = pro.stringPropertyNames();
//        for(String s:set){
//            System.out.println(pro.getProperty(s));
//        }
        pro.store(fw, "");

序列化流和反序列化流

操做對象的,對象類的創建必須實現Serializable接口,不然無法存入流中,不想把某個成員變量存入流,要用瞬態關鍵詞修飾transient,這是個字節流

objectoutputstream:序列化流,把對象寫入流中

構造方法:傳入一個字節輸出流

方法:writeobject(),傳入一個對象,並把對象寫入流中

FileOutputStream fos = new FileOutputStream("d:\\12\\dog.txt");
        ObjectOutputStream os = new ObjectOutputStream(fos);
        Dog d = new Dog();
        d.setName("java");
        d.setColor("white");
        os.writeObject(d);
        os.close();
        System.out.println(d);

objectinputstream:反序列化流,從流中讀取對象

構造方法:傳入一個字節輸入流

方法:readobject(),從流中讀取對象,返回一個object超類(要用建立的類接收的話,須要多態-向下轉型)

FileInputStream fis = new FileInputStream("d:\\12\\dog.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Dog d = (Dog)ois.readObject();
        System.out.println(d);

注意:對象必定要實現接口

 打印流

printstream:字節打印流   printwriter:字符打印流

通常使用字符打印流,由於字符打印流的構造方法比字節打印流多一個

構造方法:能夠傳入一個file對象,能夠傳入一個字符串,能夠傳入一個字節輸出流,能夠傳入一個字符輸出流

方法:print(),println(),write()

File f = new File("d:\\12\\output.txt");
        PrintWriter pw = new PrintWriter(f);
        pw.print("今天我養了一隻狗");
        pw.write(100);
        pw.print(100);
        //write方法走碼錶,print方法原樣輸出
        pw.flush();
        pw.close();

自動刷新:在構造方法的字節流或字符流的後面加上一個true

FileOutputStream fos = new FileOutputStream("d:\\12\\output.txt");
        PrintWriter pw = new PrintWriter(fos,true);
        pw.println("你好呀");
        pw.close();

io流操做工具類

須要導入第三方包的class文件

filenameutils類:用來處理文件或目錄的名稱

getextention()方法:獲取擴展名

getName()方法:得到文件名

isextention():是以哪一個擴展名結尾嗎

String str = FilenameUtils.getExtension("d:\\12\\output.txt");
        //System.out.println(str);
        String str2 = FilenameUtils.getName("d:\\12\\output.txt");
        //System.out.println(str2);
        boolean flag = FilenameUtils.isExtension("d\\12\\output.txt","txt");
        //System.out.println(flag);

fileutils類:操做文件

writestringtofile()方法:把字符串寫入文件中

readfiletostring()方法:從文件中讀取內容

copydirectorytodirectory()方法:賦值文件夾

copyfile()方法:複製文件

FileUtils.writeStringToFile(new File("d:\\12\\output.txt"), "你好",true);
        String file = FileUtils.readFileToString(new File("d:\\12\\output.txt"));
        System.out.println(file);
        FileUtils.copyDirectoryToDirectory(new File("d:\\123"), new File("d:\\12"));
        FileUtils.copyFile(new File("d:\\12\\output.txt"), new File("d:\\12\\123.txt"));

 

 不是java自帶的類,須要創建文件夾導入

相關文章
相關標籤/搜索