Java character set encoding

1, NioTest13_In.txt file contents are copied to the file NioTest13_Out.txt
public class NioTest13 {

    public static void main(String[] args) throws  Exception {
        String inputFile = "NioTest13_In.txt";
        String outFile = "NioTest13_Out.txt";

        RandomAccessFile inputRandomAccessFile = new RandomAccessFile(inputFile,"r");

        RandomAccessFile outputRandomAccessFile = new RandomAccessFile(outFile,"rw");

        long inputLength = new File(inputFile).length();

        FileChannel inputFileChannel = inputRandomAccessFile.getChannel();
        FileChannel outputFileChannel = outputRandomAccessFile.getChannel();

        MappedByteBuffer inputData = inputFileChannel.map(FileChannel.MapMode.READ_ONLY, 0, inputLength);
        System.out.println("================================");
        /*Charset.availableCharsets().forEach( (k,v) -> {
            System.out.println(k + ", " + v);
        });*/
        System.out.println("================================");

        Charset charset = Charset.forName("iso-8859-1"); //utf-8
        CharsetDecoder decoder = charset.newDecoder(); //字节数组转字符串
        CharsetEncoder encoder = charset.newEncoder(); //字符串转字符数组

        CharBuffer charBuffer = decoder.decode(inputData);

         ByteBuffer outputData = encoder.encode(charBuffer);

        outputFileChannel.write(outputData);

        inputRandomAccessFile.close();
        outputRandomAccessFile.close();
    }
}

  

2. Create a "NioTest13_In.txt file

 

3, the implementation of epigenetic became NioTest13_Out.txt file

Use can know: Charset charset = Charset.forName ( "iso-8859-1"); // utf-8

Using iso-8859-1 and utf-8, Chinese display is normal

 

Guess you like

Origin www.cnblogs.com/linlf03/p/11369291.html