不明白的一道Java题!
Java code:
import java.io.IOException;
import java.io.File;
import java.io.RandomAccessFile;
class RandomFileTest
{
public static void main(String[] args) throws Exception
{
Student s1=new Student(1, "zhangsan", 90.5);
Student s2=new Student(2, "lisi", 98.5);
Student s3=new Student(3, "wangwu", 96.5);
File userDir=new File(System.getProperties().getProperty("user.dir"));
File tempFile=File.createTempFile("~student", ".tmp", userDir);
tempFile.deleteOnExit();
RandomAccessFile raf=new RandomAccessFile(tempFile, "rw");
s1.write(raf);
s2.write(raf);
s3.write(raf);
raf.seek(0);
Student s=new Student();
for(long i=0;i<raf.length();i=raf.getFilePointer())
{
s.read(raf);
System.out.println("[lenth:"+raf.length()+",i="+i+"] " + s);
}
raf.close();
Thread.sleep(5000);
}
}
class Student
{
int num;
String name;
double score;
public Student()
{
}
public Student(int num, String name, double score)
{
this.num=num;
this.name=name;
this.score=score;
}
public void write(RandomAccessFile raf) throws IOException
{
raf.writeInt(num);
raf.writeUTF(name);
raf.writeDouble(score);
}
public void read(RandomAccessFile raf) throws IOException
{
num=raf.readInt();
name=raf.readUTF();
score=raf.readDouble();
}
public String toString
相关问答:
我在eclipse中写了一个播放音乐的类,并把音乐文件和类文件放在了一起,结果运行时,出现错误,说是音乐文件那个是空的,但是在jcreator用同样的方法结果是可以运行的,请求高说指教,告诉为什么?急
你若是在Windo ......
import java.io.*;
class FileTest
{
public static void main(String [] args) throws Exception
{
File fDir=new File(File.separator);
String strFile="java源代码测试"+File.separato ......
我的java applet 嵌入html后无法显示,而用appletviewer显示正常为什么,还有就是用html converter转化后可以显示,为什么?请大侠赐教!
HTML code:
<HTML>
<HEAD>
<TITLE>TEST.HTML< ......
当知道一个日期,如何把这个日期所在旬的前后两个日期取出
比如输入20090905,就输入20090901 20090910
输出20090901 20090910
取得天数后作一下判断再把这个方法用下void set(int field, int value)
......
我现在想知道一个不常用的java关键字,
不是volatile,transate;
有点象LocalThread类,但又不是,是个完全小写的关键字,好象是local...啥的;
原来见过几回;今天怎么也找不到了;
Locale
Locator
引用
Locale
L ......