Results 1 to 15 of 15
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default is this IOException problematic?

    My program runs fine but I keep getting this error message printed:
    Java Code:
    java.io.FileNotFoundException: C:\ozzypos\orders\2011.03.29_1.pos (Access is denied)
            at java.io.FileOutputStream.open(Native Method)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
            at ozzypos.IOWrite.writeNewOrders(IOWrite.java:41)
    Even though that happens every time i save a file, the files are still saved and the program continues to run fine. I was wondering why this is happening?

    I suspected that it is due to me locking the files, or because the folder already exists? but i don't really know so any help greatly appreciated.

    this is the relevent code:


    Java Code:
        public static String SAVE_DIRECTORY = "C:/ozzypos/";
    
        public static void writeNewOrders(Order order) {
            String filename = createDirectory("orders/") + String.format(Date.getFormattedDate(false)
                    + "_%d.pos",order.getOrderID());
            FileOutputStream fos = null;
            ObjectOutputStream out = null;
            try {
                fos = new FileOutputStream(filename); [COLOR="DarkGreen"]//Line 41[/COLOR]
                out = new ObjectOutputStream(fos);
                out.writeObject(order);
                out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        public static String createDirectory(String subdirectory) {
            String finalDirectory = SAVE_DIRECTORY + subdirectory;
            try {
                new File(finalDirectory).mkdirs();
                String cmd="attrib +h +s " + finalDirectory.concat("*") + " /S";
                Runtime.getRuntime().exec(cmd);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return finalDirectory;
        }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    It's difficult to believe that
    the files are still saved
    after an exception is thrown from line 41, because the execution should fall through to the catch block and lines 42 through 44 shouldn't be executed.

    Do you have a reason for setting the hidden and system attributes? Assuming you do, it may help to get a reference to the Process from Runtime.exec, and add a waitFor before returning from the createDirectory method.

    db

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    How do you know it's saving the file?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    hi db, tolls, thanks for the replies. it was difficult for me to believe too but i have confirmed this by deleting the folder and running the program again, and then even watching the folder (myself in Windows) while new files were created.

    i went into CMD.exe and ran this command:
    attrib -h -s "C:/ozzypos/*" /S /D

    and the files appeared.


    the reason for setting the files hidden and system, to hide and lock the files from users as i plan on loading them into the program automatically when the program is started. so to prevent the user from accidently moving or deleting those serialised object files, this was my quick fix if you like.

    the other reason is that only users with high-permissions may view those files.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    oh i forgot to mention that these files are very regularly saved/updated...........oh maybe thats it? once they are saved they can't be saved over because of the system +s attribute?

    i've tried to get around the problem by unlocking before writing, then relocking but i get the same problem even though unlocking/unhiding and locking/hiding works as illustrated in these pictures:

    Java Code:
            try {
                String unlock = "attrib -h -s " + filename;
                Runtime.getRuntime().exec(unlock);
                fos = new FileOutputStream(filename);
                out = new ObjectOutputStream(fos);
                out.writeObject(order);
                out.close();
                String lock = "attrib +h +s " + filename;
                Runtime.getRuntime().exec(lock);
            } catch (IOException ex) {
    1.

    2.

    3.

    4.
    Last edited by ozzyman; 03-29-2011 at 10:12 PM. Reason: added pictures

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    anyone have any ideas about this one?

    i changed the code to this:
    Java Code:
            } catch (IOException ex) {
                System.err.println("CAUSE: " + ex.getCause());
                System.err.println("MSG: " + ex.getMessage());
            }
    run:
    Taking Order 1
    CAUSE: null
    MSG: C:\ozzypos\orders\2011.03.29_1.pos (Access is denied)
    CAUSE: null
    MSG: C:\ozzypos\orders\2011.03.29_1.pos (Access is denied)

    BUILD SUCCESSFUL (total time: 13 seconds)

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I found this post from about 2 weeks ago, exactly the same problem as me.
    Access is denied when trying to create a file.

    so i changed all the paths to double back-slash i.e. C:\\ozzypos\\orders\\ and did the double back-slash at the end too '\\'. this did NOT work for me!!!

    on the plus side, i dont get any errors if i take out the CMD command stuff (hiding & locking). so its all related to hiding and locking.

    EDIT: I took out all the CMD stuff and just put this bit in before writing the file, and I found that the following code by itself is harmless:

    Java Code:
                String unlock = "attrib -h -s " + filename;
                Runtime.getRuntime().exec(unlock);

    But when I add this after writing the object there is the same problem:

    Java Code:
                String lock = "attrib +h +s " + filename;
                Runtime.getRuntime().exec(lock);
    Last edited by ozzyman; 03-29-2011 at 10:41 PM.

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Implementing a counter to track the number of saves & updates shows me that the first save is fine, the rest are not:

    run:
    Save number: 1
    Taking Order 1
    Save number: 2
    CAUSE: null #2
    MSG: C:\ozzypos\orders\2011.03.29_1.pos (Access is denied)

    Save number: 3
    CAUSE: null #3
    MSG: C:\ozzypos\orders\2011.03.29_1.pos (Access is denied)

    but why not? if i unlocked and unhide the file before it is written to :S

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default

    Ask windows.
    Because this strikes me as something more to do with the interaction with the OS.

    Have you waited to ensure that your call to unlock them has worked, as suggested by Darryl above (using waitFor())? Because it may not actually be unlocking the file before you attempt to write to it.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    There are reports that explicitly setting the file or stream variable reference to null, sometimes followed by a System.gc(), has been effective in releasing Windows file locks.

    Note that while all Sun JVM implementations do in fact run the garbage collector in response to a call to System.gc(), this is neither mandated by the API nor the VM spec.

    db

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Ohhhhhh my God. DARRLY YOU GENIUS!!! Where did u even learn about that??

    This completely works:
    Java Code:
                String unlock = "attrib -h -s " + filename;
                java.lang.Process p1 = Runtime.getRuntime().exec(unlock);
                p1.waitFor();

    Problem solved :D. Thank youuu sooo much!


    p.s. i'm sorry i didn't try it before i completely forgot about it until Tolls rementioned it! thanks to Tolls as well :)

  12. #12
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Java Code:
                String unlock = "attrib -h -s " + filename;
                java.lang.Process p1 = Runtime.getRuntime().exec(unlock);
                p1.waitFor();
    Wow valuable info, may save some hair pulling someday.
    Just curious, does this pop up a command prompt? If yes how long does the window stay up?

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    Just curious, does this pop up a command prompt? If yes how long does the window stay up?
    Why don't you try it and see for yourself? ;)

    db

  14. #14
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Why don't you try it and see for yourself? ;)

    db
    I have dospromptophobia :(
    Last edited by cultclassic; 03-31-2011 at 06:35 PM.

  15. #15
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    phobia? lol :P don't worry it doesn't pop up anything

    Runtime.getRuntime().exec(command) is a built in feature to run a system command so its not like opening a BAT file

Similar Threads

  1. IOException error
    By eeraj in forum AWT / Swing
    Replies: 3
    Last Post: 08-31-2009, 04:05 AM
  2. IOException error
    By eeraj in forum AWT / Swing
    Replies: 0
    Last Post: 08-29-2009, 09:12 AM
  3. Replies: 0
    Last Post: 08-03-2009, 08:04 AM
  4. minimum word length in search problematic
    By jocassid in forum Suggestions & Feedback
    Replies: 3
    Last Post: 07-01-2008, 04:24 AM
  5. GUI IOException
    By serfster in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •