mirror of
https://github.com/zhigang1992/react-native-code-push.git
synced 2026-06-12 17:08:58 +08:00
sort FileUtils methods
This commit is contained in:
@@ -17,6 +17,81 @@ public class FileUtils {
|
||||
|
||||
public static final int WRITE_BUFFER_SIZE = 1024 * 8;
|
||||
|
||||
public static void copyDirectoryContents(String sourceDirectoryPath, String destinationDirectoryPath) throws IOException {
|
||||
File sourceDir = new File(sourceDirectoryPath);
|
||||
File destDir = new File(destinationDirectoryPath);
|
||||
if (!destDir.exists()) {
|
||||
destDir.mkdir();
|
||||
}
|
||||
|
||||
for (File sourceFile : sourceDir.listFiles()) {
|
||||
if (sourceFile.isDirectory()) {
|
||||
copyDirectoryContents(
|
||||
CodePushUtils.appendPathComponent(sourceDirectoryPath, sourceFile.getName()),
|
||||
CodePushUtils.appendPathComponent(destinationDirectoryPath, sourceFile.getName()));
|
||||
} else {
|
||||
File destFile = new File(destDir, sourceFile.getName());
|
||||
FileInputStream fromFileStream = null;
|
||||
BufferedInputStream fromBufferedStream = null;
|
||||
FileOutputStream destStream = null;
|
||||
byte[] buffer = new byte[WRITE_BUFFER_SIZE];
|
||||
try {
|
||||
fromFileStream = new FileInputStream(sourceFile);
|
||||
fromBufferedStream = new BufferedInputStream(fromFileStream);
|
||||
destStream = new FileOutputStream(destFile);
|
||||
int bytesRead;
|
||||
while ((bytesRead = fromBufferedStream.read(buffer)) > 0) {
|
||||
destStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
if (fromFileStream != null) fromFileStream.close();
|
||||
if (fromBufferedStream != null) fromBufferedStream.close();
|
||||
if (destStream != null) destStream.close();
|
||||
} catch (IOException e) {
|
||||
throw new CodePushUnknownException("Error closing IO resources.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean createFolderAtPath(String filePath) {
|
||||
File file = new File(filePath);
|
||||
return file.mkdir();
|
||||
}
|
||||
|
||||
public static void deleteDirectory(File directory) {
|
||||
if (directory.exists()) {
|
||||
File[] files = directory.listFiles();
|
||||
if (files != null) {
|
||||
for (int i=0; i<files.length; i++) {
|
||||
if(files[i].isDirectory()) {
|
||||
deleteDirectory(files[i]);
|
||||
}
|
||||
else {
|
||||
files[i].delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
directory.delete();
|
||||
}
|
||||
|
||||
public static void deleteDirectoryAtPath(String directoryPath) {
|
||||
deleteDirectory(new File(directoryPath));
|
||||
}
|
||||
|
||||
public static void deleteFileAtPathSilently(String path) {
|
||||
deleteFileSilently(new File(path));
|
||||
}
|
||||
|
||||
public static void deleteFileSilently(File file) {
|
||||
if (!file.delete()) {
|
||||
CodePushUtils.log("Error deleting file " + file.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean fileAtPathExists(String filePath) {
|
||||
return new File(filePath).exists();
|
||||
}
|
||||
@@ -41,42 +116,6 @@ public class FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeStringToFile(String content, String filePath) throws IOException {
|
||||
PrintWriter out = null;
|
||||
try {
|
||||
out = new PrintWriter(filePath);
|
||||
out.print(content);
|
||||
} finally {
|
||||
if (out != null) out.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteDirectoryAtPath(String directoryPath) {
|
||||
deleteDirectory(new File(directoryPath));
|
||||
}
|
||||
|
||||
public static void deleteDirectory(File directory) {
|
||||
if (directory.exists()) {
|
||||
File[] files = directory.listFiles();
|
||||
if (files != null) {
|
||||
for (int i=0; i<files.length; i++) {
|
||||
if(files[i].isDirectory()) {
|
||||
deleteDirectory(files[i]);
|
||||
}
|
||||
else {
|
||||
files[i].delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
directory.delete();
|
||||
}
|
||||
|
||||
public static boolean createFolderAtPath(String filePath) {
|
||||
File file = new File(filePath);
|
||||
return file.mkdir();
|
||||
}
|
||||
|
||||
public static void unzipFile(File zipFile, String destination) throws IOException {
|
||||
FileInputStream fileStream = null;
|
||||
BufferedInputStream bufferedStream = null;
|
||||
@@ -130,52 +169,13 @@ public class FileUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyDirectoryContents(String sourceDirectoryPath, String destinationDirectoryPath) throws IOException {
|
||||
File sourceDir = new File(sourceDirectoryPath);
|
||||
File destDir = new File(destinationDirectoryPath);
|
||||
if (!destDir.exists()) {
|
||||
destDir.mkdir();
|
||||
}
|
||||
|
||||
for (File sourceFile : sourceDir.listFiles()) {
|
||||
if (sourceFile.isDirectory()) {
|
||||
copyDirectoryContents(
|
||||
CodePushUtils.appendPathComponent(sourceDirectoryPath, sourceFile.getName()),
|
||||
CodePushUtils.appendPathComponent(destinationDirectoryPath, sourceFile.getName()));
|
||||
} else {
|
||||
File destFile = new File(destDir, sourceFile.getName());
|
||||
FileInputStream fromFileStream = null;
|
||||
BufferedInputStream fromBufferedStream = null;
|
||||
FileOutputStream destStream = null;
|
||||
byte[] buffer = new byte[WRITE_BUFFER_SIZE];
|
||||
try {
|
||||
fromFileStream = new FileInputStream(sourceFile);
|
||||
fromBufferedStream = new BufferedInputStream(fromFileStream);
|
||||
destStream = new FileOutputStream(destFile);
|
||||
int bytesRead;
|
||||
while ((bytesRead = fromBufferedStream.read(buffer)) > 0) {
|
||||
destStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
if (fromFileStream != null) fromFileStream.close();
|
||||
if (fromBufferedStream != null) fromBufferedStream.close();
|
||||
if (destStream != null) destStream.close();
|
||||
} catch (IOException e) {
|
||||
throw new CodePushUnknownException("Error closing IO resources.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteFileAtPathSilently(String path) {
|
||||
deleteFileSilently(new File(path));
|
||||
}
|
||||
|
||||
public static void deleteFileSilently(File file) {
|
||||
if (!file.delete()) {
|
||||
CodePushUtils.log("Error deleting file " + file.getName());
|
||||
public static void writeStringToFile(String content, String filePath) throws IOException {
|
||||
PrintWriter out = null;
|
||||
try {
|
||||
out = new PrintWriter(filePath);
|
||||
out.print(content);
|
||||
} finally {
|
||||
if (out != null) out.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user