使用renameTo和ArrayList移动多个文件的Java代码无效
当代码单独运行时,它正在工作。但是当使用使用renameTo和ArrayList移动多个文件的Java代码无效,java,file,Java,File,当代码单独运行时,它正在工作。但是当使用ArrayList时,它就不起作用了。除第0个索引中的文件外,它正在工作 if(last.size()!=0) { File source = new File("C:\\B\\"); File dest = new File("C:\\C\\"); DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date date = new Dat
ArrayList
时,它就不起作用了。除第0个索引中的文件外,它正在工作
if(last.size()!=0) {
File source = new File("C:\\B\\");
File dest = new File("C:\\C\\");
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
String ts = dateFormat.format(date);
for(int e=0;e<last.size();e++)
{
String zwq = (source + "\\" + last.get(e));
String zz = (dest + "\\" + addTimestamp(last.get(e), ts));
File f1 = new File(zwq);
System.out.println("The file to be moved is"+f1);
if(f1.renameTo(new File(zz))){
System.out.println("moved: " + zz);
} else {
System.out.println("unable to move: " + zz);
}
}
private static String addTimestamp(String name, String ts) {
int lastIndexOf = name.lastIndexOf('.');
return (lastIndexOf == -1 ?
name + "_" + ts
:
name.substring(0, lastIndexOf) + "_" + ts +
name.substring(lastIndexOf))
.replaceAll("[\\/:\\*\\?\"<>| ]", "_");
}
if(last.size()!=0){
文件源=新文件(“C:\\B\\”;
File dest=新文件(“C:\\C\\”;
DateFormat DateFormat=新的SimpleDateFormat(“dd/MM/yyyy HH:MM:ss”);
日期=新日期();
字符串ts=dateFormat.format(日期);
对于(int e=0;eq)您的问题是什么?您得到了什么输出?您正在三次重命名为相同的文件名System.out.println(新文件(zwq).renameTo(新文件(zz));if(f1.renameTo(新文件(zz)){System.out.println(f1.renameTo(新文件(zz)));我想将一些文件从文件夹B移动到C,在将时间戳添加到文件名后,这两个文件夹都位于C驱动器中。要移动的文件位于arraylist“last”中。在这里,当我运行此代码时,不会发生移动。请显示您的输出和/或异常。还显示您的路径,您将使用在“重命名”之前。也可以检查以下内容: