Javaファイルをコピーするためによく使われる3つの方法


ファイルをコピーする3つの方法:
1、Files.co py(path,new FileOutputStream(dest));
2、バイトストリームを利用する。
3、文字の流れを利用する。
コードの実装は以下の通りです。

package com.tiger.io;
import java.io.*;
import java.nio.file.*;
/**
 *          
 * @author tiger
 * @Date 
 */
public class CopyFile {
 public static void main(String[] args) throws IOException, IOException {
 Path path = Paths.get("E:","17-06-15-am1.avi");
 String dest = "E:\\Copy  .avi";
 copy01(path, dest);
 String src = "E:\\[Java      1000 :Java  ].pdf";
 String dest1 = "E:\\CopyFile.pdf";
 copy02(src, dest1);
 //copy03(src, dest1);
 }
 
 /**
 *   Files  copy
 * @param path
 * @param dest
 * @throws IOException
 * @throws IOException
 */
 public static void copy01(Path path,String dest) throws IOException, IOException{
 //  Files          ,    ,      。
 Files.copy(path, new FileOutputStream(dest));
 }
 
 /**
 *        
 * @param src
 * @param dest
 * @throws IOException
 */
 public static void copy02(String src,String dest) throws IOException{
 InputStream is = new BufferedInputStream(new FileInputStream(src));
 OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));
 //    u,--   +  +  
 byte[] b = new byte[10];//    
 int len = 0;//    
 //    
 while (-1!=(len = is.read(b))) {
  //    ,    ,      。
  os.write(b,0,len);
 }
 //      
 os.flush();
 //   ,    
 os.close();
 is.close();
 }
 
 /**
 *      
 * @param src
 * @param dest
 * @throws IOException
 */
 public static void copy03(String src,String dest) throws IOException{
 //     
 BufferedReader reader = new BufferedReader(new FileReader(src));
 //     
 BufferedWriter writer = new BufferedWriter(new FileWriter(dest));
 char[] cbuf = new char[24];
 int len = 0;
 //      
 while ((len = reader.read(cbuf)) != -1) {
  writer.write(cbuf, 0, len);
 }
 //   
 writer.close();
 reader.close();
 }
}
締め括りをつける
以上はこの文章の全部の内容です。本文の内容は皆さんの学習や仕事に対して一定の参考学習価値を持ってほしいです。ありがとうございます。もっと知りたいなら、下のリンクを見てください。