Why byte stream print to the command line Chinese display is garbled, and written to a file inside the Chinese able to properly display

Why print to the command line Chinese display is garbled, and written to a file inside the Chinese able to show normal?
Note: I feel like Notepad is not the cause, because the byte read, and if reading is garbled, garbled should also be written.

It is precisely because the byte stream has not been "decoding - code" of this process, it will not be because of the different encoding and decoding and encoding used to generate an error (character stream will)
actually **, with a byte stream to ensure that the source file and the target file is exactly the same, of course, open the file is a normal matter, **
and printed on the screen is wrong because you put a two-byte characters, your code to print two-byte character as two out, nature is garbled

import java.io. *;

public class FileInputStream1 {

public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream(new File("d:\\aaa.txt"));
FileOutputStream ou = new FileOutputStream(new File("d:\\bb.txt"));

len int;
byte [] = new new byte B [10];
the while (! (len = in.read (B)) = -1) {// read the code is not clear after a few byte character printing so garbled
ou.write (B, 0, len);
of System.out.print ((char) len); // if the alphabet is not distortion because the letters a byte, the Asc code table exists, can be directly transferred char type byte to read letters that is char
}
in.close ();
ou.close ();
}
}

FileInputStream FileOutputStream and can read and write files directly (i.e., can be completed Copy), if desired printing characters read in the console needed to wrap InputStreamReader commutations turn into characters.

 

Guess you like

Origin www.cnblogs.com/peishaobo/p/11347454.html