第一个问题:
编码应该是UTF-8的字符编码(Unicode的一种变种),这种编码对常用的ASCII码仍然采用1个字节,而对其他不常用的字符集采用2个字节,这样做可以大大节省资源。

解决方案 »

  1.   

    第二个问题:
    try{
        byte[] b;
        RandomAccessFile rf = new RandomAccessFile("e:\\my.txt","rw");            
        String str = "ab";
        b = str.getBytes();
        rf.write(b);
        // rf.close();  
        // rf = new RandomAccessFile("e:\\my.txt","rw");  
        // 为什么这两行注释掉,就读不道内容,非要先关一次,再初始化一次,才能读
        // 回答:因为此时cursor已经到了文件尾,所以你读不道内容,
        // 你可以使用seek来定位到起始位置
        rf.seek(rf.length() - b.length);
        // System.out.print(rf.readChar());  
        // 为什么这个结果不是a?? 
        // 回答:因为这个char对应两个字节,所以读取的是a+b构成的一个unicode编码
        
        System.out.print((char) rf.readByte());  // 这样就是a   }catch(Exception e){
       }
      

  2.   

    to  archangelye(天使联盟) :
    如果我写入String str = "中国";
    为什么readChar出来,第一个是个?
      

  3.   

    发现把write(byte[])
    改成writeChars就可以了            RandomAccessFile rf = new RandomAccessFile("e:\\my.txt","rw");            
                String str = "china is 中国";
                // b = str.getBytes();
                rf.writeChars(str);
                rf.seek(rf.length() - str.length() * 2);           
                for(int i = 0; i < str.length(); i ++) {
                    System.out.print(rf.readChar());
                    if(i == str.length() - 1)
                        System.out.println();
                }
                  }catch(Exception e){
               }