12
12
import java .io .InputStreamReader ;
13
13
import java .io .Writer ;
14
14
import java .nio .file .Files ;
15
- import java .nio .file .Path ;
16
15
import java .nio .file .Paths ;
17
16
import java .util .ArrayList ;
18
17
import java .util .List ;
@@ -32,9 +31,13 @@ public static String getFileContent(String filePath) throws IOException {
32
31
line = br .readLine ();
33
32
}
34
33
return sb .toString ();
35
-
36
34
}
37
35
36
+
37
+ /**
38
+ * use new File(PathString).listfiles(File::isDirectory) instead
39
+ */
40
+ @ Deprecated
38
41
public static String [] getSubDirs (String rootDir ) {
39
42
40
43
File file = new File (rootDir );
@@ -53,12 +56,11 @@ public static String readFile2Str(String fpath) {
53
56
try {
54
57
content = new String (Files .readAllBytes (Paths .get (fpath )));
55
58
} catch (IOException e ) {
56
- System .out .printf ("%f not found!" , fpath );
59
+ System .out .printf ("%s not found!" , fpath );
57
60
e .printStackTrace ();
58
61
System .exit (0 );
59
62
}
60
63
return content ;
61
-
62
64
}
63
65
64
66
public static Boolean writeStr2File (String wStr , String fPath ) {
@@ -72,6 +74,33 @@ public static Boolean writeStr2File(String wStr, String fPath) {
72
74
return true ;
73
75
}
74
76
77
+ public static Boolean copyFile2Dir (File f , String DestDir ) {
78
+ try {
79
+ File DestDirFile = new File (DestDir );
80
+ if (!DestDirFile .exists ()) {
81
+ DestDirFile .mkdirs ();
82
+ }
83
+ String targetFilePath = DestDirFile .getAbsoluteFile () + "/" + f .getName ();
84
+ FileUtils .copyFile (f , new File (targetFilePath ));
85
+ } catch (IOException e ) {
86
+ // TODO Auto-generated catch block
87
+ e .printStackTrace ();
88
+ return false ;
89
+ }
90
+ return true ;
91
+ }
92
+
93
+ public static Boolean copyFile2File (File srcFile , File dstFile ) {
94
+ try {
95
+ FileUtils .copyFile (srcFile , dstFile );
96
+ } catch (IOException e ) {
97
+ // TODO Auto-generated catch block
98
+ e .printStackTrace ();
99
+ return false ;
100
+ }
101
+ return true ;
102
+ }
103
+
75
104
public static boolean deleteDirectory (String dirPath ) {
76
105
File directoryToBeDeleted = new File (dirPath );
77
106
File [] allContents = directoryToBeDeleted .listFiles ();
@@ -83,16 +112,6 @@ public static boolean deleteDirectory(String dirPath) {
83
112
return directoryToBeDeleted .delete ();
84
113
}
85
114
86
- public static void copyFolder (Path srcDirStr , Path destDirStr ) {
87
- File source = new File (srcDirStr .toString ());
88
- File dest = new File (destDirStr .toString ());
89
- try {
90
- FileUtils .copyDirectory (source , dest );
91
- } catch (IOException e ) {
92
- e .printStackTrace ();
93
- }
94
- }
95
-
96
115
public static void copyFolder (String srcDirStr , String destDirStr ) {
97
116
File source = new File (srcDirStr );
98
117
File dest = new File (destDirStr );
@@ -156,7 +175,13 @@ public static String readStringFromFile(String filePath) {
156
175
return null ;
157
176
}
158
177
159
- public static List <String > readFileToStrList (String filePath ) {
178
+ /**
179
+ * there is no \newline at the end of each line
180
+ *
181
+ * @param filePath
182
+ * @return
183
+ */
184
+ public static List <String > readFileToLineList (String filePath ) {
160
185
ArrayList <String > strList = new ArrayList <String >();
161
186
try {
162
187
if (!new File (filePath ).exists ()) {
@@ -202,21 +227,21 @@ public static boolean deleteDirectory(File directoryToBeDeleted) {
202
227
return directoryToBeDeleted .delete ();
203
228
}
204
229
205
- public static List <String > findFilePathofSpecifcTypeRecusive (String tarDir , String extension ) {
230
+ public static List <File > findFilePathofSpecifcTypeRecusive (String tarDir , String extension ) {
206
231
207
- List <String > pathList = new ArrayList <String >();
232
+ List <File > fileList = new ArrayList <File >();
208
233
try {
209
234
Files .walk (Paths .get (tarDir )).filter (Files ::isRegularFile ).forEach ((f ) -> {
210
235
String filepath = f .toString ();
211
236
if (filepath .endsWith (extension ))
212
237
// System.out.println(file + " found!");
213
- pathList .add (filepath );
238
+ fileList .add (new File ( filepath ) );
214
239
});
215
240
} catch (IOException e ) {
216
241
// TODO Auto-generated catch block
217
242
e .printStackTrace ();
218
243
}
219
- return pathList ;
244
+ return fileList ;
220
245
}
221
246
222
247
public static List <String > findRelativeFilePathofSpecifcTypeRecusive (String tarDir , String extension ) {
0 commit comments