java 大量文本的数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6031346/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:03:27  来源:igfitidea点击:

Data type for large amounts of text?

javatypes

提问by Ethan Turkeltaub

I'm developing a Java package that makes basic HTTP requests (GET, POST, PUT, and DELETE). Right now, I'm having it just print the output of the request. I would like to store it in a field, but I'm not sure if String supports large amounts of text. Is there a data type for large amounts of text, or is there a reasonable alternative to it? Right now, because I'm just printing it, I can't do anything with the data that is returned (like parse it, if it's JSON).

我正在开发一个发出基本 HTTP 请求(GET、POST、PUT 和 DELETE)的 Java 包。现在,我只是打印请求的输出。我想将它存储在一个字段中,但我不确定 String 是否支持大量文本。是否有用于大量文本的数据类型,或者是否有合理的替代方案?现在,因为我只是在打印它,所以我不能对返回的数据做任何事情(比如解析它,如果它是 JSON)。

Any ideas would be helpful.

任何想法都会有所帮助。

Edit: The code is online on GitHub.

编辑:代码在 GitHub 上在线。

回答by user151019

Strings can take up to 2^31 - 1 characters so I suspect are big enough. Data from SO question

字符串最多可以占用 2^31 - 1 个字符,所以我怀疑它们已经足够大了。来自SO 问题的数据

回答by Wim Deblauwe

I see that you use BufferedReaderin your code. You can just leave the string in there and pass that reader to your JSON parser for instance. Would be more efficient than first creating a String out of it.

我看到你BufferedReader在你的代码中使用了。例如,您可以将字符串留在那里,然后将该读取器传递给您的 JSON 解析器。会比首先从中创建一个字符串更有效。

回答by karmakaze

If you are performing a single set of operations on the data, you can stream it through a pipeline and not even store the entire data in memory at any time. It can also boost performance as work can begin upon the first character rather than after the last is received. Check out CharSequence.

如果您对数据执行一组操作,则可以通过管道将其流式传输,甚至随时都不会将整个数据存储在内存中。它还可以提高性能,因为工作可以从第一个字符开始,而不是在接收到最后一个字符之后。查看 CharSequence。