1.创建文件
@Test
public void test01() {
String path = "f:\\io.txt";
File file = new File(path);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
2.删除文件
@Test
public void test() {
String path = "f:\\io.txt";
File file = new File(path);
try {
file.delete();
System.out.println("文件删除成功");
} catch (Exception e) {
e.printStackTrace();
}
}
3.创建单级目录
@Test
public void test03() {
String path = "f:\\test";
File file = new File(path);
if(file.exists()) {
System.out.println("文件夹已存在");
}else {
if(file.mkdir()) {
System.out.println("文件夹创建成功");
}else {
System.out.println("文件夹创建失败");
}
}
}
4.创建多级目录
@Test
public void test04() {
String path = "f:\\test01\\me";
File file = new File(path);
if(file.exists()) {
System.out.println("文件夹已存在");
}else {
if(file.mkdirs()) {
System.out.println("文件夹创建成功");
}else {
System.out.println("文件夹创建失败");
}
}
}
5.删除空目录
@Test
public void test05() {
String path = "f:\\test";
File file = new File(path);
if(file.exists()) {
file.delete();
System.out.println("文件夹删除成功");
}else {
System.out.println("文件夹不存在");
}
}