Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - multiple threads writing to same file [duplicate]

I am trying to write some content to a file, through multiple threads in Java. Each thread reads a different input file, does some computation and writes some (different) content to the common output file. The problem is that in the end, the output file only contains the content written by the last terminating thread and not the content from the other threads. Relevant code for the threads -

public void run()
{
    try
    {
        File file = new File("/home/output.txt");
        if (!file.exists()) 
        {
             file.createNewFile();
        }
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);

        BufferedReader br = new BufferedReader(new FileReader(inputfile)); // each thread reads a different input file
        String line="";

        while((line=br.readLine())!=null)
        {
            String id = line.trim();               // fetch id

            StringBuffer sb = processId(userId);   // process id

            synchronized(this){
            bw.write(sb.toString() + "\n");        // write to file
            }
        }
        bw.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

How do i make all threads write their content to the common file ?

like image 753
Ankit Rustagi Avatar asked Oct 06 '13 10:10

Ankit Rustagi


People also ask

Can multiple threads write to the same file Java?

For your use case you would use a synchronized queue and modify each of your worker threads to add their log data to the master queue that the log thread uses for logging. You should NOT have multiple threads trying to write directly to the same file.

Can two threads write to the same file at the same time?

You can have multiple threads write to the same file - but one at a time. All threads will need to enter a synchronized block before writing to the file. In the P2P example - one way to implement it is to find the size of the file and create a empty file of that size.

What will happen if multiple threads accessing the same resource?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.

Can multiple threads use the same object?

Because all threads share the same heap, and the heap is where all instance variables are stored, multiple threads can attempt to use the same object's instance variables concurrently.


1 Answers

Use the constructor for FileWriter that uses append mode

FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
like image 71
Reimeus Avatar answered Sep 21 '22 09:09

Reimeus