复制文件至指定位置并改名

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CopyFile {
   public void copyToSys(String old,String newName) throws Exception {
       String url1 = old;// 源文件位置
      String url2 = "C:\\dataapp\\upload\\"+newName;
       copy(url1, url2);
   }
   private static void copy(String url1, String url2) throws Exception {
       FileInputStream in = new FileInputStream(new File(url1));
      FileOutputStream out = new FileOutputStream(new File(url2));
     byte[] buff = new byte[512];
      int n = 0;
       while ((n = in.read(buff)) != -1) {
          out.write(buff, 0, n);
      }
       out.flush();
       in.close();
      out.close();
      System.out.println("复制完成");
   }
}

你可能感兴趣的:(复制文件至指定位置并改名)