JAVA目录简单操作

复制代码
package CreateFILES;

import java.io.File;
import java.io.IOException;

public class Directory {

    public static void main(String[] args) {
        

        
        
    // md1();
    //    md2();
    //    md3();
        
    }

    
    //判断d盘news1.txt是否存在 如存在则删除
    
    public static void md1() {
        
        String filePath = "D:\\news1.txt";
        File file = new File(filePath);
        if(file.exists()) {
            if(file.delete()) {
                System.out.println("删除成功!");
            }
            else {
                System.out.println("删除失败");
            }
        }
        else {
            System.out.println("文件不存在");
        }
    }
    
    
    //判断目录D:\\demo02是否存在 如存在则删除;
    //在java编程中目录也被当做文件※※※※※※※※※※※※※※※※※
public static void md2() {
        
        String filePath = "D:\\demo02";
        File file = new File(filePath);
//        
//        try {
//            file.createNewFile();
//            System.out.println("目录创建成功");
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
        
        if(file.exists()) {
            if(file.delete()) {
                System.out.println("删除成功!");
            }
            else {
                System.out.println("删除失败");
            }
        }
        else {
            System.out.println("目录不存在");
        }
    }



// 判断目录是否存在,如不存在则进行创建
public static void md3() {
    
    String DirectoryPath = "D:\\demo02\\a\\b\\c";
    File file = new File(DirectoryPath);
    if(file.exists()) {
        System.out.println("目录存在");
    }
    else {
        if(file.mkdirs()) {
            System.out.println("目录创建成功");
        }
        else {
            System.out.println("目录创建失败");
        }
    }
}



}
复制代码

 

posted @   Konataaa  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示