|
查看: 1302|回复: 3
|
JAVA double-byte vs single-byte
[复制链接]
|
|
|
请问有没有方法可以determine一个char是double-byte或者是single-byte呢?
api也可以. |
|
|
|
|
|
|
|
|
|
|
发表于 4-9-2007 09:54 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 5-9-2007 09:02 AM
|
显示全部楼层
原帖由 pony 于 4-9-2007 09:54 PM 发表 
JAVA 的 char 不都是 16 bits (double bytes) 的吗?
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html
其实我要计算一个file里有多少个byte...
因为file里会有华语和英文.....
华语=double byte
英文=single byte
以便save in db 时不会超过field的length...
不过我找到了...
用java.nio.*
try{
BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\test.txt")));
String str = "";
int i=1;
while((str=fileReader.readLine())!=null){
CharsetEncoder coder = (CharsetEncoder)Charset.forName("utf-8").newEncoder();
CharBuffer source = CharBuffer.wrap(str);
System.out.println("line no "+i+" :"+source.length());
i++;
}
}catch(Exception e){
e.printStackTrace();
} |
|
|
|
|
|
|
|
|
|
|
发表于 5-9-2007 11:31 AM
|
显示全部楼层
JAVA 的 char 都是用 UTF-16 的,所以华文与英文都是 double bytes。
我不晓得你用什么资料库,不过如果你用 nvarchar data type ,华文与英文的算法是一样的。
至于你的代码,你忘了指定 InputStreamReader 的 Charset ,除非你的 Default Charset 是 utf-8 ,不然 readLine 得到的 str 是看不到华文的。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|