IO-文件 File 複製 讀寫 總結

必定要注意:
傳入的參數,應該是包含文件名的完整路徑名, 不能把一個文件複製到【文件夾】中,由於【文件夾】自己是不能有輸入輸出流的,只能複製到一個【文件】中,不然會報異常。

以字節流讀寫的三種方式
      
      
      
      
public class Test {
    private static final String FILE_PATH = "e:\\";
    private static final String FILE_TYPE = ".exe";
    private static final String FILE_FROM = FILE_PATH + 0 + FILE_TYPE;
    private static final String COPY_FILE_1 = FILE_PATH + 1 + FILE_TYPE;
    private static final String COPY_FILE_2 = FILE_PATH + 2 + FILE_TYPE;
    private static final String COPY_FILE_3 = FILE_PATH + 3 + FILE_TYPE;
    public static void main(String[] args) throws IOException {
        copyByBufStream(FILE_FROMCOPY_FILE_1);
        copyByBufArray(FILE_FROMCOPY_FILE_2);
        copyByByte(FILE_FROMCOPY_FILE_3);
    }
    /**
     * 利用緩衝輸入流讀取到一個緩衝容器後再寫入。建議使用
     */
    public static boolean copyByBufStream(String filePathFrom, String filePathTo) {
        try {
            BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(filePathFrom));
            BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(filePathTo));
            int ch = 0;
            while ((ch = bufis.read()) != -1) {
                bufos.write(ch);
            }
            bufos.close();
            bufis.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 每次讀取一個指定長度數組的字節。建議使用
     */
    public static boolean copyByBufArray(String filePathFrom, String filePathTo) {
        try {
            FileInputStream fis = new FileInputStream(filePathFrom);
            FileOutputStream fos = new FileOutputStream(filePathTo);
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fis.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fos.close();
            fis.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 一次讀取一個字節。千萬不要用,速度超級慢!
     */
    public static boolean copyByByte(String filePathFrom, String filePathTo) {
        try {
            FileInputStream fis = new FileInputStream(filePathFrom);
            FileOutputStream fos = new FileOutputStream(filePathTo);
            int ch = 0;
            while ((ch = fis.read()) != -1) {
                fos.write(ch);
            }
            fos.close();
            fis.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

以字符流讀寫的三種方式
      
      
      
      
public class Test {
    private static final String FILE_PATH = "e:\\";
    private static final String FILE_TYPE = ".txt";
    private static final String FILE_FROM = FILE_PATH + 0 + FILE_TYPE;
    private static final String COPY_FILE_1 = FILE_PATH + 1 + FILE_TYPE;
    private static final String COPY_FILE_2 = FILE_PATH + 2 + FILE_TYPE;
    private static final String COPY_FILE_3 = FILE_PATH + 3 + FILE_TYPE;
    public static void main(String[] args) throws IOException {
        //注意,只能複製純文本格式的文件,不然就會出現亂碼
        copyByBufLine(FILE_FROMCOPY_FILE_1);
        copyByBufArray(FILE_FROMCOPY_FILE_2);
        copyByChar(FILE_FROMCOPY_FILE_3);
    }
    /**
     * 一次寫入一行字符
     */
    public static boolean copyByBufLine(String filePathFrom, String filePathTo) {
        try {
            BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));
            BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));
            String line = null;
            //另外開闢一個緩衝區,存儲讀取的一行數據,返回包含該行內容的字符串,不包含換行符,若是已到達流末尾,則返回【 null】
            while ((line = bufr.readLine()) != null) {
                bufw.write(line);
                bufw.newLine();// 寫入一個行分隔符
                bufw.flush();
            }
            bufr.close();
            bufw.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 一次寫入指定個數的字符
     */
    public static boolean copyByBufArray(String filePathFrom, String filePathTo) {
        try {
            BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));
            BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));
            char[] buf = new char[1024];
            int len = 0;
            while ((len = bufr.read(buf)) != -1) {
                bufw.write(buf, 0, len);
                bufw.flush();
                len = 0;
            }
            bufr.close();
            bufw.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 一次寫入一個字符
     */
    public static boolean copyByChar(String filePathFrom, String filePathTo) {
        try {
            BufferedReader bufr = new BufferedReader(new FileReader(filePathFrom));
            BufferedWriter bufw = new BufferedWriter(new FileWriter(filePathTo));
            int ch = 0;
            while ((ch = bufr.read()) != -1) {
                bufw.write(ch);//寫入單個字符
            }
            bufr.close();
            bufw.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}



相關文章
相關標籤/搜索