0

I have a set of strings (String objects) in Java and would like to write them to a file so that I can later retrieve them.

I understand that Java uses UTF-16 to store strings internally. I am worried that I might muck something up due to formatting issues unless I write and read the strings properly. I do not want to dump the String objects raw to the file as I would like to be able to open the file in a standard text editor to look at it, where each string is shown on its own line in a sensible way (assuming no string contains a line break).

Can I simply use say the PrintWriter class with the println(String x) method (assuming there are no line breaks in the strings), combined with the Scanner class's nextLine() method when reading them back? Would this guarantee that I get the exact same strings back?

Further, suppose the strings do contain line breaks, what is the appropriate way of writing them then? Should I filter out line breaks (replacing them with some ad-hoc escape code or similar) and use the println method with PrintWriter as above?

5
  • 1
    Have you tried it yet? Have a try and you yourself will find the answer. Commented Mar 13, 2015 at 10:19
  • I believe you pretty much answered your own question, just try the code and see how it works, it's all part of being a programmer :) Commented Mar 13, 2015 at 10:21
  • I suggest you simply use ObjectInputStream and ObjectOutputStream instead of writing the strings in a human-readable format (if nobody -human- is going to read them, that is). Code would be much simpler and faster. Commented Mar 13, 2015 at 10:27
  • When you use a Writer or a Reader, what you write or read aren't chars but bytes; and this means you need to encode/decode. Commented Mar 13, 2015 at 10:29
  • 1
    How am I supposed to know the answer for sure by running the code? Am I supposed to try my code on all strings? :) (FYI: I'm not a programmer professionally but a computer scientist.) Commented Mar 13, 2015 at 11:24

1 Answer 1

1

For completeness I am answering my own question with the solution I eventually adopted. In retrospect the solution is very straightforward. Duh!

To write the strings I use the BufferedWriter class which has conventient methods for writing strings. The BufferedWriter is obtain through:

writer = new BufferedWriter(
             new OutputStreamWriter(
                 new FileOutputStream(filename), "UTF-8"));

Here I have specified the UTF-8 encoding which is supported by basically everything.

To read the strings back I use the BufferedReader class and make sure to use the UTF-8 encoding:

reader = new BufferedReader(
             new InputStreamReader(
                 new FileInputStream(filename), "UTF-8"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.