快速业务通道

Web项目: Java在部署项目的WebRoot下建立文件夹(附上文件操作类) - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-19
ile.canRead()) //不可读 throw new RuntimeException("FileCopy: source file"+source_name +"is unreadable"); if(dest_file.exists()) { if(dest_file.isFile()) { if(type==1) { dest_file.delete(); //覆盖 result = 1 ; } else if(type==2) { result = 2; return result; //不覆盖 ,程序返回。 } } else throw new RuntimeException("FileCopy: destination"+dest_name +"is not a file."); } else { File parentDir = new File(dest_file.getParent()); //获得目录信息 if(!parentDir.exists()) { throw new RuntimeException("FileCopy: destination"+dest_name +"directory doesn''t exist."); //目录不存在 } if(!parentDir.canWrite()) throw new RuntimeException("FileCopy: destination"+dest_name +"is unwriteable."); //目录不可写 } //开始复制文件 //输入流 source = new FileInputStream(source_file); bis = new BufferedInputStream(source); //输出流 dest = new FileOutputStream(dest_file); bos = new BufferedOutputStream(dest); buffer = new byte[1024*5]; while((byte_read=bis.read(buffer))!=-1) { bos.write(buffer, 0, byte_read); } result = 1; } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { if(source!=null){ bis.close(); source.close(); } if(dest!=null) { bos.flush(); bos.close(); dest.flush(); dest.close(); } } return result; } /**根据源路径名和目标路径名复制目录。 * 复制目录以复制文件为基础,通过递归复制子目录实现。 * @param source_name 源路径名 * @param dest_name 目标路径名 * @param type 值为判断如果目标路径存在是否覆盖,1为覆盖旧的目录,2为不覆盖,操作取消。 * @return 当复制成功时返回1, 当目标目录存在且type值为2时返回2,其他情况返回0 * @throws IOException 发生I/O错误 */ public int copyDirectory( String source_name, String dest_name, int type ) throws IOException { File source_file = new File(source_name); File dest_file = new File(dest_name); int result = 0; Delete del = new Delete(); //用于删除目录文件 if(!source_file.exists()||source_file.isFile()) //不存在 throw new RuntimeException("DirCopy: no such dir"+source_name); if(!source_file.canRead()) //不可读 throw new RuntimeException("DirCopy: source file"+source_name +"is unreadable"); if(dest_file.exists()) { if(type==1) { del.deleteDir(dest_name); //覆盖 result = 1; } if(type==2) { result = 2; //不覆盖 return result; } } if(!dest_file.exists()) { new File(dest_name).mkdirs(); //创建目标目录 File[] fileList = source_file.listFiles(); for(int i=0;i<fileList.length;i++){ System.out.println(fileList[i].getName()); if(fileList[i].isFile()){ //用copyFile函数复制文件 this.copyFile( source_name+"/"+fileList[i].getName(), dest_name+"/"+fileList[i].getName(), type); } else if(fileList[i].isDirectory()){ //递归 copyDirectory( source_name+"/"+fileList[i].getName(), dest_name+"/"+fileList[i].getName(), type); } } result = 1; } return result; } } package fileOperation; import java.io.File; /**创建一个新的文件或目录 * @author wakin * */ public class Create { /

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号