知识大全 JAVA如何实现从最后一行读取文件

Posted 文件

篇首语:知识能使你增加一双眼睛。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 JAVA如何实现从最后一行读取文件相关的知识,希望对你有一定的参考价值。

JAVA如何实现从最后一行读取文件  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  JAVA如何实现从最后一行读取文件

  import java io FileNotFoundException;

  import java io IOException;

  import java io RandomAccessFile;

  public class FromEndRF

  public static void read(String filename)

  read(filename GBK )

  

  public static void read(String filename String charset)

  RandomAccessFile rf = null;

  try

  rf = new RandomAccessFile(filename r )

  long len = rf length()

  long start = rf getFilePointer()

  long nextend = start + len ;

  String line;

  rf seek(nextend)

  int c = ;

  while (nextend > start)

  c = rf read()

  if (c == \\n || c == \\r )

  line = rf readLine()

  if (line != null)

  System out println(new String(line getBytes( ISO ) charset))

  else

  System out println(line) // 输出为null 可以注释掉

  

  nextend ;

  

  nextend ;

  rf seek(nextend)

  if (nextend == ) // 当文件指针退至文件开始处 输出第一行

  System out println(new String(rf readLine() getBytes( ISO ) charset))

  

  

   catch (FileNotFoundException e)

  e printStackTrace()

   catch (IOException e)

  e printStackTrace()

   finally

  try

  if (rf != null)

  rf close()

   catch (IOException e)

  e printStackTrace()

  

  

  

  public static void main(String args[])

  read( d:\\\\ txt gbk )

  

cha138/Article/program/Java/hx/201311/26379

相关参考