I'm using Java 1.5. Are there any pre-built functions for making a
file name unique? And by "unique", I am downloading files over an FTP
connection and if the file exists in the local directory, I would want
to create another with a "-1", "-2", etc., added before the file
extension. For example, if I were downloading "abc.txt", and
"abc.txt" already existed in the current directory, I would want the
newly downloaded file to be "abc-1.txt".
Thanks for your feedback, - Dave
If you really need the specific numbering you describe, you need to
write it yourself. If that is just an example of the sort of thing you
want, see File.createTempFile.
Patricia
I do require this specific numbering. Does this style of numbering
have a name? Searching "java making file names unique" does not turn
up this convention in Google. - Dave
No name is required, and it's fairly simple to do. You can use Java to
get a list of file names in the directory where you want to place the
file. Then find all names with the pattern "abc-[0-9]+.txt". and choose
the next free number.
And then decide if you want to account for someone else possibly
having created a file with your chosen name in the time your program
took to determine this.
Cheers,
Bent D
--
Bent Dalager - b...@pvv.org - http://www.pvv.org/~bcd
powered by emacs
That can be handled by using File's createNewFile to create it.
Patricia
Nothing directly to do with your question, but do you require any
particular sort order for these filenames? Different operating
systems may treat numerics in filenames differently.
Some leading zeros in the numeric part may be useful:
abc-001.txt
abc-002.txt
etc.
rossum
Regarding ...
> You can use Java to get a list of file names
What package/class is this done from? Could you give an example?
Thanks, - Dave
The key class for all of this is java.io.File. If you have not already
done so, I strongly recommend reading its documentation. It has a lot of
relevant methods.
Patricia
laredotornado wrote:
> What package/class is this done from? Could you give an example?
>
Patricia Shanahan wrote:
> The key class for all of this is java.io.File. If you have not already
> done so, I strongly recommend reading its documentation. It has a lot of
> relevant methods.
>
This will change in Java 7.
<http://java.sun.com/docs/books/tutorial/essential/io/legacy.html>
<http://java.sun.com/docs/books/tutorial/essential/io/dirs.html>
and generally
<http://java.sun.com/docs/books/tutorial/essential/io/fileio.html>
--
Lew
While those are good links, I just thought I'd add that the Java 7 docs
are up in draft form already:
<http://java.sun.com/javase/7/docs/api/java/nio/file/Path.html>
You're referring to using a filter as part of list or listFiles? From
what I'm seeing about filters, they take a string but I can't find
anything about using a regular expression of the type Lars wrote.
What am I missing?
- Dave
Filter and FileNameFilter are both interfaces. You can write your own to
do any test you like, including but not limited to a regular expression
match against the file name.
Alternatively, just obtain the unfiltered list of all the files in the
directory, and then use regular expression checks on the names.
Patricia
You can use java.io.File to return to you a list of extant files whose
names match the pattern to which your program is sensitive. But the
rest of the work of generating an unused name is still up to you.
FileFilter and FilenameFilter are just interfaces; you can use regular
expressions in your implementations, if you so wish.
import java.io.File;
import java.util.regex.Pattern;
...
Pattern FILENAME_PATTERN = Pattern.compile(...);
File directory = ...;
File[] files = directory.listFiles(new FileFilter() {
public boolean accept(File path) {
return path.isFile()
&& FILENAME_PATTERN.matcher(path.getName()).matches();
}
});
You're missing the fact that *you* write the innards of
the FileFilter, and you can make any tests you please on the
file name and directory you are given. You can use regular
expressions, hash codes, random numbers, ... anything at all.
--
Eric Sosman
eso...@ieee-dot-org.invalid
Thanks. My original intention was to simplify the code I already
had ...
private File uniquifyLocalFile(final File p_localFile) {
File localFile = p_localFile;
if(localFile != null) {
Integer revision = 0;
final String fileName = localFile.getName();
final Integer index = fileName.indexOf('.');
while(localFile.exists()) {
localFile = new File(localFile.getParent(),
fileName.substring(0, index) + "-" +
revision++ +
fileName.substring(index));
} // while
} // if
return localFile;
}
but it looks like even if I use the reg exp strategy suggested here, I
will still have to do a loop to find what hte next non-existent file
is. In fact, I'm thinking the code would be lengthier than what I
already have. If you think differently, please let me know.
- Dave
Just a couple of comments ...
First, you might want fileName.lastIndexOf('.'), depending
on whether you think "the extension" of foo.11Nov.txt is .txt
or .11Nov.txt.
Second, you need to handle the case where the file name
contains no dot at all. Presumably, given a filename foo you
would rather look at foo-1, foo-2, ... than get an exception
thrown at your head.
Third, you need to decide what to do if the given filename
already looks like foo-42: Do you want to try foo-42-1, or move
on to foo-43?
Fourth -- and this one's the big, gaping hole -- you need to
realize that the state of the file system may change between the
moment you call localFile.exists() and the moment you create the
file and start writing to it. If two people both send you foo.txt
at about the same time, both of them might find that there's no
foo.txt already there, and both may then start writing to it ...
Personally, I'd skip the exists() calls and use createNewFile()
instead: it does the existence test and the file creation in one
indivisible step.
> but it looks like even if I use the reg exp strategy suggested here, I
> will still have to do a loop to find what hte next non-existent file
> is. In fact, I'm thinking the code would be lengthier than what I
> already have. If you think differently, please let me know.
Yes, you need more code. Different code, at any rate.
--
Eric Sosman
eso...@ieee-dot-org.invalid
http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html#randomUUID()
UUIDs are unique.
Regards,
Benjamin
No.
It is possible just with extreme low probability to get
duplicates.
http://en.wikipedia.org/wiki/UUID#Random_UUID_probability_of_duplicates
Arne
Practically seen: unique.
You will be suffering a lot more undetected errors -- even with ECC --
in memory before you suffer from UUIDs collisions.
So while mathematically correct, towards the problem domain your comment
is useless.
> It is possible just with extreme low probability to get
> duplicates.
Practically irrelevant.
Why Should I Care What Color the Bikeshed Is?
;)
Regards,
Benjamin