确定二进制/文本文件类型在Java中? [英] Determining binary/text file type in Java?
问题描述
也就是说,你会怎么告诉存档(JAR / RAR /等)从一个文本(XML / TXT,编码无关)一个文件?
Namely, how would you tell an archive (jar/rar/etc.) file from a textual (xml/txt, encoding-independent) one?
推荐答案
有没有保证的方式,但这里有几个可能的原因:
There's no guaranteed way, but here are a couple of possibilities:
1)寻找该文件的标题。不幸的是,头文件是专用的,所以当你也许能发现,这是一个RAR文件,你不会得到它是否是文本或二进制更通用的答案。
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)计数字符与非字符类型的数目。文本文件将主要字母字符,而二进制文件 - 尤其是COM pressed的像RAR,ZIP,这样的 - 往往会拥有更多的字节重新均匀presented
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)寻找新行的规则重复的模式。
3) Look for a regularly repeating pattern of newlines.
这篇关于确定二进制/文本文件类型在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!