java字符集编码_Java 字符集编码

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文件

21eb0e89b9e5376d8e67560c0e83ae7f.png

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

25db7138a664ae64721d723253bdfbf2.png

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值