File rückwärts auslesen?

downset04

Erfahrenes Mitglied
Hallo

Wie kann ich ein File rückwärts auslesen?

vorwärts gehts so:
Code:
	file.seek(0);		
	while(file.getFilePointer()<file.length()){ 
		      String text=file.readUTF();
		      		System.out.println(text);
		    }

thx
 
Hi!

Setz die Pointerposition ans Ende des Files, also Ende - 1, und benutze einfach eine for-Schleife, die anstatt ++ -- ausführt.

Code:
for (int i = fileend-1 ; i != filestart; i--) {
  // what ever you want ... :)
}

Vielleicht hilft das ja :)

Gruß
TOM
 
Hi,

schau mal hier:


Code:
package de;

import java.io.*;
import java.util.*;

public class ReverseFileReader {       
              private String filename;       
              private RandomAccessFile randomfile;       
              private long position;
              
              public ReverseFileReader (String filename) throws Exception {              
                     // Open up a random access file
                     this.randomfile=new RandomAccessFile(filename,"r");
                     // Set our seek position to the end of the file
                     this.position=this.randomfile.length();
                            
                     // Seek to the end of the file
                     this.randomfile.seek(this.position);
                     //Move our pointer to the first valid position at the end of the file.
                     String thisLine=this.randomfile.readLine();
                     while(thisLine == null ) {
                            this.position--;
                            this.randomfile.seek(this.position);
                            thisLine=this.randomfile.readLine();
                            this.randomfile.seek(this.position);
                     }
              }       
              
              // Read one line from the current position towards the beginning
              public String readLine() throws Exception {              
                     int thisCode;
                     char thisChar;
                     String finalLine="";
                     
                     // If our position is less than zero already, we are at the beginning
                     // with nothing to return.
                     if ( this.position < 0 ) {
                                   return null;
                     }
                     
                     for(;;) {
                            // we've reached the beginning of the file
                            if ( this.position < 0 ) {
                                   break;
                            }
                            // Seek to the current position
                            this.randomfile.seek(this.position);
                            
                            // Read the data at this position
                            thisCode=this.randomfile.readByte();
                            thisChar=(char)thisCode;
                            
                            // If this is a line break or carrige return, stop looking
                            if (thisCode == 13 || thisCode == 10 ) {
                                   // See if the previous character is also a line break character.
                                   // this accounts for crlf combinations
                                   this.randomfile.seek(this.position-1);
                                   int nextCode=this.randomfile.readByte();
                                   if ( (thisCode == 10 && nextCode == 13) || (thisCode == 13 && nextCode == 10) ) {
                                          // If we found another linebreak character, ignore it
                                          this.position=this.position-1;
                                   }
                                   // Move the pointer for the next readline
                                   this.position--;
                                   break;
                            } else {
                                   // This is a valid character append to the string
                                   finalLine=thisChar + finalLine;
                            }
                            // Move to the next char
                            this.position--;
                     }
                     // return the line
                     return finalLine;
              }       
}
 
Hallo!

Schau mal hier:
Code:
  /**
   * 
   */
  package de.tutorials;
  
  import java.io.File;
  import java.io.RandomAccessFile;
  
  /**
   * @author daritho
   * 
   */
  public class FileReverseExample {
  
  	/**
  	 * @param args
  	 */
  	public static void main(String[] args) throws Exception {
  		File file = new File("d:/reverseExample.txt");
  		RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
  
 		for (long currentPosition = file.length() - 1; currentPosition >= 0; currentPosition--) {
  			randomAccessFile.seek(currentPosition);
  			System.out.print((char) randomAccessFile.read());
  		}
  
  		randomAccessFile.close();
  	}
  }

reverseExample.txt:
Code:
 abc
 def
 ghi

Gruss Tom
 
Zurück