当前位置: 首页 > 科技观察

文件拷贝FileChannel

时间:2023-03-17 22:12:54 科技观察

/***导入*@paramurlPath附件相对路径(xml存放路径)*@parampath项目绝对路径*@paramkeyID导出信息的keyID*@paramfilepath导入后路径*/privatestaticvoidimportCopy(StringurlPath,Stringpath,StringkeyID,Stringfilepath){//生成目录Filef=newFile(path+filepath);if(!f.exists()){f.mkdirs();}Stringfilename=urlPath.substring(urlPath.lastIndexOf("/")+1);FileInputStreamfi=null;FileOutputStreamfo=null;FileChannelin=null;FileChannelout=null;try{fi=newFileInputStream(path+"/imp/"+keyID+"/"+filename);//源文件fo=newFileOutputStream(path+filepath+filename);//导入的文件in=fi.getChannel();//获取对应的文件通道out=fo.getChannel();//获取对应的文件通道in.transferTo(0,in.size(),out);//连接两个通道,从in通道读取,然后写入out通道}catch(IOExceptione){e.printStackTrace();}finally{try{fi.close();在。close();fo.close();out.close();}catch(IOExceptione){e.printStackTrace();}}}【本文为专栏作者张永波原创文章,转载请取得本站授权作者转载]