@@ -17,42 +17,48 @@ public class FileUtils {
1717
1818 public static final int WRITE_BUFFER_SIZE = 1024 * 8 ;
1919
20- public static boolean fileAtPathExists (String filePath ) {
21- return new File (filePath ).exists ();
22- }
23-
24- public static String readFileToString (String filePath ) throws IOException {
25- FileInputStream fin = null ;
26- BufferedReader reader = null ;
27- try {
28- File fl = new File (filePath );
29- fin = new FileInputStream (fl );
30- reader = new BufferedReader (new InputStreamReader (fin ));
31- StringBuilder sb = new StringBuilder ();
32- String line = null ;
33- while ((line = reader .readLine ()) != null ) {
34- sb .append (line ).append ("\n " );
35- }
36-
37- return sb .toString ();
38- } finally {
39- if (reader != null ) reader .close ();
40- if (fin != null ) fin .close ();
20+ public static void copyDirectoryContents (String sourceDirectoryPath , String destinationDirectoryPath ) throws IOException {
21+ File sourceDir = new File (sourceDirectoryPath );
22+ File destDir = new File (destinationDirectoryPath );
23+ if (!destDir .exists ()) {
24+ destDir .mkdir ();
4125 }
42- }
4326
44- public static void writeStringToFile (String content , String filePath ) throws IOException {
45- PrintWriter out = null ;
46- try {
47- out = new PrintWriter (filePath );
48- out .print (content );
49- } finally {
50- if (out != null ) out .close ();
27+ for (File sourceFile : sourceDir .listFiles ()) {
28+ if (sourceFile .isDirectory ()) {
29+ copyDirectoryContents (
30+ CodePushUtils .appendPathComponent (sourceDirectoryPath , sourceFile .getName ()),
31+ CodePushUtils .appendPathComponent (destinationDirectoryPath , sourceFile .getName ()));
32+ } else {
33+ File destFile = new File (destDir , sourceFile .getName ());
34+ FileInputStream fromFileStream = null ;
35+ BufferedInputStream fromBufferedStream = null ;
36+ FileOutputStream destStream = null ;
37+ byte [] buffer = new byte [WRITE_BUFFER_SIZE ];
38+ try {
39+ fromFileStream = new FileInputStream (sourceFile );
40+ fromBufferedStream = new BufferedInputStream (fromFileStream );
41+ destStream = new FileOutputStream (destFile );
42+ int bytesRead ;
43+ while ((bytesRead = fromBufferedStream .read (buffer )) > 0 ) {
44+ destStream .write (buffer , 0 , bytesRead );
45+ }
46+ } finally {
47+ try {
48+ if (fromFileStream != null ) fromFileStream .close ();
49+ if (fromBufferedStream != null ) fromBufferedStream .close ();
50+ if (destStream != null ) destStream .close ();
51+ } catch (IOException e ) {
52+ throw new CodePushUnknownException ("Error closing IO resources." , e );
53+ }
54+ }
55+ }
5156 }
5257 }
5358
54- public static void deleteDirectoryAtPath (String directoryPath ) {
55- deleteDirectory (new File (directoryPath ));
59+ public static boolean createFolderAtPath (String filePath ) {
60+ File file = new File (filePath );
61+ return file .mkdir ();
5662 }
5763
5864 public static void deleteDirectory (File directory ) {
@@ -72,9 +78,42 @@ public static void deleteDirectory(File directory) {
7278 directory .delete ();
7379 }
7480
75- public static boolean createFolderAtPath (String filePath ) {
76- File file = new File (filePath );
77- return file .mkdir ();
81+ public static void deleteDirectoryAtPath (String directoryPath ) {
82+ deleteDirectory (new File (directoryPath ));
83+ }
84+
85+ public static void deleteFileAtPathSilently (String path ) {
86+ deleteFileSilently (new File (path ));
87+ }
88+
89+ public static void deleteFileSilently (File file ) {
90+ if (!file .delete ()) {
91+ CodePushUtils .log ("Error deleting file " + file .getName ());
92+ }
93+ }
94+
95+ public static boolean fileAtPathExists (String filePath ) {
96+ return new File (filePath ).exists ();
97+ }
98+
99+ public static String readFileToString (String filePath ) throws IOException {
100+ FileInputStream fin = null ;
101+ BufferedReader reader = null ;
102+ try {
103+ File fl = new File (filePath );
104+ fin = new FileInputStream (fl );
105+ reader = new BufferedReader (new InputStreamReader (fin ));
106+ StringBuilder sb = new StringBuilder ();
107+ String line = null ;
108+ while ((line = reader .readLine ()) != null ) {
109+ sb .append (line ).append ("\n " );
110+ }
111+
112+ return sb .toString ();
113+ } finally {
114+ if (reader != null ) reader .close ();
115+ if (fin != null ) fin .close ();
116+ }
78117 }
79118
80119 public static void unzipFile (File zipFile , String destination ) throws IOException {
@@ -130,52 +169,13 @@ public static void unzipFile(File zipFile, String destination) throws IOExceptio
130169 }
131170 }
132171
133- public static void copyDirectoryContents (String sourceDirectoryPath , String destinationDirectoryPath ) throws IOException {
134- File sourceDir = new File (sourceDirectoryPath );
135- File destDir = new File (destinationDirectoryPath );
136- if (!destDir .exists ()) {
137- destDir .mkdir ();
138- }
139-
140- for (File sourceFile : sourceDir .listFiles ()) {
141- if (sourceFile .isDirectory ()) {
142- copyDirectoryContents (
143- CodePushUtils .appendPathComponent (sourceDirectoryPath , sourceFile .getName ()),
144- CodePushUtils .appendPathComponent (destinationDirectoryPath , sourceFile .getName ()));
145- } else {
146- File destFile = new File (destDir , sourceFile .getName ());
147- FileInputStream fromFileStream = null ;
148- BufferedInputStream fromBufferedStream = null ;
149- FileOutputStream destStream = null ;
150- byte [] buffer = new byte [WRITE_BUFFER_SIZE ];
151- try {
152- fromFileStream = new FileInputStream (sourceFile );
153- fromBufferedStream = new BufferedInputStream (fromFileStream );
154- destStream = new FileOutputStream (destFile );
155- int bytesRead ;
156- while ((bytesRead = fromBufferedStream .read (buffer )) > 0 ) {
157- destStream .write (buffer , 0 , bytesRead );
158- }
159- } finally {
160- try {
161- if (fromFileStream != null ) fromFileStream .close ();
162- if (fromBufferedStream != null ) fromBufferedStream .close ();
163- if (destStream != null ) destStream .close ();
164- } catch (IOException e ) {
165- throw new CodePushUnknownException ("Error closing IO resources." , e );
166- }
167- }
168- }
169- }
170- }
171-
172- public static void deleteFileAtPathSilently (String path ) {
173- deleteFileSilently (new File (path ));
174- }
175-
176- public static void deleteFileSilently (File file ) {
177- if (!file .delete ()) {
178- CodePushUtils .log ("Error deleting file " + file .getName ());
172+ public static void writeStringToFile (String content , String filePath ) throws IOException {
173+ PrintWriter out = null ;
174+ try {
175+ out = new PrintWriter (filePath );
176+ out .print (content );
177+ } finally {
178+ if (out != null ) out .close ();
179179 }
180180 }
181181}
0 commit comments