-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocateName.java
More file actions
42 lines (41 loc) · 981 Bytes
/
LocateName.java
File metadata and controls
42 lines (41 loc) · 981 Bytes
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
import java.io.File;
public class LocateName
{
private String fileName;
private File directoryName;
private File searchDir;
private File[] pathNames;
private String outputString = "Find file: ";
private boolean fileFound = false;
public LocateName(String fileName, String directory) throws NullPointerException
{
this.fileName = fileName;
this.directoryName = new File(directory);
outputString += fileName+"\n";
fileSearch(fileName, directoryName);
}
private void fileSearch(String fileName, File directoryName)
{
String filePath;
pathNames = directoryName.listFiles();
for (File path: pathNames)
{
if(path.isDirectory())
fileSearch(fileName,path);
else if(fileName.equals(path.getName()))
{
filePath = (path.getParent()+"\n");
outputString += filePath;
fileFound=true;
}
}
}
public String getOutput()
{
if (!fileFound)
{
outputString +="File Not Found\n";
}
return outputString;
}
}