上代码
import java.io.File;
import java.io.IOException;
import org.junit.Test;
public class FileIoTest {
/**
* 判断文件是否存在
*/
@Test
public void judgeFileOrExist() {
File file = new File("E:\\6731_images\\abcd.txt");
if(file.exists()) {
System.out.println("文件存在");
}else {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*判断文件夹是否存在
*/
@Test
public void judgeFileDirOrExist() {
File file = new File("E:\\6731_images\\abcd");
if(file.exists() && file.isDirectory()) {
System.out.println("文件夹存在!");
file.deleteOnExit();
}else {
file.mkdirs();
System.out.println("文件夹不存在!");
}
}
}