Re: Making file name unique
On Nov 9, 3:17 pm, Noel <prosthetic_conscien...@yahoo.com> wrote:
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 alread=
y
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? Fro=
m
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()).match=
es();
}
});
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