先说总结:
复制文本文件,既可用字符流方式,也可以用字节流方式.
复制非文本文件,只能用字节流方式.
字符流的读取与写入方式搭配如下:
按单个字符 | 按字符数组 | 按行 | |
字符输入流FileReader | read() | read(char[] cbuf) | |
字符输出流FIleWriter | write(int c) | write(char[] cbuf,int off,int len) | |
缓冲字符输入流BufferedFileReader | read() | read(int c) | readLine() |
缓冲字符输出流BufferedFileWriter | write(int c) | write(char[] cbuf,int off,int len) | writeLine(String str) |
字节流的读取与写入方式:
按单个字节 | 按字节数组 | 按行 | |
字节流输入流FileInputStream | read() | read(byte[] b) | |
字节输出流FIleOutputStream | write(int c) | write(byte[] b,int off,int len) | |
缓冲字节输入流BufferedInputStream | read() | read(byte[] b) | |
缓冲字节输出流BufferedOutputStream | write(int c) | write(byte[] b,int off,int len) |
因此,利用Java IO复制文件有如下9种方式:
1.字符流读写复制文件,每次复制一个字符:
/** * 字符流方式复制文件,一次复制一个字节 * @param srcFilePath 源文件路径 * @param destFilePath 新文件路径 */ public static void copyFileReaderToWriterChar(String srcFilePath,String destFilePath){ FileReader fileReader = null; FileWriter fileWriter = null; try{ fileReader = new FileReader(srcFilePath); fileWriter = new FileWriter(destFilePath); int charRead; while((charRead = fileReader.read()) != -1){//读取单个字符,-1代表已达到流的末尾 fileWriter.write(charRead);//写入单个字符 } fileWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != fileReader){ try{ fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != fileWriter){ try{ fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2.字符流读写复制文件,每次复制一个字符数组:
/** * 字符流方式复制文件,一次复制一个字符数组 * @param srcFilePath 源文件路径 * @param destFilePath 新文件路径 */ public static void copyFileReaderToWriterCharArray(String srcFilePath,String destFilePath){ FileReader fileReader = null; FileWriter fileWriter = null; try{ fileReader = new FileReader(srcFilePath); fileWriter = new FileWriter(destFilePath); char[] charArray = new char[1024];//缓冲数组 int readLength;//每次读取的长度 // /** * 每次读取charArray.length个字符存入charArray,-1代表已经到流的末尾 * readLength为读取的长度,一般为charArray.length,最后一次循环时一般不足charArray.length,为实际读取长度 */ while((readLength = fileReader.read(charArray)) != -1){ fileWriter.write(charArray,0,readLength);//写入读取的字符数组. } fileWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != fileReader){ try{ fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != fileWriter){ try{ fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
3.缓冲字符流读写复制文件,每次复制复制一个字符:
/** * 使用缓冲字符流复制文件,每次复制一个字符 * @param srcFilePath 源文件 * @param destFilePath 新文件 */ public static void copyFileBufferedReaderToBufferedWriterChar(String srcFilePath,String destFilePath){ FileReader fileReader = null; FileWriter fileWriter = null; BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try{ fileReader = new FileReader(srcFilePath); fileWriter = new FileWriter(destFilePath); bufferedReader = new BufferedReader(fileReader); bufferedWriter = new BufferedWriter(fileWriter); int charRead;//保存每次循环读取的字符 while ((charRead = bufferedReader.read()) != -1){//读取一个字符,-1代表读到流末尾 bufferedWriter.write(charRead);//写入读到的字符 } bufferedWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != fileReader){ try{ fileReader.close(); }catch (IOException e) { e.printStackTrace(); } } if(null != bufferedReader){ try{ bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != fileWriter){ try{ fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != bufferedWriter){ try{ bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
4.缓冲字符流读写复制文件,每次复制一个字符数组:
/** * 使用缓冲字符流复制文件,每次复制一个字符数组 * @param srcFilePath 源文件 * @param destFilePath 新文件 */ public static void copyFileBufferedReaderToBufferedWriterCharArray(String srcFilePath,String destFilePath){ FileReader fileReader = null; FileWriter fileWriter = null; BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try{ fileReader = new FileReader(srcFilePath); fileWriter = new FileWriter(destFilePath); bufferedReader = new BufferedReader(fileReader); bufferedWriter = new BufferedWriter(fileWriter); char[] charArray = new char[1024]; int lengthRead; while ((lengthRead = bufferedReader.read(charArray)) != -1){//读取一个字符,-1代表读到流末尾 bufferedWriter.write(charArray,0,lengthRead);//写入读到的字符 } bufferedWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != fileReader){ try{ fileReader.close(); }catch (IOException e) { e.printStackTrace(); } } if(null != bufferedReader){ try{ bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != fileWriter){ try{ fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != bufferedWriter){ try{ bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
5.缓冲字符流读写复制文件,每次复制一行:
/** * 使用缓冲字符流复制文件,每次复制一行 * @param srcFilePath 源文件 * @param destFilePath 新文件 */ public static void copyFileBufferedReaderToBufferedWriterString(String srcFilePath,String destFilePath){ FileReader fileReader = null; FileWriter fileWriter = null; BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try{ fileReader = new FileReader(srcFilePath); fileWriter = new FileWriter(destFilePath); bufferedReader = new BufferedReader(fileReader); bufferedWriter = new BufferedWriter(fileWriter); String lineRead; while ((lineRead = bufferedReader.readLine()) != null){//读取一行,null代表已经读到流末尾 bufferedWriter.write(lineRead); bufferedWriter.newLine(); bufferedWriter.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != fileReader){ try{ fileReader.close(); }catch (IOException e) { e.printStackTrace(); } } if(null != bufferedReader){ try{ bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != fileWriter){ try{ fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != bufferedWriter){ try{ bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
6.字节流读写复制文件,每次复制一个字节:
/** * 字节流方式复制文件,一次复制一个字节 * @param srcFilePath 源文件路径 * @param destFilePath 新文件路径 */ public static void copyFileInputStreamToOutputStreamChar(String srcFilePath,String destFilePath){ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try{ fileInputStream = new FileInputStream(srcFilePath); fileOutputStream = new FileOutputStream(destFilePath); int charRead; while((charRead = fileInputStream.read()) != -1){//读取单个字节,-1代表已达到流的末尾 fileOutputStream.write(charRead);//写入单个字节 } fileOutputStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != fileInputStream){ try{ fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != fileOutputStream){ try{ fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
7.字节流读写复制文件,每次复制一个字节数组:
/** * 字节流方式复制文件,一次复制一个字节数组 * @param srcFilePath 源文件路径 * @param destFilePath 新文件路径 */ public static void copyFileInputStreamToOutputStreamCharArray(String srcFilePath,String destFilePath){ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try{ fileInputStream = new FileInputStream(srcFilePath); fileOutputStream = new FileOutputStream(destFilePath); byte[] byteArray = new byte[1024]; int readLength;//每次读取的长度 // /** * 每次读取byteArray.length个字符存入byteArray,-1代表已经到流的末尾 * readLength为读取的长度,一般为byteArray.length,最后一次循环时一般不足byteArray.length,为实际读取长度 */ while((readLength = fileInputStream.read(byteArray)) != -1){ fileOutputStream.write(byteArray,0,readLength);//写入读取的字节数组. } fileOutputStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != fileInputStream){ try{ fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != fileOutputStream){ try{ fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
8.缓冲字节流读写复制文件,每次复制一个字节:
/** * 使用缓冲字节流复制文件,每次复制一个字节 * @param srcFilePath 源文件 * @param destFilePath 新文件 */ public static void copyFileBufferedInputStreamToBufferedOutputStreamChar(String srcFilePath,String destFilePath){ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try{ fileInputStream = new FileInputStream(srcFilePath); fileOutputStream = new FileOutputStream(destFilePath); bufferedInputStream = new BufferedInputStream(fileInputStream); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); int charRead; while((charRead = bufferedInputStream.read()) != -1){//读取单个字节,-1代表已达到流的末尾 bufferedOutputStream.write(charRead);//写入单个字节 } bufferedOutputStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != fileInputStream){ try{ fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != fileOutputStream){ try{ fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != bufferedInputStream){ try{ bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != bufferedOutputStream){ try{ bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
9.缓冲字节流读写复制文件,每次复制一个字节数组:
/** * 使用缓冲字节流复制文件,每次复制一个字节数组 * @param srcFilePath 源文件 * @param destFilePath 新文件 */ public static void copyFileBufferedInputStreamToBufferedOutputStreamCharArray(String srcFilePath,String destFilePath){ FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try{ fileInputStream = new FileInputStream(srcFilePath); fileOutputStream = new FileOutputStream(destFilePath); bufferedInputStream = new BufferedInputStream(fileInputStream); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); byte[] byteArray = new byte[1024]; int readLength;//每次读取的长度 // /** * 每次读取byteArray.length个字符存入byteArray,-1代表已经到流的末尾 * readLength为读取的长度,一般为byteArray.length,最后一次循环时一般不足byteArray.length,为实际读取长度 */ while((readLength = bufferedInputStream.read(byteArray)) != -1){ bufferedOutputStream.write(byteArray,0,readLength);//写入读取的字节数组. } bufferedOutputStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(null != fileInputStream){ try{ fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != fileOutputStream){ try{ fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != bufferedInputStream){ try{ bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if(null != bufferedOutputStream){ try{ bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
显然,复制效率上,使用缓冲流快于不适用缓冲流;按数组快于按单个字符/字节;字符数组快于字节数组;
因此,
复制文本文件,使用方法4,缓冲字符流读写复制文件,每次复制一个字符数组.
复制非文本文件,使用方法9,缓冲字节流读写复制文件,每次复制一个字节数组.
参考:
https://www.cnblogs.com/pony1223/p/8030200.html