今天公司搬家,庆祝公司乔迁之喜顺便更一下工具类
public final class FileUtils {
public static final String APP_NAME = "xxx";//app名称
public static final String BASE_DIR = APP_NAME + File.separator;//WildmaIDCardCamera/
public static final String DIR_ROOT = FileUtils.getRootPath() + File.separator + APP_NAME + File.separator;//文件夹根目录 /storage/emulated/0/debiao/
/**
* 删除单个文件
*
* @param filePath$Name 要删除的文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteSingleFile(String filePath$Name) {
File file = new File(filePath$Name);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if (file.exists() && file.isFile()) {
if (file.delete()) {
Log.e("--Method--", "Copy_Delete.deleteSingleFile: 删除单个文件" + filePath$Name + "成功!");
return true;
} else {
// Toast.makeText(getApplicationContext(), "删除单个文件" + filePath$Name + "失败!", Toast.LENGTH_SHORT).show();
return false;
}
} else {
// Toast.makeText(getApplicationContext(), "删除单个文件失败:" + filePath$Name + "不存在!", Toast.LENGTH_SHORT).show();
return false;
}
}
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
public static void CopyFile(File src, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
File destSecond = null;
if (dest.isDirectory()) { //如果目标是一个文件夹,那么就在该dest文件夹下建立一个新的文件类型
destSecond = new File(dest, src.getName()); //这一步如何制定文件的格式,因为单独的文件拷贝要指定双方的文件类型
//实现文件拷文件夹中去
os = new FileOutputStream(destSecond); //实现拷文件操作
} else {
//如果是个文件的话,直接拷贝
os = new FileOutputStream(dest);
}
try {
is = new FileInputStream(src);
byte[] by = new byte[1024];//准备车子
int len = 0; //假设车上没有人
try {
while (-1 != (len = is.read(by))) { //开始慢慢下车和上车
os.write(by, 0, len); //下车
}
} catch (IOException e) {
e.printStackTrace();
} finally {
os.flush();
is.close();
os.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件未找到");
}
}
public static byte[] getBlock(long offset, File file, int blockSize) throws Exception {
byte[] result = new byte[blockSize];
RandomAccessFile accessFile = null;
try {
accessFile = new RandomAccessFile(file, "rwd");
// accessFile.skipBytes((int) offset);
accessFile.seek(offset);
// Log.e("access", accessFile.length() + "");
int readSize = accessFile.read(result);
if (readSize == -1) {
return null;
} else
if (readSize == blockSize) {
return result;
} else {
byte[] tmpByte = new byte[readSize];
System.arraycopy(result, 0, tmpByte, 0, readSize);
return tmpByte;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (accessFile != null) {
try {
accessFile.close();
} catch (IOException e1) {
}
}
}
return null;
}
/**
* 得到SD卡根目录,SD卡不可用则获取内部存储的根目录
*/
public static File getRootPath() {
File path = null;
if (sdCardIsAvailable()) {
path = Environment.getExternalStorageDirectory(); //SD卡根目录 /storage/emulated/0
} else {
path = Environment.getDataDirectory();//内部存储的根目录 /data
}
return path;
}
/**
* SD卡是否可用
*/
public static boolean sdCardIsAvailable() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File sd = new File(Environment.getExternalStorageDirectory().getPath());
return sd.canWrite();
} else
return false;
}
/**
* 判断目录是否存在,不存在则判断是否创建成功
*
* @param dirPath 文件路径
* @return {@code true}: 存在或创建成功<br>{@code false}: 不存在或创建失败
*/
public static boolean createOrExistsDir(String dirPath) {
return createOrExistsDir(getFileByPath(dirPath));
}
/**
* 判断目录是否存在,不存在则判断是否创建成功
*
* @param file 文件
* @return {@code true}: 存在或创建成功<br>{@code false}: 不存在或创建失败
*/
public static boolean createOrExistsDir(File file) {
// 如果存在,是目录则返回true,是文件则返回false,不存在则返回是否创建成功
return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());
}
/**
* 判断文件是否存在,不存在则判断是否创建成功
*
* @param filePath 文件路径
* @return {@code true}: 存在或创建成功<br>{@code false}: 不存在或创建失败
*/
public static boolean createOrExistsFile(String filePath) {
return createOrExistsFile(getFileByPath(filePath));
}
/**
* 判断文件是否存在,不存在则判断是否创建成功
*
* @param file 文件
* @return {@code true}: 存在或创建成功<br>{@code false}: 不存在或创建失败
*/
public static boolean createOrExistsFile(File file) {
if (file == null)
return false;
// 如果存在,是文件则返回true,是目录则返回false
if (file.exists())
return file.isFile();
if (!createOrExistsDir(file.getParentFile()))
return false;
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 根据文件路径获取文件
*
* @param filePath 文件路径
* @return 文件
*/
public static File getFileByPath(String filePath) {
return isSpace(filePath) ? null : new File(filePath);
}
/**
* 判断字符串是否为 null 或全为空白字符
*
* @param s
* @return
*/
private static boolean isSpace(final String s) {
if (s == null)
return true;
for (int i = 0, len = s.length(); i < len; ++i) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}
return true;
}
/**
* 关闭IO
*
* @param closeables closeable
*/
public static void closeIO(Closeable... closeables) {
if (closeables == null)
return;
try {
for (Closeable closeable : closeables) {
if (closeable != null) {
closeable.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}