Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda