Java 具有长度的DataInputStream和OutputStream写/读字符串
如何使用与此代码一起存储的Java 具有长度的DataInputStream和OutputStream写/读字符串,java,io,java-io,Java,Io,Java Io,如何使用与此代码一起存储的数据输入流读取字符串: DataOutputStream dataOut = new DataOutputStream (out); // Some other stream String title = processed.getTitle(); dataOut.writeInt(title.length()); dataOut.writeBytes(title); 你可以这样读 DataInputStream dataIn = new DataInputStrea
数据输入流
读取字符串
:
DataOutputStream dataOut = new DataOutputStream (out); // Some other stream
String title = processed.getTitle();
dataOut.writeInt(title.length());
dataOut.writeBytes(title);
你可以这样读
DataInputStream dataIn = new DataInputStream (input);
int length = dataIn.readInt();
byte[] array = new byte[length];
dataIn.read(array);
你可以这样读
DataInputStream dataIn = new DataInputStream (input);
int length = dataIn.readInt();
byte[] array = new byte[length];
dataIn.read(array);
您可以使用
ByteArrayOutputStream
和ByteArrayInputStream
以及字节数组作为中间缓冲区
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Some other streams
DataOutputStream dataOut = new DataOutputStream (out);
String title = processed.getTitle();
dataOut.writeInt(title.length());
dataOut.writeBytes(title);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
DataInputStream dataIn = new DataInputStream(in);
您可以使用
ByteArrayOutputStream
和ByteArrayInputStream
以及字节数组作为中间缓冲区
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Some other streams
DataOutputStream dataOut = new DataOutputStream (out);
String title = processed.getTitle();
dataOut.writeInt(title.length());
dataOut.writeBytes(title);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
DataInputStream dataIn = new DataInputStream(in);
。。。如果以这种格式编写标题时不需要超过65533字节:请参阅Javadoc
。。。如果标题以这种格式写入时不需要超过65533字节:请参阅Javadoc。读入?错误您可以从-DataInputStream读取尝试此链接-。我写了一篇关于流操作的simle文章。它的缺点是波兰语和谷歌翻译不能很好地处理它;进入?错误您可以从-DataInputStream读取尝试此链接-。我写了一篇关于流操作的simle文章。它的缺点是波兰语和谷歌翻译不能很好地处理它;P