java RandomAccessFile的问题,输出不正确啊,怎么改能输出正确的名字和年龄。求指教
classPerson{privateStringname;privateintage;publicPerson(Stringname,intage){if(name.l...
class Person{
private String name;
private int age;
public Person(String name, int age) {
if(name.length()>8)
name=name.substring(0, 8);
else if(name.length()<8)
name=name+"\u0000";
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class FileAccessRamdoms {
public static void writes() throws Exception{
Person p1=new Person("jiaohch", 27);
Person p2=new Person("zhanght", 28);
Person p3=new Person("renjianliang", 26);
RandomAccessFile ff = new RandomAccessFile("youToBe.txt","rw");
ff.writeBytes(p1.getName());
ff.writeInt(p1.getAge());
ff.writeBytes(p2.getName());
ff.writeInt(p2.getAge());
ff.writeBytes(p3.getName());
ff.writeInt(p3.getAge());
if(ff !=null)
ff.close();
System.out.println("=======================");
}
public static void reads() throws Exception{
RandomAccessFile ff = new RandomAccessFile("youToBe.txt","rw");
System.out.println("跳过第一个人");
ff.skipBytes(12);
String name="";
for(int i=0;i<7;i++){
name=name+(char)ff.readByte();
}
System.out.println("name="+name);
System.out.println("age="+ff.readInt());
System.out.println("第三个");
name="";
for(int i=0;i<8;i++){
name=name+(char)ff.readByte();
}
System.out.println("name="+name);
System.out.println("age="+ff.readInt());
System.out.println("回到第一个");
ff.seek(0);
name="";
for(int i=0;i<7;i++){
name=name+(char)ff.readByte();
}
System.out.println("name="+name);
System.out.println("age="+ff.readInt());
}
public static void main(String[] args) throws Exception {
writes();
reads();
}
} 展开
private String name;
private int age;
public Person(String name, int age) {
if(name.length()>8)
name=name.substring(0, 8);
else if(name.length()<8)
name=name+"\u0000";
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class FileAccessRamdoms {
public static void writes() throws Exception{
Person p1=new Person("jiaohch", 27);
Person p2=new Person("zhanght", 28);
Person p3=new Person("renjianliang", 26);
RandomAccessFile ff = new RandomAccessFile("youToBe.txt","rw");
ff.writeBytes(p1.getName());
ff.writeInt(p1.getAge());
ff.writeBytes(p2.getName());
ff.writeInt(p2.getAge());
ff.writeBytes(p3.getName());
ff.writeInt(p3.getAge());
if(ff !=null)
ff.close();
System.out.println("=======================");
}
public static void reads() throws Exception{
RandomAccessFile ff = new RandomAccessFile("youToBe.txt","rw");
System.out.println("跳过第一个人");
ff.skipBytes(12);
String name="";
for(int i=0;i<7;i++){
name=name+(char)ff.readByte();
}
System.out.println("name="+name);
System.out.println("age="+ff.readInt());
System.out.println("第三个");
name="";
for(int i=0;i<8;i++){
name=name+(char)ff.readByte();
}
System.out.println("name="+name);
System.out.println("age="+ff.readInt());
System.out.println("回到第一个");
ff.seek(0);
name="";
for(int i=0;i<7;i++){
name=name+(char)ff.readByte();
}
System.out.println("name="+name);
System.out.println("age="+ff.readInt());
}
public static void main(String[] args) throws Exception {
writes();
reads();
}
} 展开
1个回答
展开全部
import java.io.RandomAccessFile;
class Person{
private String name;
private int age;
public Person(String name, int age) {
if(name.length()>8)
name=name.substring(0, 8);
else if(name.length()<8)
name=name+"\u0000";
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class FileAccessRamdoms {
public static void writes() throws Exception{
Person p1=new Person("jiaohch", 27);
Person p2=new Person("zhanght", 28);
Person p3=new Person("renjianliang", 26);
RandomAccessFile ff = new RandomAccessFile("youToBe.txt","rw");
ff.writeBytes(p1.getName());
ff.writeInt(p1.getAge());
ff.writeBytes(p2.getName());
ff.writeInt(p2.getAge());
ff.writeBytes(p3.getName());
ff.writeInt(p3.getAge());
if(ff !=null)
ff.close();
System.out.println("=======================");
}
public static void reads() throws Exception{
RandomAccessFile ff = new RandomAccessFile("youToBe.txt","rw");
System.out.println("跳过第一个人");
ff.skipBytes(12);
String name="";
for(int i=0;i<7;i++){//此处你只read了7个字节。而name是都是8个字节所以下面读年龄时要越过一个字节
name=name+(char)ff.readByte();
}
System.out.println("name="+name);
ff.skipBytes(1);//前面你只read了7个字节。而name是都是8个字节所以下面读年龄时要越过一个字节
System.out.println("age="+ff.readInt());
System.out.println("第三个");
name="";
for(int i=0;i<8;i++){
name=name+(char)ff.readByte();
}
System.out.println("name="+name);
System.out.println("age="+ff.readInt());
System.out.println("回到第一个");
ff.seek(0);
name="";
for(int i=0;i<7;i++){//此处同理
name=name+(char)ff.readByte();
}
System.out.println("name="+name);
ff.skipBytes(1);//此处同理 还有如果是中文名字需要用writeChar readChar qqqun交流21、777/12
System.out.println("age="+ff.readInt());
}
public static void main(String[] args) throws Exception {
writes();
reads();
}
}
追问
多谢亲,还有两个不解的地方:
1、youTOBe文件里显示的是“jiaohch zhanght renjianl ”,看不到年龄啊?
2、else if(name.length()<8) name=name+"\u0000";后面的+"\u0000"啥意思?
追答
int使用的是一个4个字节保存的,这个并不影响读出,想显示出来必须转换为字符串
\u0000是已是一个Unicode字符,代表的应该是NULL,输出控制台是一个空格
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询