How to perform read and write operation by using thread synchronization.
Condition: If one file exists where writers may write information to, only one writer may write at a time. Confusion may arise if a reader is trying read at the same as a writer is writing. Since readers only look at the data, but do not modify the data, we can allow more than one reader to read at the same time.
//reader thread
    class Read extends Thread {
        static File Reader fr1 = null;
        static Buffered Reader br1 = null;
        static synchronized void reader() throws IO Exception {
            String path ="C:/Users/teja/Documents/file1.txt";
            fr1 = new File Reader(path);
            br1 = new Buffered Reader(fr);
            int i;
            while ((i = br. read()) != -1)
                System .out. print((char) i);
            System .out. print ln();
        }
        public void run() {
            try {
                reader();
            } catch (IO Exception e) {
                e. print Stack Trace();
            }
        }
    }
    //writer code
    class Writer extends Thread {
        static Buffered Writer bw1 = null;
        static File Writer fw1 = null;
        static synchronized void writer() throws IO Exception {
            Scanner scanner = new Scanner(System.in);
            System .out .print ln("enter data to be added:");
            String data = scanner. nextLine();
            String path = "C:/Users/vt/Documents/file1.txt";
            fw1 = new File Writer(path, true);
            bw1 = new Buffered Writer(fw1);
            bw1.newLine();
            bw1.write(data);
            bw1.flush();
            scanner. close();
            System. out . println("data added");
        }
        public void run() {
            try {
                writer();
            } catch (IO Exception e) {
                e. print Stack Trace();
            }
        }
    }
    //main method
    public class File Read Write {
        public static void main(String[] args) {
           Read rd1 =new Read();
           Read rd2=new Read();
           Writer wt1=new Writer();
           rd1.run();
           rd2.run();
           wt1.run();
           rd1. run();
            }           
    }
I am new to files and threading in java. I know this is not correct approach. Guide me.
If one file exists where writers may write information to, only one writer may write at a time. Confusion may arise if a reader is trying read at the same as a writer is writing. Since readers only look at the data, but do not modify the data, we can allow more than one reader to read at the same time.
 
     
     
    