Note that there are some explanatory texts on larger screens.

plurals
  1. POShouldn't the content of a file remain unaltered if I read it byte-by-byte?
    primarykey
    data
    text
    <p>Why does the following code alter "öäüß"? (I am using it to split big files into multiple small ones...)</p> <pre><code>InputStream is = new BufferedInputStream(new FileInputStream(file)); File newFile; BufferedWriter bw; newFile = new File(filePathBase + "." + String.valueOf(files.size() + 1) + fileExtension); files.add(newFile); bw = new BufferedWriter(new FileWriter(newFile)); try { byte[] c = new byte[1024]; int lineCount = 0; int readChars = 0; while ( ( readChars = is.read(c) ) != -1 ) for ( int i=0; i&lt;readChars; i++ ) { bw.write(c[i]); if ( c[i] == '\n' ) if ( ++lineCount % linesPerFile == 0 ) { bw.close(); newFile = new File(filePathBase + "." + String.valueOf(files.size() + 1) + fileExtension); files.add(newFile); bw = new BufferedWriter(new FileWriter(newFile)); } } } finally { bw.close(); is.close(); } </code></pre> <p>My understanding of character encoding is that everything should remain the same as long as i keep each byte the same. Why does this code alter bytes?</p> <p>Thanks a bunch in advance~</p> <p>==================== SOLUTION ====================</p> <p>The mistake is that <code>FileWriter</code> interprets bytes and shouldn't be used to just output plain bytes, thanks @meriton and @Jonathan Rosenne. Just changing everything to <code>BufferedOutputStream</code> doesn't do it though, since <code>BufferedOutputStream</code> is too slow! I ended up improving my file split-and-copy code to include a bigger read-array size and only <code>write()</code> when necessary ...</p> <pre><code>File newFile = new File(filePathBase + "." + String.valueOf(files.size() + 1) + fileExtension); files.add(newFile); InputStream iS = new BufferedInputStream(new FileInputStream(file)); OutputStream oS = new FileOutputStream(newFile); // BufferedOutputStream wrapper toooo slow! try { byte[] c; if ( linesPerFile &gt; 65536 ) c = new byte[65536]; else c = new byte[1024]; int lineCount = 0; int readChars = 0; while ( ( readChars = iS.read(c) ) != -1 ) { int from = 0; for ( int idx=0; idx&lt;readChars; idx++ ) if ( c[idx] == '\n' &amp;&amp; ++lineCount % linesPerFile == 0 ) { oS.write(c, from, idx+1 - from); oS.close(); from = idx+1; newFile = new File(filePathBase + "." + String.valueOf(files.size() + 1) + fileExtension); files.add(newFile); oS = new FileOutputStream(newFile); } oS.write(c, from, readChars - from); } } finally { iS.close(); oS.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload