Problem with Saving information in a .txt file

Hello!

I’m using a PrintWriter to write a String into a file. So far so good. However, now I’m trying to make an encryption program and I noticed that some numbers just don’t go well.
For example: if a char after encryption has the integer value of 157 then if I write a String containing that char to a .txt file and then read it in again then it will have changed to 63…
Same things happen for several values such as 137 for example (which also becomes 63). Why is this happening and how can I avoid it?
It’s making my decryption give me faulty results in the case of those characters however the problem only appears when I save to a file and load it again before decrypting it. The decryption works perfectly if I use it on the String without first writing it to a file.

Are you careful to mind the encoding when writing and reading the text from the file?

Readers\Writers are rather for string and character output. For bytes rather use the Input\Output streams

As Jeremy said, the encoding is probably the issue. Java Strings are in Unicode encoding, while a text file could for example be in UTF-8. Writing a Java String to a text file may involve converting Unicode to some other encoding (such as UTF-8) resulting in the changes you see.

You may want to use byte arrays to do the encryption and to load/save data, and to explicitly convert between Strings and byte arrays. See for example: this article about byte encodings and strings.