Topic: 一道RandomAccessFile类的问题

  Print this page

1.一道RandomAccessFile类的问题 Copy to clipboard
Posted by: Adolfmark
Posted on: 2008-11-19 16:14

如何实现用randomaccessfile类读出一个文件的后5行的数据

我的代码如下:

import java.io.*;
public class RandomAccessFilelast {
  
  public static void main(String[] args) {
  try{
      RandomAccessFile in = new RandomAccessFile("test.java","rw");
  
        for(long i=in.length();i>=in.length()-5;i--){
          in.seek(in.length());
          System.out.println(in.readLine());
        }
    
  }
  catch(IOException e){}
  }  
}


本人初学java不久,这个程序写了半天也没有实际效果,不知道该如何下手了,特向群里的朋友们请教,谢谢

2.Re:一道RandomAccessFile类的问题 [Re: Adolfmark] Copy to clipboard
Posted by: micsolaris
Posted on: 2008-11-19 20:37

package com.ricky.www;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Scanner;

public class Test {
  public static void main(String[] args) throws IOException {
    RandomAccessFile ioFile = new RandomAccessFile("c:/family/ricky.txt",
        "rw");

    FileDescriptor descriptor = ioFile.getFD();
    FileInputStream inFile = new FileInputStream(descriptor);
    Scanner scanner = new Scanner(inFile);
    HashMap<Integer, String> map = new HashMap<Integer, String>();
    int line = 0;
    while (scanner.hasNextLine()) {
      map.put(new Integer(++line), scanner.nextLine());
    }

    for (int i = line - 4; i <= line; i++) {
      System.out.println(map.get(new Integer(i)));
    }
  }
}


用这个看看。但是虽然说涉及到RandomAccessFile,其实用得还是FileInputStream,但能实现你要的功能。

3.Re:一道RandomAccessFile类的问题 [Re: Adolfmark] Copy to clipboard
Posted by: Adolfmark
Posted on: 2008-11-20 18:28

我写了两天了,还没用RandomAccessFile实现这个效果,估计光用这个类是无法实现了,也不知道那个出题的教授怎么想的 也不给个答案 郁闷

4.Re:一道RandomAccessFile类的问题 [Re: Adolfmark] Copy to clipboard
Posted by: Adolfmark
Posted on: 2008-11-20 18:46

谢谢你的回答

5.Re:一道RandomAccessFile类的问题 [Re: Adolfmark] Copy to clipboard
Posted by: Adolfmark
Posted on: 2008-11-20 20:35

搞定了 感觉奇士 不然我肯定是弄不出来的

import java.io.*;
public class rdf {
  
  public static void main(String[] args) {
      try{
      RandomAccessFile in = new RandomAccessFile("Example.java","rw");
int i=0;
int rows=0;
//--------得到文件的行数-----
in.seek(0);
while(in.getFilePointer()<in.length()){
in.readLine();
rows++;
}
//--------定位到倒数第5行-----
rows-=5;
in.seek(0);
for(i=0;i<rows;i++)
in.readLine();
//--------输出最后5行-----
for(i=0;i<5;i++)
System.out.println(in.readLine());
}
  catch(IOException e){}

    
  }
}

6.Re:一道RandomAccessFile类的问题 [Re: Adolfmark] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-11-22 00:50

I really appreciate that you have completed yourself with your question.

Three minor things with your code:
1. Class name begins with a capital letter by convention. And it should not be incomprehensible.
2. catch(IOException e){}
It is usually a bad idea, not to catch exception.
3. Give comments to your program.

Jiafan


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923