18
18
public class FileOperations {
19
19
20
20
public static enum FileExtension {
21
- ALL ("" ),
22
- HTML (".html" ),
23
- JSON (".json" ),
24
- JAVA (".java" ),
25
- TEXT (".txt" );
21
+ ALL ("" ), HTML (".html" ), JSON (".json" ), JAVA (".java" ), TEXT (".txt" );
26
22
27
23
private final String extension ;
24
+
28
25
FileExtension (String extension ) {
29
26
this .extension = extension ;
30
27
}
28
+
31
29
public String getExtension () {
32
30
return extension ;
33
31
}
@@ -37,52 +35,64 @@ public static class ScannerUtil {
37
35
public static Scanner createScanner (InputStream inputStream ) {
38
36
return new Scanner (inputStream , StandardCharsets .UTF_8 );
39
37
}
38
+
40
39
public static Scanner createScanner (File file ) throws IOException {
41
40
return new Scanner (file , StandardCharsets .UTF_8 );
42
41
}
43
42
}
44
43
45
44
/**
46
- * Returns a Set of Paths for all the files in the specified directory.
47
- * <p>Equivalent to {@link FileOperations#listFiles(Path, FileExtension) listFiles(dir, FileExtension.ALL)}
45
+ * Returns a Set of Paths for all the files in the specified directory.
46
+ * <p>
47
+ * Equivalent to {@link FileOperations#listFiles(Path, FileExtension) listFiles(dir, FileExtension.ALL)}
48
+ *
49
+ * @param dir
50
+ * The path to the directory to list the files within
48
51
*
49
- * @param dir The path to the directory to list the files within
50
52
* @return A Set of Paths to each file within the directory
51
- * @throws IOException If the directory path is invalid or unable to be located
53
+ *
54
+ * @throws IOException
55
+ * If the directory path is invalid or unable to be located
56
+ *
52
57
* @see FileExtension#ALL
53
58
*/
54
59
public static Set <Path > listFiles (Path dir ) throws IOException {
55
60
try {
56
61
Set <Path > pathSet = listFiles (dir , FileExtension .ALL );
57
62
return pathSet ;
58
- }
59
- catch (IOException e ) {
63
+ } catch (IOException e ) {
60
64
throw e ;
61
65
}
62
66
}
63
67
64
68
/**
65
69
* Returns a Set of Paths for all the files in the specificed directory with the given extension.
66
- *
67
- * @param dir The path to the directory to list the files within.
68
- * @param extension A FileOperations.FileExtension to filter the Path results by.
70
+ *
71
+ * @param dir
72
+ * The path to the directory to list the files within.
73
+ * @param extension
74
+ * A FileOperations.FileExtension to filter the Path results by.
75
+ *
69
76
* @return A Set of Paths to each file within the directory with the extension.
70
- * @throws IOException If the directory path is invalid or unable to be located.
77
+ *
78
+ * @throws IOException
79
+ * If the directory path is invalid or unable to be located.
71
80
*/
72
81
public static Set <Path > listFiles (Path dir , FileExtension extension ) throws IOException {
73
82
Set <Path > pathSet = new HashSet <>();
74
83
dir = dir .normalize ();
75
- if (!Files .exists (dir )) throw new IOException ("The specified path, " + dir .toString () + ", was not found." );
84
+ if (!Files .exists (dir ))
85
+ throw new IOException ("The specified path, " + dir .toString () + ", was not found." );
76
86
try (DirectoryStream <Path > stream = Files .newDirectoryStream (dir )) {
77
87
for (Path path : stream ) {
78
88
String fileName = path .getFileName ().toString ();
79
- if (!Files .isDirectory (path ) && !FilePaths .IGNORED_FILES .contains (fileName ) && fileName .endsWith (extension .getExtension ())) {
89
+ if (!Files .isDirectory (path ) && !FilePaths .IGNORED_FILES .contains (fileName )
90
+ && fileName .endsWith (extension .getExtension ())) {
80
91
pathSet .add (dir .resolve (fileName ));
81
92
}
82
93
}
83
94
return pathSet ;
84
- }
85
- catch (IOException e ) {
95
+ } catch (IOException e ) {
86
96
System .out .println ("Error accessing directory: " + dir );
87
97
throw e ;
88
98
}
@@ -111,8 +121,7 @@ public static List<String> readFile(Path path) {
111
121
result .add (scanner .nextLine ());
112
122
}
113
123
return result ;
114
- }
115
- catch (IOException e ) {
124
+ } catch (IOException e ) {
116
125
e .printStackTrace ();
117
126
return null ;
118
127
}
@@ -121,6 +130,7 @@ public static List<String> readFile(Path path) {
121
130
public static void writeFile (String filename , String extension , Path destination , String content ) {
122
131
writeFile (filename , extension , destination , Collections .singletonList (content ));
123
132
}
133
+
124
134
public static void writeFile (String filename , String extension , Path destination , List <String > content ) {
125
135
Path filePath = destination .resolve (filename + extension );
126
136
File file = filePath .toFile ();
0 commit comments