导航

write(int c)之"16 low-order bits"和"16 high-order bits"的理解

Posted on 2012-06-20 11:47  鹤唳九天  阅读(1058)  评论(0编辑  收藏  举报

JDK API 1.6 中文帮助文档中和LZ意思一样,这么写的:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void write(int c)
           throws IOException写入单个字符。要写入的字符包含在给定整数值的 16 个低位中,16 高位被忽略。 
用于支持高效单字符输出的子类应重写此方法。 


参数:
c - 指定要写入字符的 int。 
抛出: 
IOException - 如果发生 I/O 错误
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

例子:

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
import java.io.*; 

public class Test { 
 public static void main(String[] args) throws IOException {
  FileWriter wr=new FileWriter("test");
  int i=0x1234007F;
  wr.write(i);
  wr.close();
  FileReader rd=new FileReader("test");
  char[] chBuff=new char[2];
  rd.read(chBuff);
  rd.close();
  System.out.print(Integer.toHexString(chBuff[1])+" ");
  System.out.println(Integer.toHexString(chBuff[0]));
 }
}

//输出的是 0 7f,高16位的0x1234被屏蔽了

=============================
=============================
这个例子比上面那个好点

import java.io.*; 

public class Test { 
 public static void main(String[] args) throws IOException {
  FileWriter wr=new FileWriter("test.txt");
  char i1='我';
  char i2='你';
  int i=(i1<<16)+i2;
  wr.write(i);
  wr.close();
  FileReader rd=new FileReader("test.txt");
  char[] chBuff=new char[2];
  rd.read(chBuff);
  rd.close();
  System.out.print(Integer.toHexString(chBuff[1])+" ");
  System.out.println(Integer.toHexString(chBuff[0]));
 }
}