Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: We get the exception :Cannot create a file when that file already exists

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question We get the exception :Cannot create a file when that file already exists

    Hy all

    Following java code is run under struts action handler ,our system is multiuser environment ,so multiple threads is created to handle the multiple user requests.

    File.mkdirs() is not thread safe.

    The problem is the way in which the mkdirs() function is implemented. It first tries to check whether each of the parent directories exist, and if a parent directory does not exist, then it is created using the mkdir() function.

    suppose two threads try to create the following two directories at the same time

    Thread1: "Test/CoreJava/Threads"

    Thread2: "Test/CoreJava/Swings"

    At here suppose Thread1 attempt to file.mkdirs() , and it will make the directory Test/CoreJava/, at the same time Thread2 also attempt to file.mkdirs() or vise verse.So Thread1 or Thread2 attempt will fail and give the error :Cannot create a file when that file already exists.

    Following is our code...


    if (!file.exists())
    {
    try
    {
    file.mkdirs();
    }
    catch (Exception e)
    {
    if (!file.exists())
    {
    file.mkdirs();
    }
    }
    }

    How to resolve the describe situation?pls give the solution to prevent the exception?
    Can i put the file.mkdirs() code in synchronize block, if i put the synchronize block then should we get any issue in multi user web based application?

    any help will be appreciate.

    Thanks & Regards
    Yatin Baraiya


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: We get the exception :Cannot create a file when that file already exists

    Quote Originally Posted by yatin.baraiya View Post
    Can i put the file.mkdirs() code in synchronize block, if i put the synchronize block then should we get any issue in multi user web based application?
    What happened when you tried?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: We get the exception :Cannot create a file when that file already exists

    Still i have not try , but i think it create the below issue in our application

    Scenario is like
    User 1:upload file "text.txt" in D:/Yatin/Mydata....

    User 2:Upload the file "sample.txt" in D:/Yatin/Mydata

    while if i have put file.mkdirs() in the synchronize block,then if we think that both user perform the operation at the same time.

    then user 2 need to wait still user 1 process is not perform ....so this is not the perfect solutions, for us. it cause the delay the performance of our application.

    can you provide me other approach for the my issue?
    Last edited by yatin.baraiya; May 24th, 2012 at 09:35 AM.

  4. #4
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: We get the exception :Cannot create a file when that file already exists

    Quote Originally Posted by yatin.baraiya View Post
    while if i have put file.mkdirs() in the synchronize block,then if we think that both user perform the operation at the same time.

    then user 2 need to wait still user 1 process is not perform ....so this is not the perfect solutions, for us. it cause the delay the performance of our application.
    Do yourself a favor and don't over analyze this. You're worrying about micro-optimization - how long does it really take to create a set of directories especially after the first time? I'm willing to be it is on the order of 10s of milliseconds. Your users can't wait a few milliseconds?

    If this is your architecture then you're stuck with synchronization. Don't worry about saving a few milliseconds. Unless you can explicitly prove that this is where a bottleneck exists then you're wasting your time with something that will likely never be a performance problem.
    Need Java help? Check out the HotJoe Java Help forums!

  5. The Following User Says Thank You to stdunbar For This Useful Post:

    KevinWorkman (May 24th, 2012)

  6. #5
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: We get the exception :Cannot create a file when that file already exists

    Quote Originally Posted by stdunbar View Post
    Do yourself a favor and don't over analyze this. You're worrying about micro-optimization - how long does it really take to create a set of directories especially after the first time? I'm willing to be it is on the order of 10s of milliseconds. Your users can't wait a few milliseconds?

    If this is your architecture then you're stuck with synchronization. Don't worry about saving a few milliseconds. Unless you can explicitly prove that this is where a bottleneck exists then you're wasting your time with something that will likely never be a performance problem.
    Hy stdunbar

    ok, i am not worry about micro milliseconds , now i am applying that code inside the synchronization block, but i am not sure about that, is it really solve my issue? Can you describe me about that synchronization block use in regards of my issue?

  7. #6
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: We get the exception :Cannot create a file when that file already exists

    I would do something like:

    public static synchronized void makeDirectories( File file ) {
        boolean createdDirectories = file.mkdirs();
     
        if( !createdDirectories )
            throw new IOException( "Can't create directory \"" + file.getCanonicalPath() + "\"" );
    }

    It doesn't have to be much more than that.
    Need Java help? Check out the HotJoe Java Help forums!

Similar Threads

  1. solaris machine /tmp folder, File.exists() cant see the existing file.
    By aragorn1905 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 27th, 2011, 09:41 AM
  2. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  3. File exists or not
    By Ballister in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2011, 04:38 AM
  4. create a file
    By mos33 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 4th, 2009, 02:21 PM
  5. How to create exe file
    By sirimalla in forum Java Theory & Questions
    Replies: 6
    Last Post: November 1st, 2009, 04:07 AM