创建文件
01.根据路径创建文件
@Test
public void create01(){
String filePath = "E:\\first.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
02.根据父目录文件+子路径创建文件
@Test
public void create02(){
File parentFile = new File("E:\\");
String fileName = "second.txt";
File file = new File(parentFile,fileName);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
03.根据父目录+子路径创建文件
@Test
public void create03(){
String parentPath = "E:\\";
String fileName = "Third.txt";
File file = new File(parentPath,fileName);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
删除文件
判断文件是否存在 若存在则删除
@Test
public void del(){
String filePatch = "E:\\del.txt";
File file = new File(filePatch);
if (file.exists()){
if(file.delete()){
System.out.println("文件删除成功");
}
else {
System.out.println("文件删除失败");
}
}
else {
System.out.println("该文件不存在");
}
}