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

20次阅读
没有评论

共计 412 个字符,预计需要花费 2 分钟才能阅读完成。

在 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.”。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2024-04-13发表,共计412字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)