import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{

RandomAccessFile inout = new RandomAccessFile("c:/aa.txt","rw");
inout.writeUTF("我是一个中国人");
inout.close();
InputStreamReader in = new InputStreamReader(new FileInputStream("c:/aa.txt"),"utf-8");
int ch;
while((ch=in.read())!=-1){
System.out.print((char) ch);
}
                        in.close();

}
}以上程序先是输出两个"口"子型的符号,随后输出"我是一个中国人",请问这两个"口"子型符号是怎么得来的?最好能深入谈一下字符集编码!

解决方案 »

  1.   

    import java.io.*;
    public class Test1{
    public static void main(String[] args){
    FileWriter fw = null;
    try{
     fw = new FileWriter("d:\\bb.txt");
    System.out.println("for");

      fw.write("我是中国人");
    fw.close();
    }catch(IOException e){
    System.out.println("exception");
    }
    }
    }
    用writer就不会有问题了。RandomAccessFile 
    此类的实例支持对随机存取文件的读取和写入。随机存取文件的行为类似存储在文件系统中的一个大型字节数组。存在指向该隐含数组的光标或索引,称为文件指针
    writeUTF方法:
    使用 modified UTF-8 编码以与机器无关的方式将一个字符串写入该文件。
    首先,把两个字节从文件的当前文件指针写入到此文件,类似于使用 writeShort 方法并给定要跟随的字节数。此值是实际写出的字节数,而不是该字符串的长度。在该长度之后,按顺序输出该字符串的每个字符,并对每个字符使用 UTF-8 修改版编码
    我想那个奇怪的东西就是两个字节。但是具体是什么我不太清楚。等待牛人解释。