Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Making file name unique

11 views
Skip to first unread message

laredotornado

unread,
Nov 9, 2009, 10:29:55 AM11/9/09
to
Hi,

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

Patricia Shanahan

unread,
Nov 9, 2009, 10:43:50 AM11/9/09
to

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

laredotornado

unread,
Nov 9, 2009, 11:42:29 AM11/9/09
to

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

Lars Enderin

unread,
Nov 9, 2009, 11:55:54 AM11/9/09
to

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.

Bent C Dalager

unread,
Nov 9, 2009, 12:00:37 PM11/9/09
to
On 2009-11-09, Lars Enderin <lars.e...@telia.com> wrote:
>
> 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

Daniel Pitts

unread,
Nov 9, 2009, 12:45:18 PM11/9/09
to
Bent C Dalager wrote:
> On 2009-11-09, Lars Enderin <lars.e...@telia.com> wrote:
>> 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
Probably not, because this operation would probably be fast enough, at
least for desktop users. Even so, an error message would be nice, or
just trying the next number ;-)

Patricia Shanahan

unread,
Nov 9, 2009, 1:46:29 PM11/9/09
to
Bent C Dalager wrote:
> On 2009-11-09, Lars Enderin <lars.e...@telia.com> wrote:
>> 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.

That can be handled by using File's createNewFile to create it.

Patricia

rossum

unread,
Nov 9, 2009, 3:02:52 PM11/9/09
to

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

laredotornado

unread,
Nov 9, 2009, 3:32:46 PM11/9/09
to
On Nov 9, 9:55 am, Lars Enderin <lars.ende...@telia.com> wrote:
> laredotornadowrote:

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

Patricia Shanahan

unread,
Nov 9, 2009, 3:36:02 PM11/9/09
to

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


Lew

unread,
Nov 9, 2009, 3:49:45 PM11/9/09
to
Lars Enderin wrote:
>> You can use Java to get a list of file names
>

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

markspace

unread,
Nov 9, 2009, 4:08:00 PM11/9/09
to
Lew wrote:
>
> This will change in Java 7.
> <http://java.sun.com/docs/books/tutorial/essential/io/legacy.html>

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>

laredotornado

unread,
Nov 9, 2009, 4:17:45 PM11/9/09
to


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

Patricia Shanahan

unread,
Nov 9, 2009, 5:16:06 PM11/9/09
to

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

Noel

unread,
Nov 9, 2009, 5:17:25 PM11/9/09
to
On Nov 9, 1:17 pm, laredotornado <laredotorn...@zipmail.com> wrote:
> On Nov 9, 1:36 pm, Patricia Shanahan <p...@acm.org> wrote:
> > laredotornadowrote:
> > > On Nov 9, 9:55 am, Lars Enderin <lars.ende...@telia.com> wrote:
> > >> You can use Java to get a list of file names
>
> > > What package/class is this done from? Could you give an example?
>
> > 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.
>
> 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?

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();
}
});

Eric Sosman

unread,
Nov 9, 2009, 5:33:25 PM11/9/09
to
laredotornado wrote:
> On Nov 9, 1:36 pm, Patricia Shanahan <p...@acm.org> wrote:
>> laredotornadowrote:
>>> On Nov 9, 9:55 am, Lars Enderin <lars.ende...@telia.com> wrote:
>>>> [...]

>>>> You can use Java to get a list of file names
>>> What package/class is this done from? Could you give an example?
>> 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.
>
> 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?

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

laredotornado

unread,
Nov 9, 2009, 5:40:53 PM11/9/09
to
On Nov 9, 3:17 pm, Noel <prosthetic_conscien...@yahoo.com> wrote:

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

Eric Sosman

unread,
Nov 9, 2009, 6:01:25 PM11/9/09
to
laredotornado wrote:
> [...]

> 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;
> }

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

Benjamin Rampe

unread,
Nov 12, 2009, 7:04:01 PM11/12/09
to
laredotornado schrieb:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html#randomUUID()

UUIDs are unique.

Regards,
Benjamin

Arne Vajhøj

unread,
Nov 12, 2009, 8:50:10 PM11/12/09
to

No.

It is possible just with extreme low probability to get
duplicates.

http://en.wikipedia.org/wiki/UUID#Random_UUID_probability_of_duplicates

Arne

Benjamin Rampe

unread,
Nov 13, 2009, 11:05:46 AM11/13/09
to
Arne Vajh�j schrieb:
>> UUIDs are unique.
>
> No.

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

Teraposa Lunodas

unread,
Nov 24, 2009, 8:24:16 AM11/24/09
to
0 new messages