11package com .blankj .utilcode .utils ;
22
3+ import android .text .TextUtils ;
4+
5+ import java .io .BufferedReader ;
36import java .io .File ;
7+ import java .io .FileInputStream ;
8+ import java .io .FileNotFoundException ;
9+ import java .io .FileOutputStream ;
10+ import java .io .FileWriter ;
11+ import java .io .IOException ;
12+ import java .io .InputStream ;
13+ import java .io .InputStreamReader ;
14+ import java .io .OutputStream ;
15+ import java .util .ArrayList ;
16+ import java .util .List ;
417
518/**
619 * <pre>
@@ -16,19 +29,243 @@ private FileUtils() {
1629 throw new UnsupportedOperationException ("u can't fuck me..." );
1730 }
1831
19- public static File getFile (File directory , String ... names ) {
20- if (directory == null ) {
21- throw new NullPointerException (
22- "directorydirectory must not be null" );
32+
33+ public static StringBuilder readFile (String filePath , String charsetName ) {
34+ File file = new File (filePath );
35+ if (!file .isFile ()) return null ;
36+ return readFile (file , charsetName );
37+ }
38+
39+ public static StringBuilder readFile (File file , String charsetName ) {
40+ StringBuilder sb = new StringBuilder ("" );
41+ BufferedReader reader = null ;
42+ try {
43+ InputStreamReader is = new InputStreamReader (new FileInputStream (file ), charsetName );
44+ reader = new BufferedReader (is );
45+ String line ;
46+ while ((line = reader .readLine ()) != null ) {
47+ if (!sb .toString ().equals ("" )) {
48+ sb .append ("\r \n " );
49+ }
50+ sb .append (line );
51+ }
52+ reader .close ();
53+ return sb ;
54+ } catch (IOException e ) {
55+ throw new RuntimeException ("IOException occurred. " , e );
56+ } finally {
57+ if (reader != null ) {
58+ try {
59+ reader .close ();
60+ } catch (IOException e ) {
61+ e .printStackTrace ();
62+ }
63+ }
2364 }
24- if (names == null ) {
25- throw new NullPointerException ("names must not be null" );
65+ }
66+
67+ public static boolean writeFile (String filePath , String content , boolean append ) {
68+ if (StringUtils .isSpace (content )) return false ;
69+ FileWriter fileWriter = null ;
70+ try {
71+ makeDirs (filePath );
72+ fileWriter = new FileWriter (filePath , append );
73+ fileWriter .write (content );
74+ return true ;
75+ } catch (IOException e ) {
76+ throw new RuntimeException ("IOException occurred. " , e );
77+ } finally {
78+ if (fileWriter != null ) {
79+ try {
80+ fileWriter .close ();
81+ } catch (IOException e ) {
82+ e .printStackTrace ();
83+ }
84+ }
2685 }
27- File file = directory ;
28- for (String name : names ) {
29- file = new File (file , name );
86+ }
87+
88+ public static boolean writeFile (String filePath , String content ) {
89+ return writeFile (filePath , content , false );
90+ }
91+
92+ public static boolean writeFile (String filePath , InputStream stream ) {
93+ return writeFile (filePath , stream , false );
94+ }
95+
96+
97+ public static boolean writeFile (String filePath , InputStream stream , boolean append ) {
98+ return writeFile (filePath != null ? new File (filePath ) : null , stream , append );
99+ }
100+
101+ public static boolean writeFile (File file , InputStream stream ) {
102+ return writeFile (file , stream , false );
103+ }
104+
105+ public static boolean writeFile (File file , InputStream stream , boolean append ) {
106+ OutputStream o = null ;
107+ try {
108+ makeDirs (file .getAbsolutePath ());
109+ o = new FileOutputStream (file , append );
110+ byte data [] = new byte [1024 ];
111+ int length ;
112+ while ((length = stream .read (data )) != -1 ) {
113+ o .write (data , 0 , length );
114+ }
115+ o .flush ();
116+ return true ;
117+ } catch (FileNotFoundException e ) {
118+ throw new RuntimeException ("FileNotFoundException occurred. " , e );
119+ } catch (IOException e ) {
120+ throw new RuntimeException ("IOException occurred. " , e );
121+ } finally {
122+ if (o != null ) {
123+ try {
124+ o .close ();
125+ stream .close ();
126+ } catch (IOException e ) {
127+ e .printStackTrace ();
128+ }
129+ }
130+ }
131+ }
132+
133+ public static void moveFile (String sourceFilePath , String destFilePath ) {
134+ if (TextUtils .isEmpty (sourceFilePath ) || TextUtils .isEmpty (destFilePath )) {
135+ throw new RuntimeException ("Both sourceFilePath and destFilePath cannot be null." );
136+ }
137+ moveFile (new File (sourceFilePath ), new File (destFilePath ));
138+ }
139+
140+ public static void moveFile (File srcFile , File destFile ) {
141+ boolean rename = srcFile .renameTo (destFile );
142+ if (!rename ) {
143+ copyFile (srcFile .getAbsolutePath (), destFile .getAbsolutePath ());
144+ deleteFile (srcFile .getAbsolutePath ());
145+ }
146+ }
147+
148+ public static boolean copyFile (String sourceFilePath , String destFilePath ) {
149+ InputStream inputStream = null ;
150+ try {
151+ inputStream = new FileInputStream (sourceFilePath );
152+ } catch (FileNotFoundException e ) {
153+ throw new RuntimeException ("FileNotFoundException occurred. " , e );
30154 }
31- return file ;
155+ return writeFile ( destFilePath , inputStream ) ;
32156 }
33157
158+
159+ public static List <String > readFileToList (String filePath , String charsetName ) {
160+ File file = new File (filePath );
161+ if (!file .isFile ()) return null ;
162+ List <String > fileContent = new ArrayList <>();
163+ BufferedReader reader = null ;
164+ try {
165+ InputStreamReader is = new InputStreamReader (new FileInputStream (file ), charsetName );
166+ reader = new BufferedReader (is );
167+ String line ;
168+ while ((line = reader .readLine ()) != null ) {
169+ fileContent .add (line );
170+ }
171+ reader .close ();
172+ return fileContent ;
173+ } catch (IOException e ) {
174+ throw new RuntimeException ("IOException occurred. " , e );
175+ } finally {
176+ if (reader != null ) {
177+ try {
178+ reader .close ();
179+ } catch (IOException e ) {
180+ e .printStackTrace ();
181+ }
182+ }
183+ }
184+ }
185+
186+ public static String getFolderName (String filePath ) {
187+ if (StringUtils .isSpace (filePath )) return filePath ;
188+ int lastSep = filePath .lastIndexOf (File .separator );
189+ return lastSep == -1 ? "" : filePath .substring (0 , lastSep );
190+ }
191+
192+ public static String getFileName (String filePath ) {
193+ if (StringUtils .isSpace (filePath )) return filePath ;
194+ int lastSep = filePath .lastIndexOf (File .separator );
195+ return lastSep == -1 ? filePath : filePath .substring (lastSep + 1 );
196+ }
197+
198+ public static String getFileNameWithoutExtension (String filePath ) {
199+ if (StringUtils .isSpace (filePath )) return filePath ;
200+ int lastPoi = filePath .lastIndexOf ('.' );
201+ int lastSep = filePath .lastIndexOf (File .separator );
202+ if (lastSep == -1 ) {
203+ return (lastPoi == -1 ? filePath : filePath .substring (0 , lastPoi ));
204+ }
205+ if (lastPoi == -1 || lastSep > lastPoi ) {
206+ return filePath .substring (lastSep + 1 );
207+ }
208+ return filePath .substring (lastSep + 1 , lastPoi );
209+ }
210+
211+
212+ public static String getFileExtension (String filePath ) {
213+ if (StringUtils .isSpace (filePath )) return filePath ;
214+ int lastPoi = filePath .lastIndexOf ('.' );
215+ int lastSep = filePath .lastIndexOf (File .separator );
216+ if (lastPoi == -1 || lastSep >= lastPoi ) return "" ;
217+ return filePath .substring (lastPoi + 1 );
218+ }
219+
220+
221+ public static boolean makeDirs (String filePath ) {
222+ String folderName = getFolderName (filePath );
223+ if (StringUtils .isEmpty (folderName )) return false ;
224+ File folder = new File (folderName );
225+ return (folder .exists () && folder .isDirectory ()) || folder .mkdirs ();
226+ }
227+
228+ public static boolean makeFolders (String filePath ) {
229+ return makeDirs (filePath );
230+ }
231+
232+ public static boolean isFileExist (String filePath ) {
233+ if (StringUtils .isSpace (filePath )) return false ;
234+ File file = new File (filePath );
235+ return file .exists () && file .isFile ();
236+ }
237+
238+ public static boolean isFolderExist (String directoryPath ) {
239+ if (StringUtils .isSpace (directoryPath )) return false ;
240+ File dire = new File (directoryPath );
241+ return dire .exists () && dire .isDirectory ();
242+ }
243+
244+ public static boolean deleteFile (String path ) {
245+ if (StringUtils .isSpace (path )) return true ;
246+ File file = new File (path );
247+ if (!file .exists ()) {
248+ return true ;
249+ }
250+ if (file .isFile ()) {
251+ return file .delete ();
252+ }
253+ if (!file .isDirectory ()) {
254+ return false ;
255+ }
256+ for (File f : file .listFiles ()) {
257+ if (f .isFile ()) {
258+ f .delete ();
259+ } else if (f .isDirectory ()) {
260+ deleteFile (f .getAbsolutePath ());
261+ }
262+ }
263+ return file .delete ();
264+ }
265+
266+ public static long getFileSize (String path ) {
267+ if (StringUtils .isSpace (path )) return -1 ;
268+ File file = new File (path );
269+ return (file .exists () && file .isFile () ? file .length () : -1 );
270+ }
34271}
0 commit comments