Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
Java Open BufferedWriter only once but rewrite content many times

I am running a long running operation, Say 100k jobs. i want to update the progress of it in a file once every 100 such jobs are completed.

i am opening the file using bufferedWriter with append mode as false. Writing it and then closing it. this is done once every 100 jobs are completed. So the file open and close would have happened 1000 times. Can i optimise it further by opening and closing the file only once?

    public static void writeMetaData(String writeDir, JSONObject jsonObject) throws Exception {
        String filePath = writeDir.concat("/").concat("metadata.txt");
        BufferedWriter metaDataWriter = Files.newBufferedWriter(Paths.get(filePath), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING);
        metaDataWriter.write(jsonObject.toString());
        IOUtils.closeQuietly(metaDataWriter);
    }

for(int i =0 ; i < 100000; i++) {
    // do Something; 
    if(i % 100 == 0) {
        writeMetaData(writeDir, jsonObject); 
    }
}

File should only have a single line.

Expected file content after 100 jobs: progress: 100 Expected file content after 200 jobs: progress: 200

Can this be optimised further?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

A Stream does not allow to go back and rewrite content. A way to achieve what you want is using a RandomAccessFile. Its setLength() method will truncate the file if you pass 0.

Here is a simple example:

import java.io.*;

public class Test
{
    public static void updateFile(RandomAccessFile raf, String content) throws IOException
    {
        raf.setLength(0);
        raf.write(content.getBytes("UTF-8"));
    }

    public static void main(String[] args) throws IOException
    {
        try(RandomAccessFile raf = new RandomAccessFile("metadata.txt", "rw"))
        {
            updateFile(raf, "progress: 100");
            updateFile(raf, "progress: 200");
        }
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

File operations are typically buffered by the underlying kernel, and so you're unlikely to see much of a performance benefit by keeping an open file descriptor for this kind of low throughput application.

Keeping your code as a single operation that gets to leave no state after it finishes, rather than designing it as a continuous rewindable stream, makes for an elegant, simple, and unless you've specifically requested synchronous IO, then also sufficiently performant implementation that gets to benefit from the optimizations of all of the layers that sit beneath it.

When you do get measurable impedance to performance by this, which I suspect you never will, you could use the RandomAccessFile API, or go unnecessarily lower level by using FileChannel as others already specified.

I think you shouldn't compromise the simplicity/elegance of your design for this kind of micro-optimization, which in the grand scheme of things, is guaranteed to be insignificant (one tiny write operation per 100 jobs processed).

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda