Search code examples
javaencryptionfileutils

How does FileUtils.readFileToString() treat line breaks?


I am writing a program in java to import a user-entered text file, encrypt it using two keywords, and write the output to a new text file.

I am using FileUtils.readFileToString() to read the text file into a String, but I don't understand how it treats line breaks/return keys. What character does it convert them into?

I need to understand this, so I can encrypt this character and then it can be decrypted properly.

I am essentially trying to add a 'line break symbol' to my 'characters' String.

EDIT: Problem solved, using '/n'. I finally understand it. Thank you!


Solution

  • It has no special handling for line breaks. FileUtils#readFileToString consumes the bytes inside the file, interpreting them with whichever character encoding you provide to produce a String value. If there are bytes in the file that correspond to a new line or carriage return in the character set provided, then you'll have a new line or carriage return in the returned String.

    You can find the source code here.