Linux 拨号vps windows公众号手机端

java如何判断某个路径是否存在

lewis 7年前 (2018-04-14) 阅读数 10 #程序编程
文章标签 Java

在Java中,可以使用File类的exists()方法来判断某个路径是否存在。示例如下:

import java.io.File;

public class CheckPathExists {
    public static void main(String[] args) {
        String path = "/path/to/file/or/directory";
        File file = new File(path);
        
        if(file.exists()) {
            System.out.println("The path exists.");
        } else {
            System.out.println("The path does not exist.");
        }
    }
}

在上面的示例中,首先创建一个File对象,然后调用exists()方法来判断该路径是否存在。如果路径存在,则打印"The path exists.“;否则打印"The path does not exist.”。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门