在用File类操作文件并且改名的时候发现一个小问题!感觉还是挺好玩的!代码如下:
import java.io.File;
import java.io.IOException;
public class renameTest {
/**
* 当把一个File进行改名操作后,即fileOld.renameTo(NewFile)
* 你本地磁盘的文件夹内,即fileOld执行的对像的名字改变了
* 但是,fileOld类的内容并没有改变
*
* */
public static void main(String[] args) {
String path = "D:\\temp\\testBeyondCompare\\456";
File fileNew = new File(path);
if(fileNew.isAbsolute()){
File fileOld = new File("D:\\temp\\testBeyondCompare\\123");
fileNew.renameTo(fileOld);
fileNew.mkdirs();
File text1 = new File(fileNew.getAbsolutePath()+"\\"+"456.txt");
File text = new File(fileOld.getAbsolutePath()+"\\"+"123.txt");
try {
text.createNewFile();
text1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如果拷代码的话,请注意路径!