This discussion is archived
9 Replies Latest reply: Sep 10, 2010 1:24 PM by 653909 RSS

performance of File.mkdirs()

653909 Newbie
Currently Being Moderated
What is the performance of that method ?

I am creating multiple directories very often.
  • 1. Re: performance of File.mkdirs()
    3004 Newbie
    Currently Being Moderated
    javaUserMuser wrote:
    What is the performance of that method ?
    42.
    I am creating multiple directories very often.
    So, as a start, write a sample program that just creates a bunch of directories as fast as possible, and see if that's sufficient for your needs. If that program can create directories 100 times faster than you expect to need them, you don't have anything to worry about. If it does so less than twice as fast as you need, then you may have a problem.

    I just whipped one up in about 3 minutes and it confirmed that mkdirs is plenty fast for me.

    However, I would say if creating directories is a performance bottleneck, you probably ought to rethink your design.
  • 2. Re: performance of File.mkdirs()
    EJP Guru
    Currently Being Moderated
    What is the performance of that method ?
    Compared to what?
    I am creating multiple directories very often.
    And doing nothing else? Not creating any files? No file I/O? Nothing that would dwarf the performance of File.mkdirs() into insignificance?
  • 3. Re: performance of File.mkdirs()
    653909 Newbie
    Currently Being Moderated
    Well.. ejp.. you can guess that its part of a bigger picture. I am creating files. I just needed to know if that invocation is better to using createNewFile() or whatever, I dont know. I am not much experienced in low level I/O.
  • 4. Re: performance of File.mkdirs()
    EJP Guru
    Currently Being Moderated
    I just needed to know if that invocation is better to using createNewFile()
    That sort of answer is exactly why I asked 'compared to what?' They do different things. Apples and oranges. The only alternative to File.mkdirs() is to parse the filename and call File.mkdir() yourself for each directory, and there's no reason to believe that would be any faster.

    And you don't need to call File.createNewFile() at all if you're about to construct a new FileOutputStream or new FileWriter().
  • 5. Re: performance of File.mkdirs()
    653909 Newbie
    Currently Being Moderated
    Oh, thanks. I appreciate that.

    Well, what I am trying to accomplish is to achieve continuous files in the file system, and what I am doing is creating a new file, then of it - a random access file object, then setting its length to the supposed file size.
    I am just not sure if FileOutputStream would do that. E.g. 2 concurrent threads (servlet) can create one FOS each, then when the first increases the file to e.g. 5k, then the second does the same, and the 1st writes again - files fragment. I am trying not to do that.
  • 6. Re: performance of File.mkdirs()
    EJP Guru
    Currently Being Moderated
    This is a brand new question. RandomAccessFile also creates the file if necessary, and you still don't have to call File.createNewFile().

    And what that has to do with File.mkdirs() is a mystery to me.
  • 7. Re: performance of File.mkdirs()
    653909 Newbie
    Currently Being Moderated
    Eh, Im sorry, its 5 am here, I have had no sleep for 24+ hours.. Cannot think clearly .. Im going to sleep now. Have a nice day, thanks for the help, and happy coding ;)
    See you tomorrow.
  • 8. Re: performance of File.mkdirs()
    3004 Newbie
    Currently Being Moderated
    javaUserMuser wrote:
    Eh, Im sorry, its 5 am here, I have had no sleep for 24+ hours.. Cannot think clearly .. Im going to sleep now. Have a nice day, thanks for the help, and happy coding ;)
    See you tomorrow.
    When you wake up, review my advice in reply 1: Create a sample program that does nothing but create directories and files in a pattern that's representative of your actual use case, and time it. If it's too slow, post it here (make sure it's an SSCCE that just shows the relevant file/directory operations and no extra fluff) and explain exactly what the problem is.
  • 9. Re: performance of File.mkdirs()
    653909 Newbie
    Currently Being Moderated
    Lets say, that I messed up the API semantics, and my question is nonsense.
    Anyway it was just theoretical.. No need to prove anything.