这里原因很简单你在写入文件时,把boolean 和 int 型写到一行了。
所以在文件内容和显示结果都是错误的。
如果你把这两个分成两行就可以解决了。
你的原文件改过如下:
import java.awt.*;
import java.io.*;
public class RandomAccessTest extends Frame {
public static void main(String args[]) {
  RandomAccessTest test=new RandomAccessTest();
  TextArea ta=new TextArea();
  byte[] b=new byte[8];
  try {
  RandomAccessFile raf=new RandomAccessFile("temp.txt","rw");
  raf.writeBytes("测试一下 \r\n ");
  raf.writeBytes("abc456 \r\n ");
  raf.writeBoolean(true);
  raf.writeBytes("\r\n");
  raf.writeInt(123);
  raf.seek(0);
  raf.readFully(b);
  ta.append("newstring: "+new String(b)+"\n");
  ta.append("readline: "+raf.readLine()+"\n");
  ta.append("readboolean: "+raf.readBoolean()+"\n");
  ta.append(raf.readLine());
  ta.append("readint: "+raf.readInt()+"\n");
  raf.close();
  }
  catch(EOFException e) {}
  catch(IOException e) {
  System.out.println("IOException ");
  }
  test.add(ta,BorderLayout.CENTER);
  test.setSize(300,200);
  test.show();
}
}

解决方案 »

  1.   

    zylfarzero(等待你的回答)兄台:
      我按你的方法试后,仍不行,仍是生成的temp.txt文件和Frame图框都不能正常显示。
    但我把部分程序改为如下所示后:
      raf.write(new String("测试一下").getBytes()); //此处不要了“\r\n ”
      raf.writeBytes("abc456 \r\n ");
      raf.writeBoolean(true);
      raf.writeBytes("\r\n");
      raf.writeInt(123);
      raf.seek(0);
      则Frame图框显示正常,但生成的temp.txt文件一样不正常,其中"true"显示为一个黑框,而"123"显示为"{"。请高手继续帮忙!!!!