package com.shengsiyuan.interview;
import java.io.IOException;
import java.io.RandomAccessFile;
public class PingAnTest3
{
public static void main(String[] args)
{
RandomAccessFile raf = null;
try
{
raf = new RandomAccessFile("d:/numbers.txt", "r");
System.out.println("文件指针的初始位置:" + raf.getFilePointer());
String str = null;
int count = 0;
while (null != (str = raf.readLine()))
{
count++;
}
for (int i = 0; i< 5; i++)
{
int randValue = (int)(Math.random() * ( 1 + count));
int pos = 0;
raf.seek(0);
while (null != (str = raf.readLine()))
{
if (++pos == randValue)
{
System.out.println("第" + randValue + "行数据:" + str);
break;
}
}
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
PingAnTest1
最新推荐文章于 2013-01-29 22:03:54 发布