以下是源程序代码:
import java.io.*;
public class TestRead {
public static void main(String[] args) {
int[]a={'g','o','o','d','b','y','e',' ','m','y','l','o','v','e'};
int i=0;
File f1=new File("F:/MyJava/RAF.txt");
try
{
RandomAccessFile f=new RandomAccessFile(f1,"rw");
while((i=f.read())!=-1)
{
System.out.print((char)i);
}
System.out.println();
System.out.println(f.getFilePointer());
System.out.println(f.length());
for(i=0;i<a.length;i++)
{
f.write(a[i]);
}
System.out.println(f.getFilePointer());
System.out.println(f.length());
while(f.read()!=-1)System.out.print((char)f.read());
System.out.println();
f.seek(0);
while(f.read()!=-1)System.out.print((char)f.read());
System.out.println(f.length());
}catch(Exception e)
{
System.out.println(e.toString());
}
}
}
F:/MyJava/RAF.txt的文件运行前的内容为:this is a RandomAccessFile example!
程序运行后文件的内容为: this is a RandomAccessFile example!goodbye mylove
程序运行结果为:
this is a RandomAccessFile example!
35
35
49
49
hsi admcesieeapegobemlv?49
Press any key to continue...
最后一行是什么意思牙,看不懂.
谁能告诉程序是怎么运行的? 还有就是read()函数不是读入的一个字节吗?但是你即使是读一个字节它好像是读的一个字符呀?这一点我不懂?