okaywit / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Files.toString(File, Charset) puts an extra newline char to the result #1474

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If you read a text file with Files.toString(File, Charset) into a String, and 
your input file did not contain a newline at the end (blah-blahEOF), the 
resulting String will carry an extra (and I think redundant) newline char.
If it's a conscious behavior, I would suggest some warning into the javadocs.

System: Linux amd64
Guava: 14.0.1
java version "1.7.0_25"

Original issue reported on code.google.com by hangcsa...@gmail.com on 11 Jul 2013 at 9:36

GoogleCodeExporter commented 9 years ago
Forgot to mention: the extra newline gets put to the end.

Original comment by hangcsa...@gmail.com on 11 Jul 2013 at 9:41

GoogleCodeExporter commented 9 years ago
I did further debug and if the file ends in a new line, .toString() keeps that 
and doesn't put an extra.

Original comment by hangcsa...@gmail.com on 11 Jul 2013 at 10:36

GoogleCodeExporter commented 9 years ago
I can't reproduce this. I just created a file with no newline, read it with 
Files.toString, and got a string with no newline. Also, take a look at the 
tests in FilesTest, specifically testToString 
(https://code.google.com/p/guava-libraries/source/browse/guava-tests/test/com/go
ogle/common/io/FilesTest.java#111). Among other things, it reads the file 
ascii.txt 
(https://code.google.com/p/guava-libraries/source/browse/guava-tests/test/com/go
ogle/common/io/testdata/ascii.txt) which has no newline and asserts that it's 
equal to the constant String ASCII, which also has no newline.

Are you sure the file you're reading doesn't have a newline on the end? If it 
doesn't, could you give me a simple test case to reproduce this?

Original comment by cgdecker@google.com on 11 Jul 2013 at 2:52

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08