Reverse a file with RandomAccessFile : RandomAccessFile « File Input Output « Java






Reverse a file with RandomAccessFile

  


import java.io.RandomAccessFile;

class ReverseFile {

  public static void main(String args[]) throws Exception {
    RandomAccessFile raf = new RandomAccessFile(args[0], "r");
    long position = raf.length();
    while (position > 0) {
      position -= 1;
      raf.seek(position);
      byte b = raf.readByte();
      System.out.print((char) b);
    }
  }
}

   
    
  








Related examples in the same category

1.Random IO
2.Using the RandomAccessFile class
3.The RandomAccessFile Class
4.Use RandomAccessFile to reverse a file
5.Using a Random Access File
6.A RandomAccessFile object that is tied to a file called employee.dat.
7.Reading UTF-8 Encoded Data
8.Use RandomAccessFile class
9.Appending data to existing file
10.The class demonstrates the use of java.io.RandomAccessFile
11.Readonly RandomAccessFile
12.Translate Charset
13.Use RandomAccessFile to save an object
14.Read from back
15.Using RandomAccessFile to read file saved DataOutputStreamUsing RandomAccessFile to read file saved DataOutputStream