读取/写入字符串与二进制文件? [英] Reading/writing a BINARY File with Strings?

查看:548
本文介绍了读取/写入字符串与二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何写/读从二进制文件中的字符串?

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.


  • 对于二进制数据的一个使用的OutputStream 的InputStream 类。他们处理字节[]

  • 对于文本数据的一个使用阅读作家类。他们处理字符串可存储所有类型的文本,因为它在内部使用的Uni code。

  • For binary data one uses OutputStream and InputStream classes. They handle byte[].
  • For text data one uses Reader and Writer classes. They handle String which can store all kind of text, as it internally uses Unicode.

从文本到二进制数据的交叉可以通过指定的编码,默认为操作系统的编码来实现。

The crossover from text to binary data can be done by specifying the encoding, which defaults to the OS encoding.


  • 新OutputStreamWriter(OutputStream中,编码)

  • string.getBytes(编码)

  • new OutputStreamWriter(outputStream, encoding)
  • string.getBytes(encoding)

所以,如果你想避免字节[] 并使用字符串必须滥用涵盖以任何​​顺序所有256个字节值的编码。因此,没有UTF-8,但也许窗口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").

但在内部有一个转换,在极少数情况下,问题可能发生。例如电子可在统一code为一个code,或者两个,电子 +组合变音马克右口音 。那里存在一个转换功能(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.

该已经导致了问题的一个例子是在不同的操作系统中的文件名; MacOS的还有另外一个统一code正常化比Windows,因此在版本控制系统需要特别​​注意。

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.

所以,原则上最好是使用较为繁琐字节数组,或ByteArrayInputStream的,或者java.nio的缓冲区。心中也该字符串字符是16位的。

So on principle it is better to use the more cumbersome byte arrays, or ByteArrayInputStream, or java.nio buffers. Mind also that String chars are 16 bit.

这篇关于读取/写入字符串与二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆