成都网站建设设计

将想法与焦点和您一起共享

FileChannel进行文件复制

 
 
  1. /**
  2.  * 导入
  3.  * @param urlPath 附件相对路径(xml存储路径)
  4.  * @param path 项目绝对路径
  5.  * @param keyID 要导出信息的keyID
  6.  * @param filepath 导入后路径
  7.  */
  8. private static void importCopy(String urlPath, String path, String keyID, String filepath) {
  9.  // 生成目录
  10.  File f = new File(path + filepath);
  11.  if (!f.exists()) {
  12.  f.mkdirs();
  13.  }
  14.  String filename = urlPath.substring(urlPath.lastIndexOf("/") + 1);
  15.  FileInputStream fi = null;
  16.  FileOutputStream fo = null;
  17.  FileChannel in = null;
  18.  FileChannel out = null;
  19.  try {
  20.  fi = new FileInputStream(path + "/imp/" + keyID + "/" + filename); //源文件
  21.  fo = new FileOutputStream(path + filepath + filename); //导入后文件
  22.  in = fi.getChannel();// 得到对应的文件通道
  23.  out = fo.getChannel();// 得到对应的文件通道
  24.  in.transferTo(0, in.size(), out);// 连接两个通道,并且从in通道读取,然后写入out通道
  25.  } catch (IOException e) {
  26.  e.printStackTrace();
  27.  } finally {
  28.  try {
  29.  fi.close();
  30.  in.close();
  31.  fo.close();
  32.  out.close();
  33.  } catch (IOException e) {
  34.  e.printStackTrace();
  35.  }
  36.  }
  37. }

【本文是专栏作者张勇波的原创文章,转载请通过获取作者授权】

创新互联专注于高港网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供高港营销型网站建设,高港网站制作、高港网页设计、高港网站官网定制、微信小程序服务,打造高港网络公司原创品牌,更为您提供高港网站排名全网营销落地服务。


当前文章:FileChannel进行文件复制
本文地址:http://chengdu.cdxwcx.cn/article/dppsppo.html