ByteBuffer Converting CharBuffer
来源:互联网 发布:淘宝助理连打设置 编辑:程序博客网 时间:2024/06/05 15:07
****Java所有对象,只认识Unicode的数据 :
即1> Unicode 基本编码(2个字节表示一个符号)。
2>存储加工的1>>utf-8,
2>>utf-16 big endian (无bom ,有bom)
3>>utf-16 little endian (无bom ,有bom)
所以当我们给别人东西时编码:可以用Unicode 基本编码,或其它几种。
所以当别人给我们东西时我只接收:Unicode 基本编码,或其它几种,如果是其它编码格式,必须在接受输入时进行转换。
查询当前系统:UTF-8格式,如果一个文件有中文和Ascii码 UTF-8 无Bom 应该最省空间。
以前整个测试发现: 严1a
1、Unicode 保存6字节
2、UTF-16 little endian 无bom 保存6字节
3、 UTF-16 Big endian 无bom 保存12 字节
4、UTF-16 little endian 有bom 保存8字节
5、UTF-16 Big endian 有bom 保存14字节
6、UTF-8 (默认有Bom) EF BB BF E4 B8 A5 31 61 (8个字节)
7、UTF-8 无Bom E4 B8 A5 31 61 (5个字节)
如果让java对象做事,必须用它们所认识的语言。
目的:1》就是想打印到控制台看看,打印的对象只支持 String。
2》把byte 转换成 char,ByteBuffer 转换成 CharBuffer
一:简单描述
char: 16bits Unicode0 Unicode 2^16-1 Character
byte :8bits -128 +127 Byte
1、byte :只是一个由0,1构成的 8位数
1》比方 如果取出的是ASCII码 0--127 符号,哪么这8位表示一个完整的符号。
2》取出是中文,UTF-8 :有可能3个字节或2个字节表示一个字,
2、char:就是ASCII符号没问题,哪么中文
一个字符(一个字):由几个字节构成,所以一个字符要保存3个或2个字节,表示完整的一个字。
二、建立小程序
1、建立 data3.txt 文本文件,保存编码是Unicode(上面7种都可以).因为只认识Unicode(上面7种都可以)文件,
如果不是Unicode(上面7种都可以)格式时,必须以Unicode格式输入。
2、通过java的 FileInputStream 去取-----放入ByteBuffer中。
3、得到char (Unicode 编码,到底是那种UTF-8,或UTF-16)的编码。
System.getProperty("file.encoding");:得到char的编码 ==UTF-8
4、转换 Charset.forName(encoding).decode(buff)) 。
即几个字节合并成一个字节。
package io;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.io.*;
public class BufferToText {
private static final int BSIZE = 1024;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
FileChannel fc = new FileInputStream("data3.txt").getChannel();
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
fc.read(buff);
buff.flip();
String encoding = System.getProperty("file.encoding");
//解码方式输出,表示从是UTF-8 文本文件取出来的数据,转换成char。
System.out.println("Decoded using " + encoding + ": "+ Charset.forName(encoding).decode(buff));
// Or, we could encode with something that will print:
}
}
- ByteBuffer Converting CharBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- ByteBuffer
- Dx trouble processing "java/nio/CharBuffer.class":
- The method read(CharBuffer) from the type Reader refers to the missing type CharBuffer
- The method read(CharBuffer) from the type Reader refers to the missing type CharBuffer
- ByteBuffer用法
- ByteBuffer使用
- java bytebuffer
- ByteBuffer 类
- 关于ByteBuffer
- ByteBuffer.array()
- javascript常用的正则(一)
- Spring事务配置的五种方式
- 在IDEA上面书写的代码用IDEA自带git 到github上面失败的解决之道
- 网络编程基础——Socket编程
- Spring3事务管理——使用原始的TransactionProxyFactoryBean
- ByteBuffer Converting CharBuffer
- C语言一维数组的定义和引用
- 杨辉三角最优解法
- ubuntu16添加引导win10的启动项
- php:foreach同时使用$value和&$value时的一个坑
- 三、solr集群搭建
- 微信小程序 表单验证
- SpringMVC 前后台传参(对象集合,Map参数)
- 【redis】redis使用前后性能对比