1
1
package javaToolkit .lib .utils ;
2
2
3
3
import java .io .BufferedReader ;
4
- import java .io .BufferedWriter ;
5
4
import java .io .File ;
6
5
import java .io .FileInputStream ;
7
6
import java .io .FileNotFoundException ;
8
7
import java .io .FileReader ;
9
- import java .io .FileWriter ;
10
8
import java .io .FilenameFilter ;
11
9
import java .io .IOException ;
12
10
import java .io .InputStreamReader ;
13
- import java .io .Writer ;
14
11
import java .nio .file .Files ;
15
12
import java .nio .file .Path ;
16
13
import java .nio .file .Paths ;
@@ -52,10 +49,10 @@ public boolean accept(File current, String name) {
52
49
return directories ;
53
50
}
54
51
55
- public static String readFile2Str (String fpath ) {
52
+ public static String readFile2Str (Path fpath ) {
56
53
String content = "" ;
57
54
try {
58
- content = new String (Files .readAllBytes (Paths . get ( fpath ) ));
55
+ content = new String (Files .readAllBytes (fpath ));
59
56
} catch (IOException e ) {
60
57
System .out .printf ("%s not found!" , fpath );
61
58
e .printStackTrace ();
@@ -127,6 +124,22 @@ public static Boolean copyFile2Dir(File f, String DestDir) {
127
124
return true ;
128
125
}
129
126
127
+ public static Boolean copyFile2Dir (Path fPath , Path DestDir ) {
128
+ try {
129
+ File DestDirFile = DestDir .toFile ();
130
+ if (!DestDirFile .exists ()) {
131
+ DestDirFile .mkdirs ();
132
+ }
133
+ Path targetFilePath = Paths .get (DestDirFile .getAbsolutePath (), fPath .toFile ().getName ());
134
+ FileUtils .copyFile (fPath .toFile (), targetFilePath .toFile ());
135
+ } catch (IOException e ) {
136
+
137
+ e .printStackTrace ();
138
+ return false ;
139
+ }
140
+ return true ;
141
+ }
142
+
130
143
public static Boolean copyFile2File (File srcFile , File dstFile ) {
131
144
try {
132
145
// dstfile will be overwrited if exist
@@ -139,50 +152,27 @@ public static Boolean copyFile2File(File srcFile, File dstFile) {
139
152
return true ;
140
153
}
141
154
142
- public static boolean deleteDirectory (String dirPath ) {
143
- File directoryToBeDeleted = new File ( dirPath );
155
+ public static boolean deleteDirectory (Path dirPath ) {
156
+ File directoryToBeDeleted = dirPath . toFile ( );
144
157
File [] allContents = directoryToBeDeleted .listFiles ();
145
158
if (allContents != null ) {
146
159
for (File file : allContents ) {
147
- deleteDirectory (file .getAbsolutePath ());
160
+ deleteDirectory (file .toPath ());
148
161
}
149
162
}
150
163
return directoryToBeDeleted .delete ();
151
164
}
152
165
153
- public static void copyFolder ( String srcDirStr , String destDirStr ) {
154
- File source = new File ( srcDirStr );
155
- File dest = new File ( destDirStr );
166
+ public static void copyDirectory ( Path srcDirPath , Path destDirPath ) {
167
+ File source = srcDirPath . toFile ( );
168
+ File dest = destDirPath . toFile ( );
156
169
try {
157
170
FileUtils .copyDirectory (source , dest );
158
171
} catch (IOException e ) {
159
172
e .printStackTrace ();
160
173
}
161
174
}
162
175
163
- public static void writeStringToFile (String filePath , String dataStr ) {
164
- BufferedWriter bufferedWriter = null ;
165
- try {
166
- File file = new File (filePath );
167
- if (!file .exists ()) {
168
- file .createNewFile ();
169
- }
170
- Writer writer = new FileWriter (file );
171
- bufferedWriter = new BufferedWriter (writer );
172
- bufferedWriter .write (dataStr );
173
- } catch (IOException e ) {
174
- e .printStackTrace ();
175
- } finally {
176
- try {
177
- if (bufferedWriter != null )
178
- bufferedWriter .close ();
179
- } catch (Exception e ) {
180
- System .out .println ("Write file error!" );
181
- e .printStackTrace ();
182
- }
183
- }
184
- }
185
-
186
176
public static String readStringFromFile (String filePath ) {
187
177
try {
188
178
File file = new File (filePath );
@@ -219,14 +209,14 @@ public static String readStringFromFile(String filePath) {
219
209
* @param filePath
220
210
* @return
221
211
*/
222
- public static List <String > readFileToLineList (String filePath ) {
212
+ public static List <String > readFileToLineList (Path filePath ) {
223
213
ArrayList <String > strList = new ArrayList <String >();
224
214
try {
225
- if (!new File ( filePath ).exists ()) {
215
+ if (!filePath . toFile ( ).exists ()) {
226
216
System .out .println ("File not exists! " + filePath );
227
217
return null ;
228
218
}
229
- BufferedReader reader = new BufferedReader (new FileReader (filePath ));
219
+ BufferedReader reader = new BufferedReader (new FileReader (filePath . toString () ));
230
220
String line = reader .readLine ();
231
221
while (line != null ) {
232
222
strList .add (line );
@@ -247,7 +237,7 @@ public static List<String> readFileToLineList(String filePath) {
247
237
public static void createFolder (String dirStr , Boolean deleteIfExist ) {
248
238
File dir = new File (dirStr );
249
239
if (deleteIfExist && dir .exists ()) {
250
- deleteDirectory (dir .getAbsolutePath ());
240
+ deleteDirectory (dir .toPath ());
251
241
dir .mkdirs ();
252
242
} else if (!dir .exists ()) {
253
243
dir .mkdirs ();
@@ -264,11 +254,11 @@ public static boolean deleteDirectory(File directoryToBeDeleted) {
264
254
return directoryToBeDeleted .delete ();
265
255
}
266
256
267
- public static List <File > findFilePathofSpecifcTypeRecusive (String tarDir , String extension ) {
257
+ public static List <File > findFilePathofSpecifcTypeRecusive (Path tarDir , String extension ) {
268
258
269
259
List <File > fileList = new ArrayList <File >();
270
260
try {
271
- Files .walk (Paths . get ( tarDir ) ).filter (Files ::isRegularFile ).forEach ((f ) -> {
261
+ Files .walk (tarDir ).filter (Files ::isRegularFile ).forEach ((f ) -> {
272
262
String filepath = f .toString ();
273
263
if (filepath .endsWith (extension ))
274
264
// System.out.println(file + " found!");
0 commit comments