Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:写入RandomAccessFile文件时,怎样换行? [Re:liugongquan]
Butterfly_125





发贴: 26
积分: 0
于 2005-07-19 13:30 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
 
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",22);
    Employee e3 = new Employee("wangwu",21);
    RandomAccessFile ra = new RandomAccessFile("c:\\1.txt","rw");
    ra.write(e1.name.getBytes());
    ra.writeInt(e1.age);
    ra.write(e2.name.getBytes());
    ra.writeInt(e2.age);
    ra.write(e3.name.getBytes());
    ra.writeInt(e3.age);
    ra.close();
    RandomAccessFile raf = new RandomAccessFile("c:\\1.txt","r");
    int len = 8;
    raf.skipBytes(12);  //跳过第一个员工的信息,其中姓名8字节,年龄4字节
    System.out.println("第二个员工信息:");
    String str = "";
    for(int i = 0;i < len;i++)
    {
      str = str + (char)raf.readByte();
    }
    System.out.println("name:" + str);
    System.out.println("age:" + raf.readInt());
    System.out.println("第一个员工信息:");
    raf.seek(0);  //将文件指针移到文件开始位置
    str = "";
    for(int i = 0;i < len;i++)
    {
      str = str + (char)raf.readByte();
    }
    System.out.println("name:" + str);
    System.out.println("age:" + raf.readInt());
    System.out.println("第三个员工信息:");
    raf.skipBytes(12);
    str = "";
    for(int i = 0;i < len;i++)
    {
      str = str + (char)raf.readByte();
    }
    System.out.println("name:" + str);
    System.out.println("age:" + raf.readInt());
    raf.close();
  }
}
class Employee
{
  String name;
  int age;
  final static int LEN = 8;
  public Employee(String name,int age)
  {
    if(name.length()>LEN)
    {
      name = name.substring(0,8);
    }
    else
    {
      while(name.length()<LEN)
        name = name +"\u0000";
    }
    this.name = name;
    this.age = age;
  }
}

ra.write(e1.name.getBytes());中getBytes()是什么意思?
SDK是这样说的:
Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.

我还是看不太懂。如果e1.name为中文,输出就有点问题,这是什么?




话题树型展开
人气 标题 作者 字数 发贴时间
8857 写入RandomAccessFile文件时,怎样换行? liugongquan 46 2005-06-17 20:30
7719 Re:写入RandomAccessFile文件时,怎样换行? why 154 2005-06-21 20:34
7667 Re:写入RandomAccessFile文件时,怎样换行? liugongquan 98 2005-06-22 08:51
7425 Re:写入RandomAccessFile文件时,怎样换行? jiyunang 44 2005-07-19 12:56
7619 Re:写入RandomAccessFile文件时,怎样换行? snowbird2005 192 2005-07-19 19:03
8012 Re:写入RandomAccessFile文件时,怎样换行? Butterfly_125 2130 2005-07-19 13:30

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923