forked from ucsd-cse15l-f22/lab3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExample.java
More file actions
67 lines (50 loc) · 1.66 KB
/
FileExample.java
File metadata and controls
67 lines (50 loc) · 1.66 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
55
56
57
58
59
60
61
62
63
64
65
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileExample {
/*
Given a File (which can represent either a file or directory), return a list
of all the files in that directory and all its subdirectories.
If given the path of a file (rather than a directory), it returns a list with
just that element.
For example, for the following structure:
some-files/
|- a.txt
|- more-files/
|- b.txt
|- c.java
|- even-more-files/
|- d.java
|- a.txt
Given new File("some-files/") as a parameter, we'd expect [some-files/a.txt,
some-files/more-files/b.txt, some-files/more-files/c.java,
some-files/even-more-files/d.java, some-files/even-more-files/a.txt] as results
Given new File("some-files/more-files") as a parameter, we'd expect
[some-files/more-files/b.txt, some-files/more-files/c.java] as results
Given new File("some-files/a.txt") as a parameter, we'd expect
[some-files/a.txt] and a result
See the File documentation here: https://docs.oracle.com/javase/8/docs/api/java/io/File.html
*/
static List<File> getFiles(File start) throws IOException {
File f = start;
List<File> result = new ArrayList<>();
if(f.isFile()){
result.add(f);
return result;
}
if(f.isDirectory()) {
File[] paths = f.listFiles();
for(File subFile: paths) {
if(subFile.isFile())
result.add(subFile);
else if(subFile.isDirectory()){
File[] paths2 = subFile.listFiles();
for(File subFile2: paths2)
result.add(subFile2);
}
}
}
return result;
}
}