gpt4 book ai didi

java - 比较 DataOutputStream 和 Java 中的字符串

转载 作者:行者123 更新时间:2023-12-05 01:04:15 27 4
gpt4 key购买 nike

我有一个 DataOutputStream 我想复制到一个字符串中。我找到了很多关于通过将 DataOutputStreams 设置为新的 ByteArrayOutputStream 来转换它的教程,但我只想读取它在刷新时发送的字符串,并且我的 DataOutputStream 已经通过套接字分配给输出流。

output.writeUTF(input.readLine());
output.flush();

如果上下文有帮助,我会尝试读取服务器的输出流并将其与字符串进行比较。

最佳答案

刷新方法将刷新,即强制写入,任何缓冲但尚未写入的内容。

在下面的代码中,尝试在对 writeUTF 的第二次调用上放置一个断点 - 如果您导航到您的文件系统,您应该看到创建的文件,它将包含“一些字符串”。如果将断点置于刷新上,则可以验证内容是否已写入文件。

public static void test() throws IOException {
File file = new File("/Users/Hervian/tmp/fileWithstrings.txt");
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream(file));
dos.writeUTF("some string");
dos.writeUTF("some other string");
dos.flush();//Flushes this data output stream. This forces any buffered output bytes to be written out to the stream.
} finally {
if (dos!=null) dos.close();
}
}

因此,您无法从 DataOutputStream 对象中提取数据,但在上面的示例中,我们当然在 write 调用中使用了这些字符串。

关于java - 比较 DataOutputStream 和 Java 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36165983/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com