AntoniRyszard656
asked on
Creating unique filenames?
Hello,
Using the createNewFile or createTempFile methods, can we create a unique filename in a particular folder?
The files will be of all different types, but I wondered if we could use these methods to create filenames starting with tmp, and tmp1 ect.
boolean createNewFile()
static File createTempFile(String prefix, String suffix)
static File createTempFile(String prefix, String suffix, File directory)
Using the createNewFile or createTempFile methods, can we create a unique filename in a particular folder?
The files will be of all different types, but I wondered if we could use these methods to create filenames starting with tmp, and tmp1 ect.
boolean createNewFile()
static File createTempFile(String prefix, String suffix)
static File createTempFile(String prefix, String suffix, File directory)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Could I ask what the "Prefix", "Suffix" are?
>> Would the createTempFile not loop until it finds an un-used filename?
No, you have full control of the name of the file. The guarantee with createTempFile(), is that no other part of you program, running in the same JVM instance, can create another file with the same path, and that once it returns a File object you know that the file is writeable.
No, you have full control of the name of the file. The guarantee with createTempFile(), is that no other part of you program, running in the same JVM instance, can create another file with the same path, and that once it returns a File object you know that the file is writeable.
ASKER
I would like to call the filenames tmp, tmp1 ....
But as I mentioned the file types might not be of the same types. They could be a jpeg, doc, bmp or someother. Would this cause problems?
But as I mentioned the file types might not be of the same types. They could be a jpeg, doc, bmp or someother. Would this cause problems?
>> But as I mentioned the file types might not be of the same types. They could be a jpeg, doc, bmp or someother. Would this cause problems?
It would not cause any problems. You would need to use the suffix for the right file extension.
It would not cause any problems. You would need to use the suffix for the right file extension.
ASKER
So I would also have find someway to determine the file extension?
ASKER
Would I need to use something like the tokenstring class, or indexof to determine the file types?
Or is there some function in the java.io.File class to return the file extension.
Or is there some function in the java.io.File class to return the file extension.
>> Would I need to use something like the tokenstring class, or indexof to determine the file types?
>> Or is there some function in the java.io.File class to return the file extension.
Not sure what you're trying to do here. You specify the file name to use...including the file extension (which bears to file type)
>> Or is there some function in the java.io.File class to return the file extension.
Not sure what you're trying to do here. You specify the file name to use...including the file extension (which bears to file type)
ASKER
Thanks
I was trying to write a web application which allows the user to uploadfiles.
Each user has a folder, but in the application allows the user if they wish upload the same file.
The file extensions are unknown.
I was trying to write a web application which allows the user to uploadfiles.
Each user has a folder, but in the application allows the user if they wish upload the same file.
The file extensions are unknown.
ASKER
I have a string variable with for example test.jpg, I think I could use indexof to determine the extension.
>> I have a string variable with for example test.jpg, I think I could use indexof to determine the extension.
Indeed. If you only allow certain file types then you could implement and use a FilenameFilter. Or, just use indexOf remembering to handle the case where there might not be a '.' in the filename.
Indeed. If you only allow certain file types then you could implement and use a FilenameFilter. Or, just use indexOf remembering to handle the case where there might not be a '.' in the filename.
>> I think I could use indexof to determine the extension
Make sure you use lastIndexOf ( '.' ) and not indexOf ( '.' ) because your file name could be something like "firstpart.secondpart.ext" in which case indexOf () will return the wrong extension.
Make sure you use lastIndexOf ( '.' ) and not indexOf ( '.' ) because your file name could be something like "firstpart.secondpart.ext"
ASKER
Thanks
Would the Suffix be the tmp filename, if I wanted the tmp to increment to 1, 2... until a filename is not used. Would I need to continue to increment tmp by 1 myself?
"Prefix", "Suffix"
Would the Suffix be the tmp filename, if I wanted the tmp to increment to 1, 2... until a filename is not used. Would I need to continue to increment tmp by 1 myself?
"Prefix", "Suffix"
>> Would the Suffix be the tmp filename, if I wanted the tmp to increment to 1, 2... until a filename is not used.
YOU pick the prefix and suffix. The quoted names above are examples.
They can be whatever you want, as long as you only pick legal filename characters.
>> Would I need to continue to increment tmp by 1 myself?
Yes
YOU pick the prefix and suffix. The quoted names above are examples.
They can be whatever you want, as long as you only pick legal filename characters.
>> Would I need to continue to increment tmp by 1 myself?
Yes
Thanks
To tell the createTempFile where to store the files, would I use:
File path = new File("/files/userfolder/") ;
File.createTempFile("tmp", extension, path);
To tell the createTempFile where to store the files, would I use:
File path = new File("/files/userfolder/")
File.createTempFile("tmp",
Yes, that's the best You can do.
BTW, are you using two accounts?
It looks like the author chose his\her answer:
https://www.experts-exchange.com/questions/21818068/Creating-unique-filenames.html#16597806
, but didn't really come back to close it, cause he\she was accused of having more than one account.
https://www.experts-exchange.com/questions/21818068/Creating-unique-filenames.html#16597806
, but didn't really come back to close it, cause he\she was accused of having more than one account.
I can't be objective enough in deciding to whom to assign the points, I am one of the contributors.
Well, I put in a lot of time answering the "questions" posed by this user...I definately think I should get the lion share of the points, however, I leave to you to decide.
I thought orangehead was getting some....
mayankeagle,
there is not enough pionts left :)
p.s. how one should pronounce Your nick?
there is not enough pionts left :)
p.s. how one should pronounce Your nick?
Whichever way he wants ;-) I prefer just Mayank. Eagle was added to it ;-) I Know there is not enough left but I thought orangehead was getting some because:
>> "I put in a lot of time answering the "questions" posed by this user...I definately think I should get the lion share of the points"
>> "I put in a lot of time answering the "questions" posed by this user...I definately think I should get the lion share of the points"
The points are not enough for everybody, as StillUnAware mentioned, so I decided to recommend the two fastest solutions. Sorry orangehead911 won't get any points :(, but I recommend possible solutions on a "per question" basis.
You're doing a good job with the clean-up, girionis, thanks for all of it :)
Nice one GranMod.
mayankeagle thanks for your nice words :)
mayankeagle thanks for your nice words :)
:-(
:-)
ASKER