0

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

我试过使用writeUTF/ readUTF(DataOutputStream/DataInputStream) 但这太麻烦了。

谢谢。

4

2 回答 2

4

暂时忘记 FileWriter、DataOutputStream。

  • 对于二进制数据,一种用途OutputStreamInputStream类别。他们处理byte[]
  • 对于文本数据一个用途ReaderWriter类。他们处理String可以存储所有类型的文本,因为它在内部使用 Unicode。

可以通过指定编码来完成从文本到二进制数据的交叉,默认为 OS 编码。

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

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").

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.

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.

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.

于 2012-07-20T20:09:33.543 回答
2

如果你想写文本,你可以使用 Writers 和 Readers。

您可以使用 Data*Stream writeUTF/readUTF,但字符串长度必须少于 64K 个字符。


public static void main(String... args) throws IOException {
    // generate a million random words.
    List<String> words = new ArrayList<String>();
    for (int i = 0; i < 1000000; i++)
        words.add(Long.toHexString(System.nanoTime()));

    writeStrings("words", words);
    List<String> words2 = readWords("words");
    System.out.println("Words are the same is " + words.equals(words2));
}

public static List<String> readWords(String filename) throws IOException {
    DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)));
    int count = dis.readInt();
    List<String> words = new ArrayList<String>(count);
    while (words.size() < count)
        words.add(dis.readUTF());
    return words;
}

public static void writeStrings(String filename, List<String> words) throws IOException {
    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
    dos.writeInt(words.size());
    for (String word : words)
        dos.writeUTF(word);
    dos.close();
}

印刷

Words are the same is true
于 2012-07-20T18:11:06.000 回答