|
|
@@ -0,0 +1,347 @@
|
|
|
+package com.sheep.jiuyan.samllsheep.utils;
|
|
|
+
|
|
|
+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 android.content.Context;
|
|
|
+import android.os.Environment;
|
|
|
+import android.os.StatFs;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by d on 2016/11/10.
|
|
|
+ * Method:
|
|
|
+ * getSDCardPath()
|
|
|
+ * getSDCardTotal() 总容量
|
|
|
+ * getSDCardFree() 可用容量
|
|
|
+ * createSDDirection() 创建目录
|
|
|
+ *deleteSDDirection 删除目录
|
|
|
+ * isFileExist 文件是否存在
|
|
|
+ * deleteSDFile 删除文件
|
|
|
+ * renameSDFile 修改文件或目录
|
|
|
+ * copySDFileTo 拷贝单个文件
|
|
|
+ * copySDFilesTo 拷贝所有
|
|
|
+ * moveSDFileTo 移动单个
|
|
|
+ * moveSDFilesTo 移动多个
|
|
|
+ * creatDataDirection 建立私有目录
|
|
|
+ * deleteDataFile 删除私有文件
|
|
|
+ * deleteDataDir 删除私有目录
|
|
|
+ * renameDataFile 修改私有文件名
|
|
|
+ * copyDataFileTo 私有目录下负责
|
|
|
+ * moveDataFileTo 移动私有文件
|
|
|
+ * moveDataFilesTo移动私有目录下所有文件
|
|
|
+ * deleteFile 删除文件
|
|
|
+ * deleteDirection 删除目录
|
|
|
+ * copyFileTo 拷贝
|
|
|
+ * copyFilesTo
|
|
|
+ * moveFileTo 移动
|
|
|
+ * moveFilesTo
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class ClassFileHelper {
|
|
|
+ private static String SDPATH= Environment.getExternalStorageDirectory().getAbsolutePath();
|
|
|
+ public static String FILE_SUFFIX=".apk";
|
|
|
+ public static String DIR=Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"Sheep";
|
|
|
+ public static ClassFileHelper INSTANCE=new ClassFileHelper();
|
|
|
+
|
|
|
+ private ClassFileHelper( ){
|
|
|
+ }
|
|
|
+ public static ClassFileHelper getInstance( ){
|
|
|
+ return INSTANCE;
|
|
|
+ }
|
|
|
+
|
|
|
+ //表示SDCard存在并且可以读写
|
|
|
+ public boolean isSDCardState(){
|
|
|
+ if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
|
|
|
+ return true;
|
|
|
+ }else{
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String gbEncoding(final String gbString) {
|
|
|
+ char[] utfBytes = gbString.toCharArray(); //utfBytes = [测, 试]
|
|
|
+ String unicodeBytes = "";
|
|
|
+ for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
|
|
|
+ String hexB = Integer.toHexString(utfBytes[byteIndex]); //转换为16进制整型字符串
|
|
|
+ if (hexB.length() <= 2) {
|
|
|
+ hexB = "00" + hexB;
|
|
|
+ }
|
|
|
+ unicodeBytes = unicodeBytes + "\\u" + hexB;
|
|
|
+ }
|
|
|
+ System.out.println("unicodeBytes is: " + unicodeBytes);
|
|
|
+ return unicodeBytes;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在SD卡上创建目录
|
|
|
+ *
|
|
|
+ * 要创建的目录名
|
|
|
+ * @return 创建得到的目录
|
|
|
+ */
|
|
|
+ public void createSDDirection() {
|
|
|
+ String status = Environment.getExternalStorageState();
|
|
|
+ if (status.equals(Environment.MEDIA_MOUNTED)) {
|
|
|
+ File dir = new File(DIR);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除SD卡上的目录
|
|
|
+ *
|
|
|
+ * @param dirName
|
|
|
+ */
|
|
|
+ public boolean deleteSDDirection(String dirName) {
|
|
|
+ File dir = new File(SDPATH + dirName);
|
|
|
+ return deleteDirection(dir);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断文件是否已经存在
|
|
|
+ *
|
|
|
+ * @param fileName
|
|
|
+ * 要检查的文件名
|
|
|
+ * @return boolean, true表示存在,false表示不存在
|
|
|
+ */
|
|
|
+ public boolean isFileExist(String fileName) {
|
|
|
+ File file = new File(fileName);
|
|
|
+ return file.exists();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除SD卡上的文件
|
|
|
+ *
|
|
|
+ * @param fileName
|
|
|
+ */
|
|
|
+ public boolean deleteSDFile(String fileName) {
|
|
|
+ File file = new File(SDPATH + fileName);
|
|
|
+ if (file == null || !file.exists() || file.isDirectory())
|
|
|
+ return false;
|
|
|
+ file.delete();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改SD卡上的文件或目录名
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ */
|
|
|
+ public boolean renameSDFile(String oldfileName, String newFileName) {
|
|
|
+ File oleFile = new File(SDPATH + oldfileName);
|
|
|
+ File newFile = new File(SDPATH + newFileName);
|
|
|
+ return oleFile.renameTo(newFile);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝SD卡上的单个文件
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean copySDFileTo(String srcFileName, String destFileName) throws IOException {
|
|
|
+ File srcFile = new File(SDPATH + srcFileName);
|
|
|
+ File destFile = new File(SDPATH + destFileName);
|
|
|
+ return copyFileTo(srcFile, destFile);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝SD卡上指定目录的所有文件
|
|
|
+ *
|
|
|
+ * @param srcDirName
|
|
|
+ * @param destDirName
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public boolean copySDFilesTo(String srcDirName, String destDirName) throws IOException {
|
|
|
+ File srcDir = new File(SDPATH + srcDirName);
|
|
|
+ File destDir = new File(SDPATH + destDirName);
|
|
|
+ return copyFilesTo(srcDir, destDir);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移动SD卡上的单个文件
|
|
|
+ *
|
|
|
+ * @param srcFileName
|
|
|
+ * @param destFileName
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean moveSDFileTo(String srcFileName, String destFileName) throws IOException {
|
|
|
+ File srcFile = new File(SDPATH + srcFileName);
|
|
|
+ File destFile = new File(SDPATH + destFileName);
|
|
|
+ return moveFileTo(srcFile, destFile);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移动SD卡上的指定目录的所有文件
|
|
|
+ *
|
|
|
+ * @param srcDirName
|
|
|
+ * @param destDirName
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean moveSDFilesTo(String srcDirName, String destDirName) throws IOException {
|
|
|
+ File srcDir = new File(SDPATH + srcDirName);
|
|
|
+ File destDir = new File(SDPATH + destDirName);
|
|
|
+ return moveFilesTo(srcDir, destDir);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除一个文件
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean deleteFile(File file) {
|
|
|
+ if (file.isDirectory())
|
|
|
+ return false;
|
|
|
+ return file.delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除一个目录(可以是非空目录)
|
|
|
+ *
|
|
|
+ * @param dir
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean deleteDirection(File dir) {
|
|
|
+ if (dir == null || !dir.exists() || dir.isFile()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (File file : dir.listFiles()) {
|
|
|
+ if (file.isFile()) {
|
|
|
+ file.delete();
|
|
|
+ } else if (file.isDirectory()) {
|
|
|
+ deleteDirection(file);// 递归
|
|
|
+ }
|
|
|
+ }
|
|
|
+ dir.delete();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝一个文件,srcFile源文件,destFile目标文件
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean copyFileTo(File srcFile, File destFile) throws IOException {
|
|
|
+
|
|
|
+ if (srcFile.isDirectory() || destFile.isDirectory())
|
|
|
+ return false;// 判断是否是文件
|
|
|
+ FileInputStream fis = new FileInputStream(srcFile);
|
|
|
+ FileOutputStream fos = new FileOutputStream(destFile);
|
|
|
+ int readLen = 0;
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ while ((readLen = fis.read(buf)) != -1) {
|
|
|
+ fos.write(buf, 0, readLen);
|
|
|
+ }
|
|
|
+ fos.flush();
|
|
|
+ fos.close();
|
|
|
+ fis.close();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 拷贝目录下的所有文件到指定目录
|
|
|
+ *
|
|
|
+ * @param srcDir
|
|
|
+ * @param destDir
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean copyFilesTo(File srcDir, File destDir) throws IOException {
|
|
|
+
|
|
|
+ if (!srcDir.isDirectory() || !destDir.isDirectory())
|
|
|
+ return false;// 判断是否是目录
|
|
|
+ if (!destDir.exists())
|
|
|
+ return false;// 判断目标目录是否存在
|
|
|
+ File[] srcFiles = srcDir.listFiles();
|
|
|
+ for (int i = 0; i < srcFiles.length; i++) {
|
|
|
+ if (srcFiles[i].isFile()) {
|
|
|
+ // 获得目标文件
|
|
|
+ File destFile = new File(destDir.getPath() + "//"
|
|
|
+ + srcFiles[i].getName());
|
|
|
+ copyFileTo(srcFiles[i], destFile);
|
|
|
+ } else if (srcFiles[i].isDirectory()) {
|
|
|
+ File theDestDir = new File(destDir.getPath() + "//"
|
|
|
+ + srcFiles[i].getName());
|
|
|
+ copyFilesTo(srcFiles[i], theDestDir);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移动一个文件
|
|
|
+ *
|
|
|
+ * @param srcFile
|
|
|
+ * @param destFile
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean moveFileTo(File srcFile, File destFile) throws IOException {
|
|
|
+
|
|
|
+ boolean is_copy = copyFileTo(srcFile, destFile);
|
|
|
+
|
|
|
+ if (!is_copy)
|
|
|
+ return false;
|
|
|
+ deleteFile(srcFile);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移动目录下的所有文件到指定目录
|
|
|
+ *
|
|
|
+ * @param srcDir
|
|
|
+ * @param destDir
|
|
|
+ * @return
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean moveFilesTo(File srcDir, File destDir) throws IOException {
|
|
|
+ if (!srcDir.isDirectory() || !destDir.isDirectory()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ File[] srcDirFiles = srcDir.listFiles();
|
|
|
+ for (int i = 0; i < srcDirFiles.length; i++) {
|
|
|
+ if (srcDirFiles[i].isFile()) {
|
|
|
+ File oneDestFile = new File(destDir.getPath() + "//"
|
|
|
+ + srcDirFiles[i].getName());
|
|
|
+ moveFileTo(srcDirFiles[i], oneDestFile);
|
|
|
+ deleteFile(srcDirFiles[i]);
|
|
|
+ } else if (srcDirFiles[i].isDirectory()) {
|
|
|
+ File oneDestFile = new File(destDir.getPath() + "//"
|
|
|
+ + srcDirFiles[i].getName());
|
|
|
+ moveFilesTo(srcDirFiles[i], oneDestFile);
|
|
|
+ deleteDirection(srcDirFiles[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|