天天看點

位元組流寫資料(位元組輸出流)

FileOutputStream :檔案輸入流用于将資料寫入File

FileOutputStream​(String name) 建立檔案輸出流以指定的名稱寫入檔案。

public class FileOutputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        //建立位元組輸出流對象
        //FileOutputStream​(String name) 建立檔案輸出流以指定的名稱寫入檔案。
        FileOutputStream fos = new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\fos.txt");
/*
*       1.調用系統功能建立檔案
*       2.建立位元組流輸出對象
*       3.讓位元組流輸出對象指向建立好的檔案
* */


        //void write​(int b) 将指定的位元組寫入此檔案輸出流。

        fos.write(97);
//        fos.write(57);
//        fos.write(55);

        //最後需要釋放資源
        //void close​() 關閉此檔案輸出流并釋放與此流相關聯的任何系統資源。
        fos.close();
    }

}
           

void write​(int b) 将指定的位元組寫入此檔案輸出流。

void write​(byte[] b) 将 b.length位元組從指定的位元組數組寫入此檔案輸出流。

void write​(byte[] b, int off, int len) 将 len位元組從指定的位元組數組開始,從偏移量 off開始寫入此檔案輸出流。

public class FileOutputStreamDemo02 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\fos.txt");


//        File file = new File("C:\\Users\\PCTC\\Desktop\\file\\fos.txt");
//        FileOutputStream fos2 = new FileOutputStream(file);

//        FileOutputStream fos3 = new FileOutputStream(new File("C:\\Users\\PCTC\\Desktop\\file\\fos.txt"));
        //這種寫法實際用法和上面一緻

        //void write​(int b)      将指定的位元組寫入此檔案輸出流。
//        fos.write(97);
//        fos.write(98);
//        fos.write(99);


        //void write​(byte[] b)    将 b.length位元組從指定的位元組數組寫入此檔案輸出流。

//        byte[] bys = {97,98,99,100,110};

//         byte[] getBytes(); 傳回字元串對應的位元組數組
        byte[] bys = "12346".getBytes();
//        fos.write(bys);

        //void write​(byte[] b, int off, int len)     将 len位元組從指定的位元組數組開始,從偏移量 off開始寫入此檔案輸出流。
        fos.write(bys,0,bys.length);
        //釋放資源
        fos.close();
    }
}
           

位元組流寫資料兩個問題:

1.位元組流如何實作換行

Windows:/r/n

Linux: /n

mac: /r

2.如何追加寫入

public FileOutputStream​(String name,boolean append)

name - 與系統相關的檔案名

append - 如果是 true ,則位元組将寫入檔案的末尾而不是開頭

public class FileOutputStreamDemo03 {
    public static void main(String[] args) throws IOException {
        //建立位元組輸出流對象
//        FileOutputStream fos = new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\fos.txt");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\fos.txt",true);

        //寫資料
        for (int i = 0; i < 10;i++){
            fos.write("hello".getBytes());
            fos.write("\n".getBytes());
        }
        fos.close();
    }
}

           

加入finally來釋放資源

public class FileOutputStreamDemo04 {
    public static void main(String[] args) {
        //加入finally來釋放資源
        FileOutputStream  fos  = null;
        try {
            fos = new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\fos.txt");
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}