快速业务通道

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

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-19
** * 根据路径名创建文件 * @param filePath 路径名 * @return 当创建文件成功后,返回true,否则返回false. * */ public boolean createFile(String filePath) { boolean isDone = false; File file = new File(filePath); if(file.exists()) throw new RuntimeException("File: "+filePath+" is already exist"); try { isDone = file.createNewFile(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return isDone; } /** * 根据路径名创建目录 * @param filePath 路径名 * @return 当创建目录成功后,返回true,否则返回false. */ public boolean createDir(String filePath) { boolean isDone = false; File file = new File(filePath); if(file.exists()) throw new RuntimeException("FileDirectory: "+filePath+" is already exist"); isDone = file.mkdirs(); return isDone; } } package fileOperation; import java.io.File; import java.io.IOException; /** * 删除一个文件或目录 * @author wakin * */ public class Delete { /** * 删除路径名所代表的文件。 * @param filePath 路径名 * @return 当文件成功删除,返回true,否则返回false. */ public boolean deleteFile(String filePath) throws IOException{ File file = new File(filePath); if(file.exists()) { file.delete(); //System.out.println(filePath+"文件已删除."); return true; } else { //System.out.println("逻辑错误:"+filePath+"文件不存在."); return false; } } /** * 删除路径名所代表的目录 * @param filePath 路径名 * @return 当目录成功删除,返回true,否则返回false. */ public boolean deleteDir(String filePath) throws IOException{ boolean isDone = false; File file = new File(filePath); //判断是文件还是目录 if(file.exists()&&file.isDirectory()){ if(file.listFiles().length==0){ file.delete(); isDone = true; } else { File [] delFiles = file.listFiles(); for(int i=0;i<delFiles.length;i++){ if(delFiles[i].isDirectory()){ deleteDir(delFiles[i].getAbsolutePath()); //递归调用deleteDir函数 } else { delFiles[i].delete(); } } } //删除最后剩下的目录名。 deleteDir(filePath); isDone = true; } else return false; return isDone; } } package fileOperation; import java.io.File; import java.io.IOException; /** * 移动文件/目录,利用Delete类和Copy类来实现。 * @author wakin * */ public class Move { /** * 利用Copy类的函数和Delete类来完成移动文件/目录的操作。 * @param source_name 源路径名 * @param dest_name 目标路径名 * @param type type 值为判断如果目标路径存在是否覆盖,1为覆盖旧的文件/目录,2为不覆盖操作取消。 * @return 当移动成功后返回1,当目标目录存在且type值为2时返回2,其他情况返回0 * @throws IOException 发生I/O错误 */ public int move(String source_name,String dest_name,int type) throws IOException{ int result = 0; Copy copy = new Copy(); Delete delete = new Delete(); File source_file = new File(source_name); //File dest_file = new File(dest_name); if(!source_file.exists()) throw new RuntimeException("FileMove: no such source file:"+source_name); if(source_file.isFile()){ result = copy.copyFile(source_name, dest_name, type); //调用Copy类的copyFile函数 if(result ==1) del

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站: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号