Determining binary/text file type in Java?

前端 未结 10 1153
心在旅途
心在旅途 2020-12-02 16:46

Namely, how would you tell an archive (jar/rar/etc.) file from a textual (xml/txt, encoding-independent) one?

10条回答
  •  感情败类
    2020-12-02 17:42

    There's no guaranteed way, but here are a couple of possibilities:

    1. Look for a header on the file. Unfortunately, headers are file-specific, so while you might be able to find out that it's a RAR file, you won't get the more generic answer of whether it's text or binary.

    2. Count the number of character vs. non-character types. Text files will be mostly alphabetical characters while binary files - especially compressed ones like rar, zip, and such - will tend to have bytes more evenly represented.

    3. Look for a regularly repeating pattern of newlines.

提交回复
热议问题