• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

random file name

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be done on a server so maybe it is more about servlets.

it is two questions:

1. I wonder if there is already something in java for creating random strings at length n. I need them for creating random file names. If i should do it myself, is there anything in java to help with it or should I really do it from the scratch as if I am working with the good old c++. sure I have rand() but anything more???


2. when i finally get a potential random file name, I need to block a directory called "tempUpload" to other other attempts to write to this directory, then see if there is no such a file already there with that name, then write it to the directory. then I realese the dir source to other users. but how do I block it from other writing attempts???
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like you want to create a unique temporary file. If so, check out File.createTempFile(prefix, suffix, dir).

If not, then you'll need to create your own random String generator for part 1 and use some form of locking file (if your program will compete with other applications outside the JVM or in other JVMs) or use synchronization on a singleton if you just need to synchronize within the JVM.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. I wonder if there is already something in java for creating random strings at length n. I need them for creating random file names. If i should do it myself, is there anything in java to help with it or should I really do it from the scratch as if I am working with the good old c++. sure I have rand() but anything more???



This is rather simply to generating random passwords of random lengths. You could define an array of char to contain 'A' - 'Z' and 'a' - 'z'. Also, an array of int to contain 0 - 9. By using the nextInt(int n) method of the Random class, you could obtain the random length of the filename. Then using a for-loop, you could call the same method repeatedly to get a random character or random integer from the two arrays.

But if what you need is just a simple unique filename, simply use the System.currentTimeMillis method to return you the current system time in milliseconds. This will definitely be unique.

2. when i finally get a potential random file name, I need to block a directory called "tempUpload" to other other attempts to write to this directory, then see if there is no such a file already there with that name, then write it to the directory. then I realese the dir source to other users. but how do I block it from other writing attempts???



In this case, the directory would be your critical section/region. All you have to do is just to synchronize the method that writes files to that directory. Or you just synchronize the block of codes that does the writing of files into the directory.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heck, I would use System.currentTimeMillis().

This would give me a unique, "random" name. And the length is constant ;-)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Azriel Abramovich:
Heck, I would use System.currentTimeMillis().

This would give me a unique, "random" name.



If you are creating files very fast, that isn't even unique inside the same VM...
 
I am a man of mystery. Mostly because of this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic