1. 程式人生 > 實用技巧 >對檔案拷貝、刪除操作

對檔案拷貝、刪除操作

public class FileUtis {
public static final File externalStorageDirectory = Environment.getExternalStorageDirectory();
public static String packageFilesDirectory = null;
public static String storagePath = null;
private static String mDefaultFolder = "libCGE";

public FileUtis() {
}

public static void setDefaultFolder(String defaultFolder) {
mDefaultFolder = defaultFolder;
}

public static String getPath() {
return getPath((Context)null);
}

public static String getPath(Context context) {
if (storagePath == null) {
storagePath = externalStorageDirectory.getAbsolutePath() + "/" + mDefaultFolder;
File file = new File(storagePath);
if (!file.exists() && !file.mkdirs()) {
storagePath = getPathInPackage(context, true);
}
}

return storagePath;
}

public static String getPathInPackage(Context context, boolean grantPermissions) {
if (context != null && packageFilesDirectory == null) {
String path = context.getFilesDir() + "/" + mDefaultFolder;
File file = new File(path);
if (!file.exists()) {
if (!file.mkdirs()) {
Log.e("libCGE_java", "在pakage目錄建立CGE臨時目錄失敗!");
return null;
}

if (grantPermissions) {
if (file.setExecutable(true, false)) {
Log.i("libCGE_java", "Package folder is executable");
}

if (file.setReadable(true, false)) {
Log.i("libCGE_java", "Package folder is readable");
}

if (file.setWritable(true, false)) {
Log.i("libCGE_java", "Package folder is writable");
}
}
}

packageFilesDirectory = path;
return packageFilesDirectory;
} else {
return packageFilesDirectory;
}
}

//將內容儲存在某個檔案裡面
public static void saveTextContent(String text, String filename) {
Log.i("libCGE_java", "Saving text : " + filename);

try {
FileOutputStream fileout = new FileOutputStream(filename);
fileout.write(text.getBytes());
fileout.flush();
fileout.close();
} catch (Exception var3) {
Log.e("libCGE_java", "Error: " + var3.getMessage());
}

}

//獲得某個問價裡面的內容
public static String getTextContent(String filename) {
Log.i("libCGE_java", "Reading text : " + filename);
if (filename == null) {
return null;
} else {
String content = "";
byte[] buffer = new byte[256];

try {
FileInputStream filein = new FileInputStream(filename);

while(true) {
int len = filein.read(buffer);
if (len <= 0) {
return content;
}

content = content + new String(buffer, 0, len);
}
} catch (Exception var5) {
Log.e("libCGE_java", "Error: " + var5.getMessage());
return null;
}
}
}

//將Assets目錄下的檔案拷貝到指定目錄
public static boolean copyAssetsFile(AssetManager assetManager, String src, String dst) {
if ((new File(dst)).exists()) {
return false;
} else {
try {
FileOutputStream outputStream = new FileOutputStream(dst);
InputStream inputStream = assetManager.open(src);
byte[] buffer = new byte[1024];

int count;
while((count = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, count);
}

inputStream.close();
outputStream.close();
return true;
} catch (FileNotFoundException var7) {
var7.printStackTrace();
Log.d("camera", "copy assets file failed:" + var7.getMessage());
return false;
} catch (IOException var8) {
Log.d("camera", "copy assets file failed:" + var8.getMessage());
var8.printStackTrace();
return false;
}
}
}

  
    //會在該應用下建立某個目錄  /storage/emulated/0/Android/data/com.example.myapplication(應用)/file
    public static String rootPath(Context context) {
File file = context.getExternalFilesDir((String)null);
if (file == null) {
file = context.getFilesDir(); 通過修改這個引數可以改變建立目錄的名稱
}

return file.getAbsolutePath();
}

//把資料把存到指定目錄
public static void saveDataToFile(String path, byte[] data) {
try {
try {
File file = new File(path);
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}

if (file.exists()) {
file.delete();
}

if (!file.exists()) {
boolean isok = file.createNewFile();
if (isok) {
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(data);
if (bos != null) {
bos.flush();
bos.close();
}
}
}
} catch (Exception var10) {
var10.printStackTrace();
}

} finally {
;
}
}


  //讀取某個檔案裡面的內容
public static byte[] readDataFromFile(String path) {
File file = new File(path);
BufferedInputStream bis = null;
FileInputStream fis = null;

try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
byte[] bytes = new byte[(int)file.length()];
bis.read(bytes);
if (bis != null) {
bis.close();
}

return bytes;
} catch (Exception var5) {
var5.printStackTrace();
return null;
}
}

  //刪除某個目錄
public static void deleteDir(String pPath) {
File dir = new File(pPath);
if (dir.exists() && dir.isFile()) {
dir.delete();
}

deleteDirWihtFile(dir);
}

  //刪除某個目錄的下的檔案
public static void deleteDirWihtFile(File dir) {
if (dir != null && dir.exists() && dir.isDirectory()) {
File[] var1 = dir.listFiles();
int var2 = var1.length;

for(int var3 = 0; var3 < var2; ++var3) {
File file = var1[var3];
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
deleteDirWihtFile(file);
}
}

dir.delete();
}
}
}