模拟试题

3773考试网计算机等级考试模拟试题正文

2011计算机二级JAVA编程:拷贝一个目录或者文件到指定路径下

来源:fjzsksw.com 2010-11-5 9:23:39

 

 

 拷贝一个目录或者文件到指定路径下

  /**

  * 拷贝一个目录或者文件到指定路径下

  *

  * @param source

  * @param target

  */

  public static void copy(File source, File target)

  {

  File tarpath = new File(target, source.getName());

  if (source.isDirectory())

  {

  tarpath.mkdir();

  File[] dir = source.listFiles();

  for (int i = 0; i < dir.length; i++) { copy(dir[i], tarpath); } } else

  {

  try

  {

  InputStream is = new FileInputStream(source);

  OutputStream os = new FileOutputStream(tarpath);

  byte[] buf = new byte[1024];

  int len = 0;

  while ((len = is.read(buf)) != -1)

  {

  os.write(buf, 0, len);

  }

  is.close();

  os.close();

  }

  catch (FileNotFoundException e)

  {

  e.printStackTrace();

  }

  catch (IOException e)

  {

  e.printStackTrace();

  }

  }

  }

 

 

触屏版 电脑版
3773考试网 琼ICP备12003406号-1