开花流水

空山无人,水流花开。

BlogJava 首页 新随笔 联系 聚合 管理
  79 Posts :: 42 Stories :: 160 Comments :: 0 Trackbacks

/**
  * 转移文件路径
  *
  * @param filename
  *            文件名
  * @param oldpath
  *            旧文件路径
  * @param newpath
  *            新文件路径
  * @param cover
  *            若新目录下存在和转移文件具有相同文件名的文件时,是否覆盖新目录下文件,cover=true将会覆盖原文件,否则不操作
  */
 public void changeDirectory(String fileName, String oldPath,
   String newPath, boolean cover) {
  if (!oldPath.equals(newPath)) {
   File oldFile = new File(oldPath + "/" + fileName);
   File newFile = new File(newPath + "/" + fileName);
   if (newFile.exists()) {
    if (cover) {
     newFile.delete();
     oldFile.renameTo(newFile);
    } else {
     System.out.println("新目录已经存在:" + fileName);
    }
   } else {
    oldFile.renameTo(newFile);
   }
  }
 }

 /**
  * 文件重命名
  *
  * @param path
  *            文件目录
  * @param oldname
  *            原来的文件名
  * @param newname
  *            新文件名
  */
 public void renameFile(String path, String oldname, String newname) {
  if (!oldname.equals(newname)) {
   File oldfile = new File(path + "/" + oldname);
   File newfile = new File(path + "/" + newname);
   if (newfile.exists()) {// 若在该目录下已经有一个文件和新文件名相同,则删除重建
    System.out.println(newname + "已经存在!");
    newfile.delete();
    oldfile.renameTo(newfile);
   } else {
    oldfile.renameTo(newfile);
   }
  }
 }

 /**
  * 创建文件夹
  *
  * @param path
  *            文件路径
  */
 public void createDir(String path) {
  String pathes[] = path.split("/");
  String filePath = "";
  for (int i = 0; i < pathes.length; i++) {
   filePath += pathes[i];
   filePath += "/";
   File dirPath = new File(filePath);
   if (!dirPath.exists())
    dirPath.mkdir();
  }
 }

posted on 2009-10-22 16:11 开花流水 阅读(236) 评论(0)  编辑  收藏 所属分类: java技术

只有注册用户登录后才能发表评论。


网站导航: