Accessing Files

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing Files

bds81175
I have a macro program that I need to access a text or csv file that other PC's may be accessing and writing to.  Is there a way to either open it read-only or wait for the other user to finish and try again?
Reply | Threaded
Open this post in threaded view
|

Re: Accessing Files

Nathaniel Ryckman
That's not so much an imageJ question as it is a Java question. You should convert your macro into a plugin and then read these following articles/commentary/documents:

http://www.exampledepot.com/egs/java.nio/SetFileLock.html
http://stackoverflow.com/questions/128038/how-can-i-lock-a-file-using-java-if-possible
http://download.oracle.com/javase/6/docs/api/java/nio/channels/FileChannel.html#lock()

Oh! I suddenly had another idea that would take less work. Create a plugin or stand alone java program that would update the file shared file with the macro output. So, the pipeline would look like this:

Macro creates file1.txt -> Plugin updates file2.txt (shared file) using data in file1.txt.

Good luck!

bds81175 wrote
I have a macro program that I need to access a text or csv file that other PC's may be accessing and writing to.  Is there a way to either open it read-only or wait for the other user to finish and try again?
Reply | Threaded
Open this post in threaded view
|

Re: Accessing Files

bds81175
Don't you still have to open the shared file to update it with the data from your file1.txt?  I was looking at the isOpen function and it doesn't appear to do anything (can't get it to return a TRUE at all).  I'm not familiar enough with stuff to create separate plugins.  I'll start down that path but I would rather have something quick and dirty if I can.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing Files

Nathaniel Ryckman
Sorry. I didn't see that you had replied to my message.

isOpen("title") only checks to see if there is a window that is currently open with a specific title. It doesn't actually check the file to see if it's open.

Here, I found the direct code you will need to change the read only status of the file:
 
http://www.mkyong.com/java/how-to-make-a-file-read-only-in-java/


"File file = new File("c:/file.txt");
 
   
    if(file.canWrite()){
        //mark this file as read only, since jdk 1.2
    file.setReadOnly();
 

Your code here.....


    //revert the operation, mark this file as writable, since jdk 1.6
    file.setWritable(true);

    }else{
Do nothing.....
    }
 "

imageJ has a plugin editor. It can even attempt to convert your macro into a plugin.

Either way, you are going to need to create a plugin if you are interested in changing permissions on files. Either hire a programmer, get a programmer friend to convert your macro, or learn to write a plugin yourself.

bds81175 wrote
Don't you still have to open the shared file to update it with the data from your file1.txt?  I was looking at the isOpen function and it doesn't appear to do anything (can't get it to return a TRUE at all).  I'm not familiar enough with stuff to create separate plugins.  I'll start down that path but I would rather have something quick and dirty if I can.