Java 如何检查.properties文件是否为文本文件

Java 如何检查.properties文件是否为文本文件,java,Java,我尝试使用Files.probeContentType,但它随机返回null,并且我在java版本早于java 8的windows上读到了它的bug。还有其他选择吗 URLConnection.getContentType,返回 内容类型标题字段 这对我来说很好: public String identifyFileTypeUsingUrlConnectionGetContentType(final String fileName) { String fileType =

我尝试使用Files.probeContentType,但它随机返回null,并且我在java版本早于java 8的windows上读到了它的bug。还有其他选择吗

URLConnection.getContentType,返回 内容类型标题字段

这对我来说很好:

 public String identifyFileTypeUsingUrlConnectionGetContentType(final String fileName)
    {
       String fileType = "Undetermined";
       try
       {
          final URL url = new URL("file://" + fileName);
          final URLConnection connection = url.openConnection();
          fileType = connection.getContentType();
       }
       catch (MalformedURLException badUrlEx)
       {
          System.out.println("ERROR: Bad URL - " + badUrlEx);
       }
       catch (IOException ioEx)
       {
          System.out.println("Cannot access URLConnection - " + ioEx);
       }
       return fileType;
    }

@阿列克谢·罗曼诺夫这在我的情况下如何有效?计算字符类型和非字符类型的数量。文本文件将主要是字母字符,而二进制文件——特别是压缩文件,如rar、zip等——将倾向于更均匀地表示字节。@AlexeyRomanov有更简单的方法吗?有没有内置的功能?这就是我要找的。这个问题的其他答案显示,这个问题没有具体说明。