-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileFinder.java
More file actions
54 lines (46 loc) · 1.35 KB
/
Copy pathFileFinder.java
File metadata and controls
54 lines (46 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* **********************************************************************
* **********************************************************************
*
*
*
*/
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
public class FileFinder
{
public String findFile(String fileName)
{Path startPath = Path.of("."); // Start searching from the current directory
try (Stream<Path> stream = Files.walk(startPath))
{Path foundFile = stream
.filter(Files::isRegularFile)
.filter(path -> path.getFileName().toString().equals(fileName))
.findFirst()
.orElse(null);
if (foundFile != null)
{//return foundFile.toAbsolutePath().toString();
return foundFile.toString();
}
}
catch (IOException e)
{System.out.println("IO error looking for ["+fileName+"]");
System.exit(8);
}
return "";
}
public static void main(String[] args)
{String fileName, fileNameResult;
fileName = "xferMail";
fileName = "Xx.java";
if (args.length > 0)
fileName = args[0];
FileFinder ff = new FileFinder();
fileNameResult = ff.findFile(fileName);
if (fileNameResult == null)
System.out.println("Can't find: "+fileName);
else
System.out.println("Found file: ["+fileNameResult+"]");
}
}