Reputation: 619
For large strings (60MB or so long), FileWriter
is appending extra null
s to the end of my files. For small strings this code works as expected.
For clarity, dat and filePath are Strings.
FileWriter fstream = new FileWriter( filePath );
fstream.write( dat );
fstream.close();
File f = new File( filePath );
System.out.println("Data: " + dat.length() + ", File: " + f.length());
In short, under what circumstances, should the two printed values be different?
Here's my example output:
Data: 63833144, File: 63833728
I got 584 extra null
s at the end of file for some reason. I find it reasonable that the string might be over allocated, but these shouldn't print to file, right ? To make things worse, if I explicitly give it the length:
fstream.write(dat, 0, dat.length());
The behavior is the same. Coincidentally, if I say (dat.length() - 584), it does what I want, but only in this specific case.
Any ideas?
JDK version: 1.7.0_02
Edited: Add file types for variables (both Strings)
Upvotes: 3
Views: 3781
Reputation: 136042
The file length depends on encoding. This test
System.out.println(dat.getBytes().length);
will show the length in bytes after encoding, because String.getBytes
will use the same encoding (default) as new FileWriter(file)
Upvotes: 1
Reputation: 1048
So run a test with a 63833144 long string with only 'A's in it and the output is: Data: 63833144, File: 63833144
So Im sure the problem is a encoding problem.
(I would have post this as comment but because I have not 50 rep Im not able to :/)
Upvotes: 0
Reputation: 910
What is "dat"? If "dat" is a StringBuffer, you need to be careful. If the length of the StringBuffer is greater than its contents, then nulls will be appended to the end. You might try to use dat.toString(). The null characters will be trimmed in the conversion, I believe.
Upvotes: 2
Reputation: 45576
I suggest that you never use FileWriter, because it is using default encoding on your platform to convert String to byte stream.
Instead you should do this:
Writer writer =
new OutputStreamWriter(
new FileOutputStream( fileName ),
// Always specify encoding compatible with your string
"UTF-8"
);
try
{
writer.write( dat );
writer.flush( );
}
finally
{
writer.close( );
}
Also, the String length and resulting byte stream length don't have to match. They will match only for ASCII text string.
Upvotes: 1