PingAnTest1

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();
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章