java 文件 流水_java中的部分文件操作

/**

* 转移文件路径

*

* @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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值