天天看點

File操作-InputStream/OutputStream及是否建立檔案

處理的都是位元組流,寫入的時候也為位元組流,不能直接寫入中文字元

1.實作檔案複制

示例代碼如下:

public void copyFile(String src, String dest) {
        // 1.提供讀入、寫出的檔案
        File file1 = new File(src);
        File file2 = new File(dest);
        // 2.提供相應的流
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2);
            // 3.實作檔案的複制
            byte[] b = new byte[1024];
            int len;
            while ((len = fis.read(b)) != -1) {
                // fos.write(b);//錯誤的寫法兩種: fos.write(b,0,b.length);
                fos.write(b, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }      

2.FileOutputStream寫入檔案

寫入的時候一定要轉換為位元組,getBytes();如果目标檔案不存在,則FileOutputStream會建立目标檔案(前提是父路徑檔案對象必須存在)。

如果檔案存在,會将原有的檔案覆寫。若加上參數true,則為append。

@Test
    public void testFileOutputStream() {
        // 1.建立一個File對象,表明要寫入的檔案位置。
        File file = new File("test7.txt");
        // 2.建立一個FileOutputStream的對象,将file的對象作為形參傳遞給FileOutputStream的構造器中
        FileOutputStream fos = null;
        try {
            // 輸出的實體檔案可以不存在,當執行過程中,若不存在,會自動的建立。若存在,會将原有的檔案覆寫
            //若加上參數true,則為append;不加,則為覆寫
            //fos = new FileOutputStream(file,true);
            fos = new FileOutputStream(file);
            // 3.寫入的操作--将String轉換為位元組數組
            fos.write(new String("I love China,你呢!").getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 4.關閉輸出流
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }      

3.FileInputStream讀出檔案

隻能處理位元組流,英文字元,不能進行中文字元

@Test
    public void testFileInputStream3() { // abcdefgcde
        FileInputStream fis = null;
        try {
            File file = new File("test7.txt");
            fis = new FileInputStream(file);
            byte[] b = new byte[5];// 讀取到的資料要寫入的數組。
            int len;// 每次讀入到byte中的位元組的長度,若無則傳回-1
            while ((len = fis.read(b)) != -1) {
                // for (int i = 0; i < len; i++) {
                // System.out.print((char) b[i]);
                // }
                String str = new String(b, 0, len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



/*可以進行中文字元和英文字元*/
    @Test
    public void testFileInputStream4() { // abcdefgcde
        FileInputStream fis = null;
        try {
            File file = new File("hello.txt");
            fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            char[] b = new char[5];// 讀取到的資料要寫入的數組。
            int len;// 每次讀入到char[]中的長度,若無則傳回-1
            while ((len = isr.read(b)) != -1) {
                 System.out.println(len);
                 for (int i = 0; i < len; i++) {
                 System.out.print((char) b[i]);
                 }
                String str = new String(b, 0, len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }
    }      
new FileOutputStream("name")會産生一個新檔案;

new FileInputStream("name")不會建立新檔案,若檔案不存在會報錯;

file=new File("name")+new FileInputStream(file)

不會建立新檔案,若檔案不存在也會報錯;

file=new File("name")+new FileOutputStream(file),會建立新檔案。