0

I've made two apps designed to run concurrently (I do not want to combine them), and one reads from a certain file and the other writes to it. When one or the other are running no errors, however if they are both running a get an access is denied error. Relevant code of the first:

    class MakeImage implements Runnable {
        @Override
        public void run() {
            File file = new File("C:/Users/jeremy/Desktop/New folder (3)/test.png");
            while (true) {


                try{
//make image
                if(image!=null)
                {
                    file.createNewFile();
                ImageIO.write(image, "png", file);
                hello.repaint();}}
                catch(Exception e)
                {
                    e.printStackTrace();
                }

                }
    }
}   

Relevant code of the second:

            BufferedImage image = null;
            try {
                // Read from a file
                image = ImageIO.read(new File("C:/Users/jeremy/Desktop/New folder (3)/test.png"));
            }
            catch(Exception e){
                e.printStackTrace();
            }
            if(image!=null)
            {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write( image, "png", baos );
            baos.flush();
            byte[] imageInByte = baos.toByteArray();
            baos.close();
            returns=Base64.encodeBase64String(imageInByte);
            }

I looked at this: Java: how to handle two process trying to modify the same file, but that is when both are writting to the file where here only one is. I tried the retry later method as suggested in the former's answer without any luck. Any help would be greatly appreciated.

Community
  • 1
  • 1
jersam515
  • 657
  • 6
  • 22

3 Answers3

3

Unless you use OS level file locking of some sort and check for the locks you're not going to be able to reliably do this very easily. A fairly reliable way to manage this would be to use another file in the directory as a semaphore, "touch" it when you're writing or reading and remove it when you're done. Check for the existence of the semaphore before accessing the file. Otherwise you will need to use a database of some sort to store the file lock (guaranteed consistency) and check for it there.

That said, you really should just combine this into 1 program.

Rick Mangi
  • 3,761
  • 1
  • 14
  • 17
0

Use synchronized on the method that modify the file.

Edited:

As per the Defination of a Thread safe class, its this way.. " A class is said to be thread safe, which it works correctly in the presence of the underlying OS interleaving and scheduling with NO means of synchronization mechanism from the client side".

I believe there is a File which is to be accessed on to a different machine, so there must be some client-server mechanism, if its there.. then Let the Server side have the synchronization mechanism, and then it doesnt matters how many client access it...

If not, synchronized is more than enough........

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

Try RandomAccessFile.

This is a useful but very dangerous feature. It goes like this "if you create different instances of RandomAccessFile for a same file you can concurrently write to the different parts of the file."
You can create multiple threads pointing to different parts of the file using seek method and multiple threads can update the file at the same time. Seek allow you to move to any part of the file even if it doesn't exist (after EOF), hence you can move to any location in the newly created file and write bytes on that location. You can open multiple instances of the same file and seek to different locations and write to multiple locations at the same time.

elias
  • 15,010
  • 4
  • 40
  • 65