Home arrow static arrow Java Programming [Archive] - can java create unique filenames?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - can java create unique filenames?
This topic has 12 replies on 1 page.

Posts:343
Registered: 7/13/04
can java create unique filenames?  
Jul 14, 2004 12:52 PM



 
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
String fileName = "c:/" + Integer.toString(stuff) + ".txt";
out.write(stuff);

The reason that this is important, is because I want to be able to create a unique filename each time I run the program (which records important data). I dont want the data overwritten within the same file over and over again.

currently my program looks like this:

DataOutputStream out = new DataOutputStream(new FileOutputStream("c:/data.txt"));
out.write(stuff);

each time I run the program however, the data.txt file will just get overwritten. How can I make my program resemble the one above without Java rejecting the codeing?
 

Posts:66
Registered: 7/13/04
Re: can java create unique filenames?  
Jul 14, 2004 12:58 PM (reply 1 of 12)



 
You can use the current date/time new java.util.Date.getTime() as one idea.

The other idea, is to use the grey matter.
 

Posts:7,258
Registered: 1/31/02
Re: can java create unique filenames?  
Jul 14, 2004 1:00 PM (reply 2 of 12)



 
You might check out the following method:

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String)

(the forum formatting will likely jack the URL up though)

Lee
 

Posts:7,258
Registered: 1/31/02
Re: can java create unique filenames?  
Jul 14, 2004 1:00 PM (reply 3 of 12)



 
(which means you get to manually enter the trailing closing parentheses)
 

Posts:343
Registered: 7/13/04
Re: can java create unique filenames?  
Jul 14, 2004 1:01 PM (reply 4 of 12)



 
how would getting the current date/time help me?
 

Posts:2,206
Registered: 8/15/02
Re: can java create unique filenames?  
Jul 14, 2004 1:10 PM (reply 5 of 12)



 
Getting the time is good. You can use SimpleDateFormatter to get a String of the form HHmmssSSS, which will be pretty unique unless you are creating several files very rapidly and you are running windows (**** time resolution). You can then use this in your file.

You could increase uniqueness by adding a constructing new Random number generaters based on a seed that chnages from file to file (like the hashcode associated with the file)
 

Posts:31,095
Registered: 4/30/99
Re: can java create unique filenames?  
Jul 14, 2004 1:21 PM (reply 6 of 12)



 
"Pretty unique" is pretty good, but I think that File.createTempFile() is a better solution.

PC²
 

Posts:2,206
Registered: 8/15/02
Re: can java create unique filenames?  
Jul 14, 2004 1:31 PM (reply 7 of 12)



 
At least in 1.4.2, you need to supply some names to actually create the temp file. It does not guarantee uniqueness.
 

Posts:31,095
Registered: 4/30/99
Re: can java create unique filenames?  
Jul 14, 2004 1:42 PM (reply 8 of 12)



 
Sure, you need to specify part of the filename. The OP's example specified ".txt" as the last 4 characters, for example. But the API docs say:

"...it is guaranteed that:
1. The file denoted by the returned abstract pathname did not exist before this method was invoked."

That (to me, anyway) says that the method will always generate a filename that doesn't exist in the file system. That sounds unique enough to me, although it certainly could generate a filename that was used earlier by the program and was subsequently deleted. I suppose it's up to the OP to decide whether unique means "different from any filename now on the system" or "different from any filename ever produced by the program". It sounded like the former to me.

PC²
 

Posts:2,206
Registered: 8/15/02
Re: can java create unique filenames?  
Jul 14, 2004 2:00 PM (reply 9 of 12)



 
To throw this back at ya - the full quote is
If this method returns successfully then it is guaranteed that:

1. The file denoted by the returned abstract pathname did not exist before this method was invoked, and
2. Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.


The first 5 words are VERY significant. This means if the strings you pass in are the same as an existing file, the method will return false, and will continue to return false until either you change the arguments or the file gets deleted.
 

Posts:2,206
Registered: 8/15/02
Re: can java create unique filenames?  
Jul 14, 2004 2:12 PM (reply 10 of 12)



 
I defer to DrClap - it does seem to work. Usingthe following code
import java.io.*;                                                                                                                         public class Test {                                                                                                                             public static boolean createTempFile (String prefix, String suffix) {        try {            File.createTempFile(prefix, suffix);        }        catch (IOException e) {            return false;        }        return true;    }                                                                                                                             public static void main( String [] args ) {        System.out.println(Test.createTempFile("foo", "bar"));        System.out.println(Test.createTempFile("bar", "foo"));        System.out.println(Test.createTempFile("foo", "bar"));    }}


I get files of the form
fooNNNNNbar created. So my question is when would his EVER throw an IOException?
 

Posts:31,095
Registered: 4/30/99
Re: can java create unique filenames?  
Jul 14, 2004 2:19 PM (reply 11 of 12)



 
I don't understand that. Do you mean that if I use
File.createTempFile("abc", ".txt", new File("C:/archive/"))
and if C:/archive/abc.txt exists already, then an exception will be thrown instead of the method returning something like C:/archive/abc40443.txt? The documentation certainly doesn't say that, and when I tried it I got a new file created with no exception. Or did you mean something else?
 

Posts:31,095
Registered: 4/30/99
Re: can java create unique filenames?  
Jul 14, 2004 2:22 PM (reply 12 of 12)



 
I defer to DrClap - it does seem to work.

Yes; I use it to save versions of a transaction file for audit purposes.

So my question is when would this EVER throw an IOException?

It throws an IOException when you give it a File that represents a directory that doesn't exist, for one thing. It would likely throw one if you weren't authorized to create files in the directory, but I don't have a quick test environment for that.
 
This topic has 12 replies on 1 page.