forked from JavaOPs/basejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFile.java
More file actions
43 lines (36 loc) · 1.29 KB
/
MainFile.java
File metadata and controls
43 lines (36 loc) · 1.29 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
package ru.javaops.webapp;
import ru.javaops.webapp.exception.StorageException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class MainFile {
public static void main(String[] args) {
File filePath = new File("./.gitignore");
try {
System.out.println(filePath.getCanonicalPath());
} catch (IOException e) {
throw new RuntimeException("Error", e);
}
try (FileInputStream fis = new FileInputStream(filePath)) {
System.out.println(fis.read());
} catch (IOException e) {
throw new RuntimeException(e);
}
File dir = new File("./src/ru/javaops"); // /webapp
getDirectoryFileNames(dir, "");
}
private static void getDirectoryFileNames(File dir, String space) {
File[] files = dir.listFiles();
if (files == null) {
throw new StorageException(dir.getName() + " is not directory, or IO Error");
}
for (File file : files) {
if (file.isFile()) {
System.out.println(space + file.getName());
} else {
System.out.println(space + "[" + file.getName() + "]");
getDirectoryFileNames(file, space + " ");
}
}
}
}