Home > Java > javaTutorial > Is `BufferedWriter` the Fastest Way to Write Large Data to a Text File in Java?

Is `BufferedWriter` the Fastest Way to Write Large Data to a Text File in Java?

Susan Sarandon
Release: 2024-11-09 19:12:02
Original
802 people have browsed it

Is `BufferedWriter` the Fastest Way to Write Large Data to a Text File in Java?

Fastest Method for Writing Extensive Data to Text Files in Java

Introduction:

Writing large volumes of data to text files can be a crucial task in various programming scenarios. In Java, the BufferedWriter class is commonly employed for this purpose. However, it's important to consider the efficiency of the operation, especially when dealing with massive datasets.

Question:

Can BufferedWriter offer the optimal speed for writing large data to a text file? Can Java provide more efficient alternatives?

Response:

While BufferedWriter is a widely used method for file writing, it may not always be the fastest solution for very large data. To optimize performance, bypassing BufferedWriter and directly using the FileWriter might be beneficial. Modern systems often store data in cache memory, making the BufferedWriter's buffering mechanism redundant.

Results:

Empirical testing demonstrates that direct FileWriter usage can significantly improve write speeds. A benchmark test involving the writing of 175MB of data (4 million strings) using FileWriter achieved a time range of 4-5 seconds, significantly faster than using BufferedWriter.

Additional Considerations:

Isolating the time components for record retrieval and file writing can provide valuable insights into the efficiency of the overall process.

Example Code:

The following code snippet illustrates a performance test that compares writing raw data with writing using BufferedWriters with different buffer sizes:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class FileWritingPerfTest {

    private static final int ITERATIONS = 5;
    private static final double MEG = (Math.pow(1024, 2));
    private static final int RECORD_COUNT = 4000000;
    private static final String RECORD = "Help I am trapped in a fortune cookie factory\n";
    private static final int RECSIZE = RECORD.getBytes().length;

    public static void main(String[] args) throws Exception {
        List<String> records = new ArrayList<>(RECORD_COUNT);
        int size = 0;
        for (int i = 0; i < RECORD_COUNT; i++) {
            records.add(RECORD);
            size += RECSIZE;
        }
        System.out.println(records.size() + " 'records'");
        System.out.println(size / MEG + " MB");

        for (int i = 0; i < ITERATIONS; i++) {
            System.out.println("\nIteration " + i);

            writeRaw(records);
            writeBuffered(records, 8192);
            writeBuffered(records, (int) MEG);
            writeBuffered(records, 4 * (int) MEG);
        }
    }

    private static void writeRaw(List<String> records) throws IOException {
        File file = File.createTempFile("foo", ".txt");
        try {
            FileWriter writer = new FileWriter(file);
            System.out.print("Writing raw... ");
            write(records, writer);
        } finally {
            // comment this out if you want to inspect the files afterward
            file.delete();
        }
    }

    private static void writeBuffered(List<String> records, int bufSize) throws IOException {
        File file = File.createTempFile("foo", ".txt");
        try {
            FileWriter writer = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(writer, bufSize);

            System.out.print("Writing buffered (buffer size: " + bufSize + ")... ");
            write(records, bufferedWriter);
        } finally {
            // comment this out if you want to inspect the files afterward
            file.delete();
        }
    }

    private static void write(List<String> records, Writer writer) throws IOException {
        long start = System.currentTimeMillis();
        for (String record : records) {
            writer.write(record);
        }
        // writer.flush(); // close() should take care of this
        writer.close();
        long end = System.currentTimeMillis();
        System.out.println((end - start) / 1000f + " seconds");
    }
}
Copy after login

The above is the detailed content of Is `BufferedWriter` the Fastest Way to Write Large Data to a Text File in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template