問題描述
如何從二進(jìn)制文件中寫入/讀取字符串?
How can I write/read a string from a binary file?
我試過使用 writeUTF
/readUTF
(DataOutputStream/DataInputStream) 但太麻煩了.
I've tried using writeUTF
/ readUTF
(DataOutputStream/DataInputStream) but it was too much of a hassle.
謝謝.
推薦答案
暫時忘記 FileWriter,DataOutputStream.
Forget about FileWriter, DataOutputStream for a moment.
- 對于二進(jìn)制數(shù)據(jù),可以使用
OutputStream
和InputStream
類.他們處理byte[]
. - 對于文本數(shù)據(jù),使用
Reader
和Writer
類.他們處理String
可以存儲所有類型的文本,因為它在內(nèi)部使用 Unicode.
- For binary data one uses
OutputStream
andInputStream
classes. They handlebyte[]
. - For text data one uses
Reader
andWriter
classes. They handleString
which can store all kind of text, as it internally uses Unicode.
文本到二進(jìn)制數(shù)據(jù)的交叉可以通過指定編碼來完成,默認(rèn)為OS編碼.
The crossover from text to binary data can be done by specifying the encoding, which defaults to the OS encoding.
new OutputStreamWriter(outputStream, encoding)
string.getBytes(編碼)
因此,如果您想避免 byte[]
并使用 String,則必須濫用以任何順序覆蓋所有 256 字節(jié)值的編碼.所以沒有UTF-8",但可能是windows-1252"(也稱為Cp1252").
So if you want to avoid byte[]
and use String you must abuse an encoding which covers all 256 byte values in any order. So no "UTF-8", but maybe "windows-1252" (also named "Cp1252").
但內(nèi)部存在轉(zhuǎn)換,在極少數(shù)情況下可能會出現(xiàn)問題.例如 é
在 Unicode 中可以是一個代碼,也可以是兩個,e
+ 組合變音符號右重音 '
.有一個轉(zhuǎn)換函數(shù)(java.text.Normalizer).
But internally there is a conversion, and in very rare cases problems might happen. For instance é
can in Unicode be one code, or two, e
+ combining diacritical mark right-accent '
. There exists a conversion function (java.text.Normalizer) for that.
這已經(jīng)導(dǎo)致問題的一種情況是不同操作系統(tǒng)中的文件名;MacOS 比 Windows 有另一個 Unicode 規(guī)范化,因此在版本控制系統(tǒng)中需要特別注意.
One case where this already led to problems is file names in different operating systems; MacOS has another Unicode normalisation than Windows, and hence in version control system need special attention.
所以原則上最好使用更繁瑣的字節(jié)數(shù)組,或ByteArrayInputStream,或java.nio 緩沖區(qū).還要注意 String char
是 16 位的.
So on principle it is better to use the more cumbersome byte arrays, or ByteArrayInputStream, or java.nio buffers. Mind also that String char
s are 16 bit.
這篇關(guān)于用字符串讀/寫二進(jìn)制文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!