目录
2.1 判断 D:\\news1.txt 是否存在,如果存在就删除
2.2 判断 D:\\demo02 目录是否存在,存在就删除,否则提示不存在
2.3 判断 D:\\demo\\a\\b\\c 目录是否存在,如果存在就提示已存在,否则就创建
- public class FileInformation {
- public static void main(String[] args) {
-
- }
- @Test
- public void info(){
- //先创建文件对象
- File file = new File("D:\\news1.txt");
-
- //调用相应的方法,得到对应信息
- System.out.println("文件名字="+file.getName());
- System.out.println("文件绝对路径="+file.getAbsolutePath());
- System.out.println("文件父级目录="+file.getParent());
- System.out.println("文件大小(字节)="+file.length());
- System.out.println("文件是否存在="+file.exists());
- System.out.println("是不是一个文件="+file.isFile());
- System.out.println("是不是一个目录="+file.isDirectory());
- }
- }

- public class Directory {
- public static void main(String[] args) {
-
- }
- @Test
- public void m1(){
- String filePath = "D:\\news1.txt";
- File file = new File(filePath);
- if (file.exists()){
- if (file.delete()){
- System.out.println(filePath+"删除成功");
- }else {
- System.out.println(filePath+"删除失败");
- }
- }else {
- System.out.println("该文件不存在");
- }
- }
- }


- public class Directory {
- public static void main(String[] args) {
-
- }
- @Test
- public void m2(){
- String filePath = "D:\\demo02";
- File file = new File(filePath);
- if (file.exists()){
- if (file.delete()){
- System.out.println(filePath+"删除成功");
- }else {
- System.out.println(filePath+"删除失败");
- }
- }else {
- System.out.println("该目录不存在");
- }
- }
- }

- public class Directory {
- public static void main(String[] args) {
-
- }
- @Test
- public void m3(){
- String directoryPath = "D:\\demo\\a\\b\\c";
- File file = new File(directoryPath);
- if (file.exists()){
- System.out.println(directoryPath+"存在。。。");
- }else {
- if(file.mkdirs()){
- System.out.println(directoryPath+"创建成功。。。");
- }else {
- System.out.println(directoryPath+"创建失败。。。");
- }
- }
- }
- }
第一次运行结果:

第二次运行结果:
