package RandomTest;/**
 *
 * @author Z
 */
import java.io.*;
public class RandomAccessFileTest1 
{
    
    /** Creates a new instance of RadomAccessFileTest */
    File f=new File("D:\\javaprogram\\src\\f1.txt");//注意路径的表示
    public int lenn=8;
    public int lena=3;
    public RandomAccessFileTest1()
    {
        String strc=new String("");
        String strc1=new String("");
        Person1 p1=new Person1("王五",25,"男");
        Person1 p2=new Person1("李四",24,"男");
        Person1 p3=new Person1("张三",23,"女");
        try
        {
            RandomAccessFile raf=new RandomAccessFile(f,"rw");
            raf.writeChars(p1.name);
            raf.writeChars(new String().valueOf(p1.age));
            raf.writeChars(p1.sex);
            
            raf.seek(0);
            while(lenn>0)
            {
                strc1=strc1+raf.readChar();
                lenn--;
            }
            System.out.println(strc);
            
            while(lena>0)
            {
                strc1=strc1+raf.readChar();
                lena--;
            }
            System.out.println(strc1);
            
        }
        catch(Exception e)
        {
           e.printStackTrace();
        }
    }
    public static void main(String args[])
    {
        RandomAccessFileTest1 raf=new RandomAccessFileTest1();
    }
}package RandomTest;/**
 *
 * @author Z
 */
public class Person1
{
    String name;
    int age;
    String sex;
    public static int lenn=8;
    public static int lena=3;
    public static String str=new String();
    /** Creates a new instance of Person */
    public Person1(String name,int age,String sex) 
    {
        str=str.valueOf(age);
        while(name.length()<lenn)
                  this.name=name+"\u0000";
        while(str.length()<lena)
                          str="0"+str;
        this.age=Integer.parseInt(str);
        this.sex=sex;
    }
    
}
这个程序运行的结果不是我想要的并且可能导致死机,大家要小心!!!及时杀死这个进程,请各位高人帮我找出原因

解决方案 »

  1.   

    改了点
    先试试
    import java.io.*;public class RandomAccessFileTest1 { /** Creates a new instance of RadomAccessFileTest */
    File f = new File("D:\\javaprogram\\src\\f1.txt");// 注意路径的表示 public int lenn = 8; public int lena = 3; public RandomAccessFileTest1() {
    String strc = new String("");
    String strc1 = new String("");
    Person1 p1 = new Person1("王五", 25, "男");
    Person1 p2 = new Person1("李四", 24, "男");
    Person1 p3 = new Person1("张三", 23, "女");
    try {
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    raf.writeChars(p1.name);
    // raf.writeChars(new String().valueOf(p1.age));
    raf.writeChars(String.valueOf(p1.age));
    raf.writeChars(p1.sex); raf.seek(0);
    while (lenn > 0) {
    strc1 = strc1 + raf.readChar();
    lenn--;
    }
    System.out.println(strc); while (lena > 0) {
    strc1 = strc1 + raf.readChar();
    lena--;
    }
    System.out.println(strc1); } catch (Exception e) {
    e.printStackTrace();
    }
    } public static void main(String args[]) {
    RandomAccessFileTest1 raf = new RandomAccessFileTest1();
    }
    }class Person1 {
    String name; int age; String sex; public static int lenn = 8; public static int lena = 3; public static String str = new String(); /** Creates a new instance of Person */
    public Person1(String name, int age, String sex) {
    str = str.valueOf(age);
    /*while (name.length() < lenn)
    this.name = name + "\u0000";*/
    this.name = name;
    while (this.name.length() < lenn)
    this.name += "\u0020";
    while (str.length() < lena)
    str = "0" + str;
    this.age = Integer.parseInt(str);
    this.sex = sex;
    }}