标签:
文件的地址是d:\ti.txt和d:\titi.txt
一开始写成这样
public static void main(String[] args) { List<Object> list = new ArrayList<>(); File file1 = new File("d:\ti.txt"); File file2 = new File("d:\titi.txt"); file1.renameTo(file2); list.add(file1); list.add(file2); for(int i = 0;i<list.size();i++){ File f = (File)list.get(i); f.renameTo(new File("d:\\"+i+"cc.txt")); System.out.println(i); } }
运行,没发现重命名
后来修改为:
public static void main(String[] args) { List<Object> list = new ArrayList<>(); File file1 = new File("d:\\ti.txt"); File file2 = new File("d:\\titi.txt"); file1.renameTo(file2); list.add(file1); list.add(file2); for(int i = 0;i<list.size();i++){ File f = (File)list.get(i); f.renameTo(new File("d:\\"+i+"cc.txt")); System.out.println(i); } }
重命名成功
首先,地址中的\需要转义
只想要‘或者"或者\这个字符时,需要转义
标签:
原文地址:http://www.cnblogs.com/lonely-buffoon/p/5575947.html