Making file name unique

L

laredotornado

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
 
L

laredotornado

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
 
L

Lars Enderin

laredotornado said:
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.
 
B

Bent C Dalager

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
 
D

Daniel Pitts

Bent said:
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 ;-)
 
L

laredotornado

laredotornadowrote:
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.

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
 
L

Lew

What package/class is this done from? Could you give an example?

Patricia said:
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>
 
L

laredotornado

laredotornadowrote:
laredotornadowrote:
laredotornadowrote:
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
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.
Regarding ...
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.

Patricia


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
 
N

Noel

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

Eric Sosman

laredotornado said:
laredotornadowrote:
[...]
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.
 
L

laredotornado

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

});

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
 
E

Eric Sosman

laredotornado said:
[...]
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.
 
B

Benjamin Rampe

laredotornado said:
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".

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

UUIDs are unique.

Regards,
Benjamin
 
B

Benjamin Rampe

Arne said:

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
 
T

Teraposa Lunodas

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top