Java文件移动到指定的文件中

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();
}
}
}


}

导入需要的包资源,这个eclipse会自动提示。之前碰到一个前辈的博客,感觉出了问题,于是自己测试了一下,方法可行。如果第一次测试的还没有.txt文件需要运行两遍。

猜你喜欢

转载自blog.csdn.net/weixin_41195786/article/details/79150438
今日推荐