/**
* modified by ChaoMeng.
*/
package com.borqs.browser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
public class FileUtils {
// copy a file from srcFile to destFile, return true if succeed, return
// false if fail
public static boolean copyFile(File srcFile, File destFile) {
boolean result = false;
try {
InputStream in = new FileInputStream(srcFile);
try {
result = copyToFile(in, destFile);
} finally {
in.close();
}
} catch (IOException e) {
result = false;
}
return result;
}
/**
* Copy data from a source stream to destFile.
* Return true if succeed, return false if failed.
*/
public static boolean copyToFile(InputStream inputStream, File destFile) {
try {
if (destFile.exists()) {
destFile.delete();
}
FileOutputStream out = new FileOutputStream(destFile);
try {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) >= 0) {
out.write(buffer, 0, bytesRead);
}
} finally {
out.flush();
try {
out.getFD().sync();
} catch (IOException e) {
}
out.close();
}
return true;
} catch (IOException e) {
return false;
}
}
public static File getSharedPrefsFile(Context context, String name) {
// context.getApplication().getApplicationInfo().dataDir;
String dataFilePath = context.getApplicationContext().getApplicationInfo().dataDir;
String preferencesDir = dataFilePath + "/" + "shared_prefs";
return makeFilename(preferencesDir, name + ".xml");
}
public static File makeFilename(String base, String name) {
if (name.indexOf(File.separatorChar) < 0) {
return new File(base, name);
}
throw new IllegalArgumentException(
"File " + name + " contains a path separator");
}
}