如何在java中检查文件类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25298691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:04:22  来源:igfitidea点击:

How to check the File Type in java

javafile-type

提问by Akshitha

How can I check whether the file version is .gzor .bzip2. I searched in File Java docs but couldn' find any method. Can you please let me know?

我如何检查文件版本是否.gz还是.bzip2。我在 File Java docs 中搜索,但找不到任何方法。你能告诉我吗?

My requirement is to show the file on the UI , if .txt doesn't exist then check if .gz exists and if that doesnt exist then check if .bzip2 file exists and hence I am looking to check the extension of the file. I am assuming that I need to be looking at the Type of file.

我的要求是在 UI 上显示文件,如果 .txt 不存在,则检查 .gz 是否存在,如果不存在,则检查 .bzip2 文件是否存在,因此我希望检查文件的扩展名。我假设我需要查看文件类型。

采纳答案by Jaskey

You may use FilesUtility of Guava, and use the method of Files.getFileExtension(String String fullName)

您可以使用Guava 的FilesUtility ,并使用Files.getFileExtension(String String fullName)

System.out.println(Files.getFileExtension("C:\fileName.txt"));

The output is:

输出是:

txt

The source code is pretty simple though,

源代码虽然很简单,

public static String getFileExtension(String fullName) {
    checkNotNull(fullName);
    String fileName = new File(fullName).getName();
    int dotIndex = fileName.lastIndexOf('.');
    return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
}

回答by Sotirios Delimanolis

Java 7 provides FileTypeDetector#probeContentType(Path)which returns

Java 7 提供了FileTypeDetector#probeContentType(Path)哪些返回

[...] the string form of the value of a Multipurpose Internet Mail Extension (MIME) content type as defined by RFC 2045: Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies. The string must be parsable according to the grammar in the RFC 2045.

[...] RFC 2045 定义的多用途互联网邮件扩展 (MIME) 内容类型值的字符串形式:多用途互联网邮件扩展 (MIME) 第一部分:互联网消息体的格式。该字符串必须可根据 RFC 2045 中的语法进行解析。

If that fails, you don't have many options. You can check the extension (unsafe because a file can have any name) or try to parse it as gz or bzip2 content and whichever succeeds is correct.

如果那失败了,你就没有很多选择了。您可以检查扩展名(不安全,因为文件可以有任何名称)或尝试将其解析为 gz 或 bzip2 内容,以成功者为准。

回答by MasterOfStupidQuestions

Try something like this

尝试这样的事情

String extension = "";
int i = fileName.lastIndexOf('.');
if (i >= 0) { extension = fileName.substring(i+1); }

which would produce the extension not including the "." so Notepad.txt this syntax would produce txt --- Or word.docx this syntax would produce docx

这将产生不包括“.”的扩展名。所以 Notepad.txt 这个语法会产生 txt --- 或者 word.docx 这个语法会产生 docx

回答by user2768

回答by M-sAnNan

Programmatically determining the type of a file can be surprisingly tricky and there have been many content-based file identification approaches proposed and implemented. There are several implementations available in Java for detecting file types and most of them are largely or solely based on files' extensions

以编程方式确定文件类型可能非常棘手,并且已经提出并实施了许多基于内容的文件识别方法。Java 中有几种实现文件类型可用于检测文件类型,其中大多数主要或完全基于文件的扩展名

Here is LINK

这是链接