简体   繁体   中英

Long String Used in Java

Let say I have to used a two paragraph string in my java program. What would be the best approach on this? Should the paragraph saved in a file and retrieved when used? Or there are better approach?

It depends. If the paragraphs will never change, then it might make sense to embed them directly in the source code. This has the advantage that no extra files are needed to run your code.

If you expect that the paragraphs might change, it's probably better to put them in an external file because that allows somebody to modify the text without having to recompile the program. This could be beneficial if those who modify are not programmers.

并且仅添加是否需要将这两个段落字符串连接在一起,更好的方法是使用StringBuffer类并附加它们。

The only time I've seen a String with that kind of length on it would be in internationalized property files . This keeps them separate from logic, and make editing the paragraph/necessary code simpler.

In short, yes, the better approach would be to store the String in its own separate file. However, there shouldn't be any reason to have one that long - even with property files, it's better to break those out too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM