public class Test {
public static void main(String[] args) {
File file=new File("D:\\helloWorld.txt");
if(file.isFile()){
System.out.println("it is a exists");
file.renameTo(new File("D:\\IOTest\\"+file.getName()));
}else{
try {
file.createNewFile();
System.out.println("excute");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File file=new File("D:\\helloWorld.txt");
if(file.isFile()){
System.out.println("it is a exists");
file.renameTo(new File("D:\\IOTest\\"+file.getName()));
}else{
try {
file.createNewFile();
System.out.println("excute");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
导入需要的包资源,这个eclipse会自动提示。之前碰到一个前辈的博客,感觉出了问题,于是自己测试了一下,方法可行。如果第一次测试的还没有.txt文件需要运行两遍。