OMGitzMidgar
OMGitzMidgar

Reputation: 39

Saving paragraphs in Java String VS in a File

So I am building the backend of a Blog Engine in Java, and one of the very first questions that I had when designing the engine was "Is saving paragraphs into a Java String the best approach here?" when thinking about how to store the paragraphs of text that the text body of a Blog Entry might have.

Currently, my blog posts only have text, so do not worry about other media like pictures and videos.

However, would it be a good idea to use a String to store the body of a blog entry?

My engine has a BlogEntry class where it stores various features of a blog entry such as the author, the title, and the body text of a blog. I am using a String for author and title simply because those won't ever be too long of a String. Obviously, if the body text of every blog entry would be just a sentence or two, I would have no problem using Java String. But these blog entries can be multiple paragraphs of incredibly long sentences! So to handle the text of huge blog entries, should I stay away from using Java String? Would it be better for me to use something like a File to store the text of the blog entries?

Please note: when I say "better", I am looking at maintainability, good practice, and possibly time complexity. My definition of "better" is basically "is this a good way to implement this? Would professional programmers look at this and like how I implemented this? Would other programmers using this engine be able to easily use and understand this implementation?"

Let me know if I can clarify anything else. Thanks!

Edit: I am trying to not use databases at all, and I plan on saving my blog entries to a file before my program closes each time. So I would have the text of the blog be stored as a String, and the BlogEntry itself would be saved as a file for further use.

Upvotes: 3

Views: 1971

Answers (1)

kapex
kapex

Reputation: 29959

A String can have a maximum length of 2.147.483.647 characters in Java. Even with a simple 1 byte per character encoding, that is over 2GB. That should be enough to fit any plain text document in it.

If you have large documents (over a few MB), long before you reach string limits, you will run into other problems that you have to solve first. For example usability issues, bandwidth limits, or rendering performance.

Also, "String vs File" doesn't sound like you are comparing the right things. Maybe you should better ask "in-memory vs persistent". Having strings stored in memory is fine - if you don't care if you loose them. Web applications usually run forever, when they stop it is because the application or the server crashed or had to be shut down for maintencance - in either case all your in-memory strings will be lost. You should definitely persist them as soon as possible. You can still keep an in-memory copy if you want, used as a cache if you are concerned about performance.

Or maybe your issue is about "Database vs File". But even that comparison isn't quite right, as there are lightweight file based databases and in-memory databases too. Anyway, the common approach would be to use a database. I would expect a system like you descibed to use files only in rare cases, for example for documents that can be downloaded directly.

Upvotes: 3

Related Questions