file 工具

package com.broventure.re.files;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;


import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.util.Log;


public class FileUtility {


// 
public static String GetMD5(String filename) {
InputStream fis;
byte[] buffer = new byte[1024];
int numRead = 0;
MessageDigest md5;
try {
fis = new FileInputStream(filename);
md5 = MessageDigest.getInstance("MD5");
while ((numRead = fis.read(buffer)) > 0) {
md5.update(buffer, 0, numRead);
}
fis.close();
return toHexString(md5.digest());
} catch (Exception e) {
System.out.println("error");
return null;
}
}


public static long GetFileSize(String filePath) {
File file = new File(filePath);
return file.length();
}


private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };


public static String toHexString(byte[] b) { // String to byte
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i++) {
sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
sb.append(HEX_DIGITS[b[i] & 0x0f]);
}
return sb.toString();
}


// 将字符串转化为MD5
public String md5(String s) {
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest
.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();


return toHexString(messageDigest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}


return "";
}


public static long GetAssertFileSize(Context con, String fileName) {
long fileSize = -1;
AssetManager am = con.getAssets();
AssetFileDescriptor fd;
try {
fd = am.openFd(fileName);
fileSize = fd.getDeclaredLength();
if (fileSize == AssetFileDescriptor.UNKNOWN_LENGTH) {
fileSize = fd.getLength();
}
fd.close();
} catch (IOException e) {
e.printStackTrace();
}
am.close();
return fileSize;
}


// 文件是否存在,且文件大小一样
public static boolean IsFileExists(String filePath, long fileSize) {
File f = new File(filePath);
if (f.exists() && f.length() == fileSize) {
return true;
} else
return false;
}


// 拷贝文件
public static boolean CopyFile(InputStream is, OutputStream os) {
byte[] buffer = new byte[1000];
int readLength = -1;
try {
while ((readLength = is.read(buffer)) > 0) {
os.write(buffer, 0, readLength);
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}


/*****
* 拷贝数据库

* @return
*/
public static int CopyDBFromAssertToDBPath(Context con, String dbName,
boolean returnFalseWhenExists) {
File filePath = con.getDatabasePath(dbName);
if (returnFalseWhenExists) {
if (filePath.exists())
return -1;
}

String parentPathName = filePath.getParent();//
File parentPath = new File(parentPathName);
parentPath.mkdirs();


AssetManager am = con.getAssets();
InputStream assertIS = null;
FileOutputStream fos = null;
try {
assertIS = am.open(dbName);
fos = new FileOutputStream(filePath);
if (FileUtility.CopyFile(assertIS, fos)) {
Log.e(dbName, "CopyDBToData:Success");
} else {
Log.e(dbName, "CopyDBToData:Fail");
}


fos.close();
assertIS.close();


return 1;
} catch (IOException e) {
e.printStackTrace();


try {
if (fos != null)
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}


try {
if (assertIS != null)
assertIS.close();
} catch (IOException e1) {
e1.printStackTrace();
}


return 0;
}
}


public static ArrayList<String> GetAllFilesUnderFiles(Context con) {
File f = con.getFilesDir();
return GetAllFiles(f);
}


public static ArrayList<String> GetAllFiles(String filePath) {
File f = new File(filePath);
return GetAllFiles(f);
}


public static ArrayList<String> GetAllFiles(File f) {
if (f == null)
return null;


File[] files = f.listFiles();
if (files == null)
return null;


ArrayList<String> fileNames = new ArrayList<String>(files.length);
for (int i = 0; i < files.length; i++) {
fileNames.add(files[i].getPath());
}
return fileNames;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值