1)创建文件目录,File newFile=new File(“E:/a/b/c”);
其中E:后面要有一个斜杠。
public class makedir {
public static boolean mkdir() {
File newFile=new File("E:/a/b/c");
try {
if(newFile.exists()){
System.out.println("已经存在了");
return false;
}
else{
System.out.println("不存在可以创建");
newFile.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public static void main(String args[]){
mkdir();
}
}
2 使用file.exist()和file.isFile(),exist判断是否存在,而isFile需要存在并且是file
public static void moveFile(){
String oldPath="D:/c/d/e.txt";
String toPath="D:/a";
File file = new File(oldPath);
if (file.isFile()){
File toFile=new File(toPath+"/"+file.getName());
if (toFile.exists()){
System.out.println("文件已存在");
}
else{
file.renameTo(toFile);
System.out.println("移动文件成功");
}
}
}