Skip to content

Instantly share code, notes, and snippets.

@kiichi
Created July 6, 2019 15:06
Show Gist options
  • Select an option

  • Save kiichi/b8a3467909c2fe7c110e5ac1b857f7f8 to your computer and use it in GitHub Desktop.

Select an option

Save kiichi/b8a3467909c2fe7c110e5ac1b857f7f8 to your computer and use it in GitHub Desktop.
Java IO Seek Example
import java.io.File;
import java.io.RandomAccessFile;
public class MyFileReader {
public static void main(String[] args) throws Exception {
File file = new File("bigfile.txt");
RandomAccessFile raf = new RandomAccessFile(file, "r");
long maxSize = 500000;
if(file.exists()){
long size = file.length();
System.out.println(size);
if(size > maxSize) {
long seekPoint = size - maxSize;
raf.seek(seekPoint);
System.out.println("Seeking : " + seekPoint);
String line = "";
int count = 0;
while( null != (line = raf.readLine()) ){
System.out.println("Line: " + count + " " + line);
count++;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment