Java 使用 DataOutputStream 写入大字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/270884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing large strings with DataOutputStream
提问by Glen
I've been doing some socket programming to transmit information across the wire. I've run into a problem with DataOutputStream.writeUTF(). It seems to allow strings of up to 64k but I have a few situations where I can run over this. Are there any good alternatives that support larger strings or do I need to roll my own?
我一直在做一些套接字编程来通过线路传输信息。我遇到了 DataOutputStream.writeUTF() 的问题。它似乎允许最多 64k 的字符串,但我有几种情况可以解决这个问题。是否有支持更大字符串的好的替代方案,或者我需要自己滚动吗?
采纳答案by kasperjj
It actually uses a two bytes to write the length of the string before using an algorithm that compacts it into one, two or three bytes per character. (See the documentation on java.io.DataOutput) It is close to UTF-8, but even though documented as being so, there are compatibility problems. If you are not terribly worried about the amount of data you will be writing, you can easily write your own by writing the length of the string first, and then the raw data of the string using the getBytes method.
在使用将字符串压缩为每个字符一个、两个或三个字节的算法之前,它实际上使用两个字节来写入字符串的长度。(请参阅 java.io.DataOutput 上的文档)它接近 UTF-8,但即使记录如此,也存在兼容性问题。如果您不是非常担心您将要写入的数据量,您可以通过先写入字符串的长度,然后使用 getBytes 方法写入字符串的原始数据来轻松编写自己的数据。
// Write data
String str="foo";
byte[] data=str.getBytes("UTF-8");
out.writeInt(data.length);
out.write(data);
// Read data
int length=in.readInt();
byte[] data=new byte[length];
in.readFully(data);
String str=new String(data,"UTF-8");
回答by Bill the Lizard
You should be able to use OutputStreamWriterwith the UTF-8 encoding. There's no explicit writeUTF method, but you can set the charset in the constructor. Try
您应该能够将OutputStreamWriter与 UTF-8 编码一起使用。没有明确的 writeUTF 方法,但您可以在构造函数中设置字符集。尝试
Writer osw = new OutputStreamWriter(out, "UTF-8");
where out
is whatever OutputStream you're wrapping now.
out
您现在包装的任何 OutputStream在哪里。
回答by ebruchez
ObjectOutputStream.writeObject()
properly handles long strings (verified by looking at the source code). Write the string out this way:
ObjectOutputStream.writeObject()
正确处理长字符串(通过查看源代码验证)。这样写出字符串:
ObjectOutputStream oos = new ObjectOutputStream(out);
... other write operations ...
oos.writeObject(myString);
... other write operations ...
Read it this way:
这样读:
ObjectInputStream ois = new ObjectInputStream(in);
... other read operations ...
String myString = (String) ois.readObject();
... other read operations ...
Another difference with DataOutputStream
is that using ObjectOutputStream
automatically writes a 4-byte stream header when instantiated, but its usually going to be a pretty small penalty to pay.
另一个区别DataOutputStream
是 usingObjectOutputStream
在实例化时自动写入一个 4 字节的流标头,但它通常会付出很小的代价。