使用Java删除文件的最后一行

时间:2012-02-05 13:32:54

标签: java file-io text-processing

我有一个.txt文件,我想用Java处理它。我想删除它的最后一行。

我需要有关如何实现这一点的想法,而无需将整个内容复制到另一个文件中并忽略最后一行。有什么建议吗?

2 个答案:

答案 0 :(得分:9)

您可以通过扫描文件找到最后一行的开头,然后使用FileChannel.truncateRandomAccessFile.setLength截断它。

答案 1 :(得分:1)

通过使用RandomAccessFile,您可以:

  • 使用方法搜索(长)向前跳并读取这些行。但你不会确切地知道跳跃应该有多大。
  • 删除最后一行,你需要最后一行开头的位置,所以在读取每一行之前,存储他们的文件指针位置(方法 getFilePointer())。删除该位置即可使用 setLength(长)

代码将是这样的:

    LinkedList<String> lines=null;
    int howMuch = 1; // one line to read and delete
    try{
        RandomAccessFile raf = new RandomAccessFile(inputFileName, "rw");

        System.out.println("File Length="+raf.length());

        long step = 20;  // here I advice to write average length of line
        long jump = raf.length()<step*howMuch?
                0:
                    raf.length()-step*howMuch; 
        raf.seek(jump);
        lines = new LinkedList<String>();
        LinkedList<Long> pos = new LinkedList<Long>();

        Entry<LinkedList<String>,LinkedList<Long>> rLinesRead = getRemainingLines(raf,
                new AbstractMap.SimpleEntry<LinkedList<String>,LinkedList<Long>> (lines,pos));
        while(rLinesRead.getKey().size()<howMuch){
            if(jump<step)
                if(jump<=0)
                    break;
                else{
                    jump=0;
                    raf.seek(jump);
                    rLinesRead=getRemainingLines(raf,rLinesRead);
                    break;
                }
            else
                jump=jump-step;
            raf.seek(jump);
            rLinesRead=getRemainingLines(raf,rLinesRead);
        }
        int originalSize=rLinesRead.getKey().size(); 
        lines=rLinesRead.getKey();
        pos=rLinesRead.getValue();
        for (int i=0;i<originalSize-howMuch;++i){
            lines.removeFirst();
            pos.removeFirst();
        }
        if(!pos.isEmpty())
            raf.setLength(pos.getFirst()); // before last(from EOF) returned fully read lines in file          
    }catch(Exception ex){
        ex.printStackTrace();
    } finally{
        try {               raf.close();            } catch (Exception e) {             e.printStackTrace();            }
    }
//returns line to EOF with their begin file pointer positions
private Entry<LinkedList<String>,LinkedList<Long>> getRemainingLines(RandomAccessFile raf,
        Entry<LinkedList<String>,LinkedList<Long>> linesAlreadyLoadedFromEnd) throws IOException{
    LinkedList<String> pLines = linesAlreadyLoadedFromEnd.getKey();
    LinkedList<Long> pPos = linesAlreadyLoadedFromEnd.getValue();

    long init=raf.getFilePointer();
    String str = raf.readLine();
    if(pPos.size()>0?pPos.getFirst()==0:false || str==null)
        return linesAlreadyLoadedFromEnd;

    LinkedList<String> lines = new LinkedList<String>();
    LinkedList<Long> pos = new LinkedList<Long>();
    if(init==0L ){              
        lines.add(str);
        pos.add(0L);
    }
    Long tmpPos = raf.getFilePointer();
    while ((str = raf.readLine())!=null && !pPos.contains(tmpPos)){
        lines.add(str);
        pos.add(tmpPos);
        tmpPos = raf.getFilePointer();
    }
    pLines.addAll(0,lines);
    pPos.addAll(0,pos);
    return new AbstractMap.SimpleEntry<LinkedList<String>,LinkedList<Long>> (pLines,pPos);
}