Java implements file copying

package cn.weida.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 *
 * file copy
 * srcpath source file directory
 * aimpath target file
 * @author 24569
 *
 */
public class FileUtils {
 public static void copyFile(String srcpath, String aimpath) throws FileNotFoundException, IOException {
  File src = new File("srcpath");
  File aim = new File("aimpath");
  copyFile(src, aim);
 }
 public static void copyFile(File src, File aim) throws FileNotFoundException, IOException {
  if (!src.isFile()) {
   throw new IOException("只能拷贝文件");
  }
  InputStream in = new FileInputStream(src);
  OutputStream out = new FileOutputStream(aim);
 /* byte[] car = new byte[10240];
  int len = 0;
  while (-1 != (len = in.read(car))) {
   out.write(car);
  }
  */
  /*String line = null;
  while (null!=(line=in.readLine())){
   out.write(line);
  }
  */
  in.close();
  out.close();
 }
 // copy the file
 public static void copyDictory(String srcpath, String aimpath) throws FileNotFoundException, IOException {
  File src = new File(srcpath);
  File aim = new File(aimpath);
  copyDictory(src, aim);
 }
 public static void copyDictory(File src, File aim) throws FileNotFoundException, IOException {
  if (aim.isDirectory() && src.isFile()) {
   FileUtils.copyDictory(src, new File(aim.getPath(), src.getName()));
   return;
  } else if (src.isFile()) {
   FileUtils.copyFile(src, aim);
   return;
  } else {
   aim = new File(aim, src.getName());
   if (aim.isDirectory()) {
    throw new IOException(aim.getAbsolutePath() + "不能建立同名的文件夹");
   }
   aim.mkdirs();
   File[] Lin = src.listFiles();
   for (File temp : Lin) {
    copyDictory(temp, new File(aim, temp.getName()));
   }
  }
  return;
 }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325403723&siteId=291194637