码迷,mamicode.com
首页 > 编程语言 > 详细

Java 字符集编码

时间:2019-08-17 17:36:17      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:img   ble   字符串   rgs   enc   oid   http   output   bsp   

1、NioTest13_In.txt文件内容拷贝到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、创建"NioTest13_In.txt文件

技术图片

 

3、执行后生成了NioTest13_Out.txt 文件

技术图片

可以知道使用: Charset charset = Charset.forName("iso-8859-1"); //utf-8

使用iso-8859-1和utf-8,中文显示都是正常的

 

Java 字符集编码

标签:img   ble   字符串   rgs   enc   oid   http   output   bsp   

原文地址:https://www.cnblogs.com/linlf03/p/11369291.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!