Java character sets, encoding, decoding

 

1、

A computer file, the underlying data are based on binary.

Bottom of the computer and not a text file, image file of the points, it just records the binary sequence of each file.

 

Character set: contains a correspondence relationship between characters and binary sequence, a binary sequence corresponding to a character.

 

Encoding (Encode): people can understand the plaintext into a computer can understand binary sequence.

Decoding (Decode): the computer can read the binary sequence into a human plaintext.

 

Garbled: encoding decoding scheme in different ways, i.e. not match the character set encoding and decoding use.

 

The default character set of the text file in Windows is GBK.

 

 

 

 

 

2、

Byte file output stream can specify the write mode:

FileOutputStream fos=new FileOutputStream(String/File file);
FileOutputStream fos = new FileOutputStream (String / File file, boolean b); // second parameter specifies whether the append mode, true-- added, false-- cover.


File stream of characters you can specify the character set:

FileWriter fw=new FileWriter(String/File file);
FileReader fr=new FileReader(String/File file);
 //指定字符集
FileWriter fw=new FileWriter(String/File file, String/Charset charset);
FileReader fr=new FileReader(String/File file, String/Charset charset);

// file output stream of characters may also specify the write mode 
FileWriter fw = new FileWriter (String / File file, boolean b); // whether the append mode
FileWriter fw = new FileWriter (String / File file, String / Charset charset, boolean b);

 

Two commutations can specify the encoding / decoding character sets:

InputStreamReader isr = new InputStreamReader (InputStream is , String / Charset charset); // decode the character set specified 
OutputStreamWriter osr = new OutputStreamWriter (OutputStream os , String / Charset charset); // specified coded character set




 

 

3, NIO class of Charset

1    // Create Charset object that specifies the encoding / decoding character set 
2          Charset charset = Charset.forName ( "GBK" );
 . 3  
. 4          // Create encoder 
. 5          a CharsetEncoder, Encoder = charset.newEncoder ();
 . 6          // encoding It is encoded. Convert the character sequence is encoded as sequences of bytes, is the only parameter CharBuffer type, the type of the return value is ByteBuffer 
. 7          ByteBuffer ByteBuffer = encoder.encode (CharBuffer CharBuffer);
 . 8          
. 9          // Create a decoder 
10          of a CharsetDecoder Decoder = charset.newDecoder ( );
 11          // use decoders for decoding. Decoding a sequence of bytes is converted to a sequence of characters, type ByteBuffer parameters, return value is CharBuffer type 
12         CharBuffer charBuffer=decoder.decode(ByteBuffer byteBuffer);

 

You may not create encoder, decoder:

1     // Create Charset object that specifies the encoding / decoding of character sets 
2          Charset charset = Charset.forName ( "GBK" );
 . 3  
. 4          // encoding 
. 5          the ByteBuffer ByteBuffer Charset.encode = (String STR);   // here make a String parameter 
. 6          the ByteBuffer ByteBuffer = Charset.encode (CharBuffer CharBuffer);
 . 7          
. 8          // decoding 
. 9          CharBuffer CharBuffer = charset.decode (the ByteBuffer ByteBuffer);

Easier.

 

 

String object class can use the following methods String (character sequence) is encoded as byte [] (sequence of bytes):

  • byte [] getBytes () // use the platform's default character set
  • byte [] getBytes (String / Charset charset) // using the specified character set

Good coding sequence of bytes returned.

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/10936786.html