Tags down


Using RandomAccessFile along with BufferedReader to speed up file read

By : a14325
Date : October 16 2020, 06:10 PM
With these it helps The reason for the observed behavior is that, as the name suggests, the BufferedReader is buffered. It reads a larger chunk of data at once (into a buffer), and returns only the relevant parts of the buffer contents - namely, the part up to the next \n line separator.
I think there are, broadly speaking, two possible approaches:
code :
byte buffer[] = new byte[8192];
// In a loop:
int read = randomAccessFile.read(buffer);
// Figure out where a line break `\n` appears in the buffer,
// return the resulting lines, and take the position of the `\n`
// into account when storing the "file pointer"
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class LargeFileRead {
    public static void main(String[] args) throws Exception {

        String fileName = "myBigFile.txt";

        long before = System.nanoTime();
        List<String> result = readBuffered(fileName);
        //List<String> result = readDefault(fileName);
        long after = System.nanoTime();
        double ms = (after - before) / 1e6;
        System.out.println("Reading took " + ms + "ms "
                + "for " + result.size() + " lines");

    private static List<String> readBuffered(String fileName) throws Exception {
        List<String> lines = new ArrayList<String>();
        RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "r");
        BufferedReader brRafReader = new BufferedReader(
                new FileReader(randomAccessFile.getFD()));
        String line = null;
        long currentOffset = 0;
        long previousOffset = -1;
        while ((line = brRafReader.readLine()) != null) {
            long fileOffset = randomAccessFile.getFilePointer();
            if (fileOffset != previousOffset) {
                if (previousOffset != -1) {
                    currentOffset = previousOffset;
                previousOffset = fileOffset;
            int bufferOffset = getOffset(brRafReader);
            long realPosition = currentOffset + bufferOffset;
            System.out.println("Position : " + realPosition 
                    + " with FP " + randomAccessFile.getFilePointer()
                    + " and offset " + bufferOffset);
        return lines;

    private static int getOffset(BufferedReader bufferedReader) throws Exception {
        Field field = BufferedReader.class.getDeclaredField("nextChar");
        int result = 0;
        try {
            result = (Integer) field.get(bufferedReader);
        } finally {
        return result;

    private static List<String> readDefault(String fileName) throws Exception {
        List<String> lines = new ArrayList<String>();
        RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "r");
        String line = null;
        while ((line = randomAccessFile.readLine()) != null) {
            System.out.println("Position : " + randomAccessFile.getFilePointer());
        return lines;

Share : facebook icon twitter icon

BufferedReader vs. RandomAccessFile in java

By : Mohammad Soltani Del
Date : March 29 2020, 07:55 AM
I hope this helps you . RandomAccessFile.readLine() might be slightly faster because it ignores character encoding. However it doesn't use buffering and still use StringBuffer :P so it could be slower on your system.
BufferedReader.readLine() is preferred because it handles character encoding e.g. UTF-8 or Windows-1252.

How the RandomAccessFile class returns bytes with randomAccessFile.read() method;

By : amit
Date : March 29 2020, 07:55 AM
Any of those help You should use readFully
code :
    try (RandomAccessFile raf = new RandomAccessFile("filename", "r")) {
        byte[] document = new byte[(int) raf.length()];
class Test {
    public static void main(String args[]) {
        // here is where chars will be stored. If printed now, will show random junk
        char[] buffer = new char[5];

        // call our method. It does not "return" data.
        // It puts data into an array we already created.

        // prints "hello", even though hello was never "returned"

    static void putCharsInMyBuffer(char[] buffer) {
        buffer[0] = 'h';
        buffer[1] = 'e';
        buffer[2] = 'l';
        buffer[3] = 'l';
        buffer[4] = 'o';

Does RandomAccessFile.read() from local file guarantee that exact number of bytes will be read?

By : user3266072
Date : March 29 2020, 07:55 AM
help you fix your problem The read method does not guarantee to read all requested bytes (even if they exist in the file). The actual behavior is going to be dependent on the underlying OS and file system. For example, when backed by NFS, you may be more likely to not get all the requested bytes in a single call.
If you want to guarantee to get all the requested bytes in a single call, you must use readFully.

Android speed BufferedReader read line and ArrayList add and get

By : Shyam Kumar Emandi
Date : March 29 2020, 07:55 AM
wish of those help BufferedReader and ArrayList are not the problems.
Your performance problems basically come down to two issues: allocation and parsing.
code :
    String[] tokens = line.split("[ ]+");
    if (tokens[1].matches("[0-9]+")) {// f: v
    Face face = new Face(vertices.get(Integer.parseInt(tokens[1]) - 1), vertices.get(Integer.parseInt(tokens[2]) - 1), vertices.get(Integer.parseInt(tokens[1]) - 1));
    if (normals.size() > 0) {
        face.setAn(normals.get(Integer.parseInt(tokens[1]) - 1));
        face.setBn(normals.get(Integer.parseInt(tokens[2]) - 1));
        face.setCn(normals.get(Integer.parseInt(tokens[3]) - 1));

RandomAccessFile to read files in a Jar file

By : Nicholas Chan
Date : March 29 2020, 07:55 AM
like below fixes the issue The key information you seem to be missing is that Jar files are compressed, and you can't "seek" because of the compression (which is I believe the DEFLATE algorithm).
However, you could extract the file(s) into temp file(s) on start and then use that. Temp files would be removed on application exit, and are the best answer I can think of.
Related Posts Related Posts :
  • How to flip two words in a sentence in java
  • Maven - How to remove module-info.class warning for shaded .jar?
  • Project Reactor, using a Flux sink outside of the creation lambda
  • Count the number of Arrays & access a specific array in the JSON Response using REST ASSURED with JAVA & Seleniu
  • Rotating zombies to face player
  • How to distribute the same apk for regular and instant app
  • How do I find one String inside of another
  • How to instantiate interface in Kotlin?
  • Java Intern pool implementation creates too many temporary objects
  • I need some advide about user type with hashmap
  • Is there a way to split for loop in java to do different things for values?
  • Thread safety on block of code which has external reference
  • I am unable to install java8 on my ubuntu gcp machine
  • Function returning blank string
  • Class Random Java: The method "roll()" is undefined for type Random. Why?
  • How to return value from the function without exiting from the function?
  • Getting wrong hours when Convert date without time to Milliseconds using SimpleDateFormat
  • Why we need Action Interface when all actions can be done with ActionsClass
  • How can I make this program run when it says could not find or load main class?
  • How to avoid duplicate node?
  • Everytime i run this recrusion algorithm, my variable is set to 0 how to solve it
  • Determine Whether JLabel Text is Being Cut Off?
  • Why is TomEE not able to create JMS connection factory resource?
  • Unable to sendKeys in an EditText field
  • Merge sort implementation in java is copying a value into another index instead of swapping
  • Should method parameters be made as final in Java8
  • AEM OSGI 3rd party dependency, bundle not Active
  • Using mybatis to map multiple tables to a single collection
  • Is it possible to pass text from edittext to webview?
  • Problems with repeat use of while loop
  • Change border color on click Java
  • Cannot find any information on property [bankOffer] in a bean of type [game.Game]
  • How to check if a button is clicked in another Jframe
  • IDEA Gives warning 'some variable always false'?
  • Using methods of classes that its didn't extends the same parent class
  • How do I set up a build-script with Maven?
  • How do I add arrays to an Arraylist of Arrays (Arraylist<Integer[]>)
  • Trying to remove a tuple using a prepared statement
  • Best/recommended way to convert Java 8 OffsetDateTime to localized string on android
  • Print a String n times using recursion
  • I have a Jtable with a button in one of the columns and I want to access the value in the 1st column when I press the bu
  • Generating source code with a gradle task based on existing classes with annotations on them
  • a subclass for specifying a generic shows bound mismatch
  • Static Variable with null reference
  • painting string in the middle of a given size cell java
  • Displaying column in tableView
  • What does <+ mean in Java?
  • What cplex function I have to use to write this variable?
  • Java ConcurrentHashSet - iterating over it in a multi-threaded environment
  • Can't seem to get my Java to work in VS Code
  • I don't understand the function of "count++"
  • What are the possibilities to design an API that needs overloads depending on a generic type?
  • Can't connect to Apache Ignite Cluster on AWS EC2 - Ignite server rejects clients
  • Trying to understand garbage collection in java
  • Is there a way to associate an ArrayList with another ArrayList with a one to many relationship?
  • Generate List with Combination of Subset of List, Java
  • Getting Illegal State Exception in Regex
  • Shuffling an Array (Java)
  • Tomcat v 9.0 - Public Key Retrieval is not allowed
  • Clarification on why this code is resulting in false and not true
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk