Java isFile(), isDirectory() 不检查是否存在

发布于 2024-12-22 23:58:12 字数 686 浏览 1 评论 0原文

我想检查给定的字符串是文件还是目录,我尝试了 File 类,但问题是,如果目录或文件不存在,这些方法将返回 false,因为如 javadoc 中所述:

isFile()

当且仅当此抽象路径名表示的文件存在时才为 true 并且是一个普通文件;否则为假

isDirectory()

当且仅当此抽象路径名表示的文件存在时才为 true 是一个目录;否则为假

基本上我需要两种不带存在子句的方法 ...

所以我想测试给定的字符串是否符合目录格式或符合文件格式,在多平台上下文中(所以,应该适用于 Windows、Linux 和 Mac Os X)。

是否存在一些提供这些方法的库?这些方法的最佳实施是什么?

更新

在默认情况下可以是两者(不带扩展名)的字符串的情况下,如果具有该路径的文件不存在,则应将其标识为目录。

I want to check if a given String is a file or a directory, i've tried the methods isFile() and isDirectory() of the File class but the problem is that if the directory or file doesn't exist these methods returns false, because as stated in the javadoc :

isFile() :

true if and only if the file denoted by this abstract pathname exists
and is a normal file; false otherwise

isDirectory() :

true if and only if the file denoted by this abstract pathname exists
and is a directory; false otherwise

Basically i need two methods without the exist clause ...

So i want to test if the given string complies to a directory format or complies to a file format, in a multiplatform context (so, should work on Windows, Linux and Mac Os X).

Does exist some library that provide these methods ? What could be the best implementation of these methods ?

UPDATE

In the case of a string that could be both(without extension) by default should be identified as directory, if a file with that path does not exist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疾风者 2024-12-29 23:58:12

所以我想在多平台上下文中测试给定的字符串是否符合目录格式或文件格式(因此,应该适用于 Windows、Linux 和 Mac Os X)。

在 Windows 中,目录可以有扩展名,而文件不需要有扩展名。所以,仅仅通过查看字符串是无法判断的。

如果您强制规定目录没有扩展名,而文件始终具有扩展名,那么您可以通过查找扩展名来确定目录和文件之间的区别。

So i want to test if the given string complies to a directory format or complies to a file format, in a multiplatform context (so, should work on Windows, Linux and Mac Os X).

In Windows, a directory can have an extension and a file is not required to have an extension. So, you can't tell just by looking at the string.

If you enforce a rule that a directory doesn't have an extension, and a file always has an extension, then you can determine the difference between a directory and a file by looking for an extension.

橘和柠 2024-12-29 23:58:12

为什么不将它们包装在对 File#exists()

File file = new File(...);
if (file.exists()) {

    // call isFile() or isDirectory()

}

通过这样做,您实际上否定了 isFile()isDirectory() 的“exists”部分,因为您可以保证它确实存在。


也有可能我误解了你在这里问的问题。鉴于问题的第二部分,您是否尝试对不存在的文件使用 isFile()isDirectory() 来查看它们是否看起来像 它们是文件还是目录?

如果是这样,那么使用 File API 就很难做到这一点(而且一般情况下也很难做到)。如果/foo/bar/baz不存在,则无法确定它是文件还是目录。也可能是。

Why not just wrap them in a call to File#exists()?

File file = new File(...);
if (file.exists()) {

    // call isFile() or isDirectory()

}

By doing that, you've effectively negated the "exists" portion of isFile() and isDirectory(), since you're guaranteed that it does exist.


It's also possible that I've misunderstood what you're asking here. Given the second part of your question, are you trying to use isFile() and isDirectory() on non-existent files to see if they look like they're files or directories?

If so, that's going to be tough to do with the File API (and tough to do in general). If /foo/bar/baz doesn't exist, it's not possible to determine whether it's a file or a directory. It could be either.

世俗缘 2024-12-29 23:58:12

根据您的更新,听起来您知道自己想要什么:如果路径不存在并且路径具有扩展名,则它是一个文件,如果不存在,则它是一个目录。像这样的东西就足够了:

private boolean isPathDirectory(String myPath) {
    File test = new File(myPath);

    // check if the file/directory is already there
    if (!test.exists()) {
        // see if the file portion it doesn't have an extension
        return test.getName().lastIndexOf('.') == -1;
    } else {
        // see if the path that's already in place is a file or directory
        return test.isDirectory();
    }
}

Sounds like you know what you want, according to your update: if the path doesn't exist and the path has an extension it's a file, if it doesn't it's a directory. Something like this would suffice:

private boolean isPathDirectory(String myPath) {
    File test = new File(myPath);

    // check if the file/directory is already there
    if (!test.exists()) {
        // see if the file portion it doesn't have an extension
        return test.getName().lastIndexOf('.') == -1;
    } else {
        // see if the path that's already in place is a file or directory
        return test.isDirectory();
    }
}
风吹过旳痕迹 2024-12-29 23:58:12

文件和/或文件夹名称中的无效内容有一些规则。例如,Windows 不允许使用 *? 和其他一些字符。根据无效内容,您可以构建一个regex表达式或其他一些处理/检查系统来查看它是否看起来是一个文件或文件夹。

这可能会变得复杂,因为您希望它适用于许多不同的操作系统。另外,正如之前发布的,除非您人为地强制执行约定,否则无法区分文件和文件夹。例如,在 Windows 中,目录必须以前斜杠 / 结尾。

首先进行 IF EXISTS 检查会有所帮助。如果IF EXISTS = true,则运行现有的File.isDirectory()File.isFile()代码将简化很多工作。您只需在 IF EXISTS = false 时编写代码。

There are rules for what is invalid in a file and/or folder name. For example, Windows doesn't allow *, ?, and a few other characters. Based on what's invalid, you could build a regex expression or some other process/checking system to see if it looks like a file or folder.

This could get complex as you want it to work for many different OS's. Also, as previously posted, there would be no way to tell a file from a folder unless you artificially enforced a convention. For example, directories must end in a front-slash / in Windows.

Having the IF EXISTS check first would help. If IF EXISTS = true, then running the existing File.isDirectory() or File.isFile() code would simplify a lot of this. You would only have to write code for when IF EXISTS = false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文