黑马程序员技术交流社区

标题: 求高手来指导!!IO [打印本页]

作者: 意映    时间: 2012-7-24 22:08
标题: 求高手来指导!!IO
import java.io.*;
public class RandomFileTest {
        public static void main(String[] args) throws Exception
        {
                Employee e1=new Employee("zhangsan",23);
                Employee e2=new Employee("lisi",29);
                Employee e3=new Employee("wangwu",19);
                RandomAccessFile ra=new RandomAccessFile("employee.txt","rw");
                ra.write(e1.name.getBytes());
                ra.write(e1.age);
                ra.write(e2.name.getBytes());
                ra.write(e2.age);
        ra.write(e3.name.getBytes());
                ra.write(e3.age);
                ra.close();
                int len=0;
                byte[] buf=new byte[8];
                String strName=null;
                RandomAccessFile raf=new RandomAccessFile("employee.txt","r");
                raf.skipBytes(12);
                len=raf.read(buf);
         strName=new String(buf,0,len);
         System.out.println(strName+" : "+raf.read());
         raf.seek(0);
         len=raf.read(buf);
         strName=new String(buf,0,len);
         System.out.println(strName+" : "+raf.read());
         raf.skipBytes(12);
         len=raf.read(buf);
         strName=new String(buf,0,len);
         System.out.println(strName+" : "+raf.read());
         raf.close();
                }
}
class Employee {
       
         public String name=null;
        public int age=0;
        public static final int  LEN=8;
        public Employee(String name,int age){
                if(name.length()>LEN){
                        this.name=name.substring(1,LEN);
                }
                else{
                        while(name.length()<LEN){
                        this.name+="\0000";
                        }
                }
                this.name=name;
                this.age=age;
       
        }

}怎么程序没有结果求高手指导??
作者: 刘春发    时间: 2012-7-24 22:29
while(name.length()<LEN){
                        this.name+="\0000";//死循环了.name和this.name是不同的,加的是this.name,判断的却是name
               }

作者: 意映    时间: 2012-7-24 22:39
刘春发 发表于 2012-7-24 22:29
while(name.length()

我改成name 了,怎么结果不正确
作者: 刘春发    时间: 2012-7-24 22:44
意映 发表于 2012-7-24 22:39
我改成name 了,怎么结果不正确

还不正确?我这边看着可以了啊!还是什么都没有吗?
作者: 王志明    时间: 2012-7-24 23:16
你的问题解决了,看注释。。
  1. package test2;

  2. import java.io.RandomAccessFile;

  3. public class RandomFileTest {
  4.         public static void main(String[] args) throws Exception {
  5.                 Employee e1 = new Employee("zhangsan", 23);
  6.                 Employee e2 = new Employee("lisi", 29);
  7.                 Employee e3 = new Employee("wangwu", 19);

  8.                 RandomAccessFile ra = new RandomAccessFile("src/employee.txt", "rw");
  9.                 ra.write(e1.name.getBytes());
  10.                 ra.write(e1.age);
  11.                 ra.write(e2.name.getBytes());
  12.                 ra.write(e2.age);
  13.                 ra.write(e3.name.getBytes());
  14.                 ra.write(e3.age);
  15.                 ra.close();

  16.                 int len = 0;
  17.                 byte[] buf = new byte[8];
  18.                 String strName = null;
  19.                 RandomAccessFile raf = new RandomAccessFile("src/employee.txt", "r");
  20.                 raf.skipBytes(12);
  21.                 len = raf.read(buf);
  22.                 strName = new String(buf, 0, len);
  23.                 System.out.println(strName + " : " + raf.read());
  24.                 raf.seek(0);
  25.                 len = raf.read(buf);
  26.                 strName = new String(buf, 0, len);
  27.                 System.out.println(strName + " : " + raf.read());
  28.                 raf.skipBytes(12);
  29.                 len = raf.read(buf);
  30.                 strName = new String(buf, 0, len);
  31.                 System.out.println(strName + " : " + raf.read());
  32.                 raf.close();
  33.         }
  34. }

  35. class Employee {
  36.         public String name = null;
  37.         public int age = 0;
  38.         public static final int LEN = 8;

  39.         public Employee(String name, int age) {
  40.                 //首先,你要先为name,age赋值,再操作name
  41.                 this.name = name;
  42.                 this.age = age;
  43.                
  44.                 //if和while的括号里要判断的是this.name,不是name,搞清楚了
  45.                 if (this.name.length() > LEN) {
  46.                         this.name = name.substring(0, LEN);
  47.                 } else {
  48.                         while (this.name.length() < LEN) {
  49.                                 this.name += "\0000";
  50.                         }
  51.                 }
  52.         }
  53. }
复制代码

作者: 李菁    时间: 2012-7-24 23:39
class Employee {
        
         public String name=null;
        public int age=0;
        public static final int  LEN=8;
        public Employee(String name,int age){
                if(name.length()>LEN){       应该是this.name
                        this.name=name.substring(1,LEN);
                }
                else{
                        while(name.length()<LEN){      也是this.name
                        this.name+="\0000";
                        }
                }
                this.name=name;
                this.age=age;
        
        }

}
作者: 意映    时间: 2012-7-25 00:43
Mrng8888 发表于 2012-7-24 23:16
你的问题解决了,看注释。。

谢谢各位的帮助  




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2