# File rückwärts auslesen?



## downset04 (2. November 2005)

Hallo

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

vorwärts gehts so:

```
file.seek(0);		
	while(file.getFilePointer()<file.length()){ 
		      String text=file.readUTF();
		      		System.out.println(text);
		    }
```

thx


----------



## TommyMo (2. November 2005)

Hi!

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


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

Vielleicht hilft das ja 

Gruß
TOM


----------



## OliverT (2. November 2005)

Hi,

schau mal hier:



```
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;
              }       
}
```


----------



## Thomas Darimont (2. November 2005)

Hallo!

  Schau mal hier:

```
/**
   * 
   */
  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:

```
abc
 def
 ghi
```
 
  Gruss Tom


----------

