Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read and write files in java using separate threads

I have created two threads and modified the run function so that one thread reads one line and the other writes the same line to the new file. This happens till the whole file is copied. The problem i am getting is that even though i have used variables to control that the threads execute one by one but still the threads are executing unevenly i.e one thread executes multiple times and then the control transfers. Any solutions i have attached the code. I am new to java as it is only for class assignment so the code might not be the most optimized.

  public class thread1 extends Thread {
    //To create producer and consumer as threads
        //Shared variable
        public static int x = 0;//checks if all lines are read                                      
        public static String line; /holds lines from file
        public static int j = 0;//variable to switch between threads based upon its value

        public thread1(String threadName) {     //Constuctor
            super(threadName);      //Call to constructor of Thread class
        }

        public void run() {

            while (x != -1)
            {
                if (Thread.currentThread().getName().contains("Reader")) {
                    if (x != -1&&j==0)
                    {
                   j=1;
                    String fileName = "d:/salfar.txt";


                    try {
                        // FileReader reads text files in the default encoding.
                        FileReader fileReader =
                                new FileReader(fileName);

                        // Always wrap FileReader in BufferedReader.
                        BufferedReader bufferedReader =
                                new BufferedReader(fileReader);

                        for (int check = 0; check <= x; check++) {

                            line = bufferedReader.readLine();
                        }
                        if (line == null) {
                            x = -1;
                        } else {
                            System.out.println(line);

                            x++;
                        }


                        // Always close files.
                        bufferedReader.close();
                    } catch (FileNotFoundException ex) {
                        System.out.println(
                                "Unable to open file '"
                                + fileName + "'");
                    } catch (IOException ex) {
                        System.out.println(
                                "Error reading file '"
                                + fileName + "'");
                        // Or we could just do this: 
                        // ex.printStackTrace();

                    }
                    }

                    yield();
                } 
                else if (Thread.currentThread().getName().contains("writer")) {
    if (x != -1 && line != null&&j==1)
    {
                    j=0;

                    String fileName = "d:/salfar1.txt";

                    try {
                        // Assume default encoding.
                        FileWriter fileWriter =
                                new FileWriter(fileName, true);

                        // Always wrap FileWriter in BufferedWriter.
                        BufferedWriter bufferedWriter =
                                new BufferedWriter(fileWriter);

                        // Note that write() does not automatically
                        // append a newline character.
                        bufferedWriter.write(line);
                        bufferedWriter.newLine();
                        System.out.println("y");
                        // Always close files.
                        bufferedWriter.close();
                    } catch (IOException ex) {
                        System.out.println(
                                "Error writing to file '"
                                + fileName + "'");
                        // Or we could just do this:
                        // ex.printStackTrace();
                    }
    }     
                    Thread.yield();
                }
                else{}
            }
        }

        public static void main(String[] args) {

            thread1 p = new thread1("Reader");
            thread1 c = new thread1("writer");

            p.start();
            c.start();


        }
    }


    Thanks
like image 641
Salman Farooq Avatar asked Mar 30 '15 06:03

Salman Farooq


People also ask

Can a file be read by multiple threads in Java?

Multiple threads can read and write the same file in several situations: Multiple threads read the same file at the same time. In this case, there is no conflict.

Can two threads write to the same file?

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.


1 Answers

You cannot control the order of thread execution. However, to perform read and write operation via separate threads, you should use BlockingQueue which has the following properties:

A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element.

ReaderThread will read from the input file.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;

public class ReaderThread implements Runnable{

  protected BlockingQueue<String> blockingQueue = null;

  public ReaderThread(BlockingQueue<String> blockingQueue){
    this.blockingQueue = blockingQueue;     
  }

  @Override
  public void run() {
    BufferedReader br = null;
     try {
            br = new BufferedReader(new FileReader(new File("./inputFile.txt")));
            String buffer =null;
            while((buffer=br.readLine())!=null){
                blockingQueue.put(buffer);
            }
            blockingQueue.put("EOF");  //When end of file has been reached

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } catch(InterruptedException e){

        }finally{
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


  }



}

WriterThread will write to output file.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.concurrent.BlockingQueue;

public class WriterThread implements Runnable{

  protected BlockingQueue<String> blockingQueue = null;

  public WriterThread(BlockingQueue<String> blockingQueue){
    this.blockingQueue = blockingQueue;     
  }

  @Override
  public void run() {
    PrintWriter writer = null;

    try {
        writer = new PrintWriter(new File("outputFile.txt"));

        while(true){
            String buffer = blockingQueue.take();
            //Check whether end of file has been reached
            if(buffer.equals("EOF")){ 
                break;
            }
            writer.println(buffer);
        }               


    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch(InterruptedException e){

    }finally{
        writer.close();
    } 

  }

}

From Launcher class start your multithreaded read and write.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Launcher {

  public static void main(String[] args) {

    BlockingQueue<String> queue = new ArrayBlockingQueue<String>(1024);

    ReaderThread reader = new ReaderThread(queue);
    WriterThread writer = new WriterThread(queue);

    new Thread(reader).start();
    new Thread(writer).start();

  }

 }
like image 146
Touchstone Avatar answered Sep 23 '22 15:09

Touchstone