为什么文件重命名失败?
我的操作系统是Windows7,文件系统中存在文件夹C:/test/dfhsdfhs。
我的代码:
String path = "C:/test/dfhsdfhs/test2.txt";
boolean hasDeleteFolder = true;
File delFile = new File(path);
if (delFile.exists()) {
if (hasDeleteFolder == true) {
Date dateTimeNow = new Date();
String _dateTimeNowStr = dateTimeNow.toString();
_dateTimeNowStr = _dateTimeNowStr.replace(" ", "_");
File timeStampFile = new File (delFile.getAbsolutePath() + "_" + _dateTimeNowStr + "." + FilenameUtils.getExtension(delFile.getName()));
if (delFile.renameTo(timeStampFile)) {
System.out.println("renamed");
} else {
System.out.println("Error");
}
}
}发布于 2016-08-03 17:39:59
它失败是因为您的时间戳字符串包含windows操作系统不允许的:字符。替换它们,它就会起作用。
_dateTimeNowStr = _dateTimeNowStr.replace(":", "_");https://stackoverflow.com/questions/38739768
复制相似问题