問題描述
也就是說,你將如何從文本(xml/txt,編碼無關(guān))文件中區(qū)分存檔(jar/rar/etc.)文件?
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:
在文件中查找標題.不幸的是,標頭是特定于文件的,因此雖然您可能會發(fā)現(xiàn)它是一個 RAR 文件,但您不會得到更通用的答案,即它是文本還是二進制文件.
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.
計算字符與非字符類型的數(shù)量.文本文件大多是字母字符,而二進制文件——尤其是壓縮文件,如 rar、zip 等——往往會更均勻地表示字節(jié).
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.
尋找有規(guī)律地重復的換行符.
Look for a regularly repeating pattern of newlines.
這篇關(guān)于在Java中確定二進制/文本文件類型?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!