Created
July 6, 2019 15:06
-
-
Save kiichi/b8a3467909c2fe7c110e5ac1b857f7f8 to your computer and use it in GitHub Desktop.
Java IO Seek Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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