`

RandomAccessFile读取文件

    博客分类:
  • jdk
阅读更多
package io;

import java.io.RandomAccessFile;

public class RandomAccessFileTest {

	public static void main(String[] args)throws Exception {
		printFileInReverseOrder();
	}
	
	
	private static void printFileInReverseOrder()throws Exception{
		RandomAccessFile randomAccessFile = new RandomAccessFile("D:/Projects/JDK/src/io/info.txt","r");
		
		long endIndex = randomAccessFile.length();
		
		while(endIndex>=0){
			randomAccessFile.seek(endIndex);
			
			char c = (char) randomAccessFile.read();
			
			if(c=='\n'){
				System.out.println(randomAccessFile.readLine());
			}
			if(endIndex==0){
				randomAccessFile.seek(0);
				System.out.println(randomAccessFile.readLine());
			}
			endIndex--;
		}
		randomAccessFile.close();
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics