I have a directory with files.
I have a function that takes a String fileName
. I would like to either be able to open that file and if it does not exist open a file that has the most similar name.
E.g.
If filename passed is file_name_v1.txt
then if that does not exist open file_name_v2.txt
These are just examples. I don’t really know know the actual number after _v
so I don’t know how I could create a regex for a file for this case
Asked
Active
Viewed 160 times
-4

Jim
- 3,845
- 3
- 22
- 47
-
Sort file names by dictionary order – flyingfox Jul 11 '18 at 09:25
-
You mean all files in the dir? – Jim Jul 11 '18 at 09:31
1 Answers
0
This has been answered here: How to find files that match a wildcard string in Java?
Note that the solutions above requires Java 7 NIO.
PathMatcher pathMatcher = FileSystems.getDefault()
.getPathMatcher("regex:Test.[\\/]sample\\w+\\.txt");
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(
new File("..").toPath(), pathMatcher::matches)) {
dirStream.forEach(path -> System.out.println(path));
}

adinapiza
- 17
- 5