dupengyue
2017-07-20 3a5f09c61a87adb8dba2cc4a5366893886ba1c1d
VisitFace/DemoForBsk/app/src/main/java/cn/com/basic/face/util/FileUtil.java
@@ -3,6 +3,8 @@
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import cn.com.basic.face.base.MainActivity;
@@ -11,6 +13,19 @@
    public static File writeToFile(String fileName, byte[] fileBytes) {
        try {
            return writeToFile(fileName, fileBytes, 0, fileBytes.length);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static File writeToFile(String fileName, byte[] fileBytes, int offset, int size) {
        try {
            if (fileBytes == null) {
                fileBytes = new byte[]{};
            }
            String dir = MainActivity.getInstance().getFilesDir().getAbsolutePath();
            File file = new File(dir, fileName);
@@ -19,7 +34,7 @@
            }
            file.createNewFile();
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dir, fileName)));
            bos.write(fileBytes);
            bos.write(fileBytes, offset, size);
            bos.flush();
            bos.close();
            return new File(dir, fileName);
@@ -29,4 +44,49 @@
        return null;
    }
    public static File getFile(String shortFileName) {
        String dir = MainActivity.getInstance().getFilesDir().getAbsolutePath();
        return new File(dir, shortFileName);
    }
    public static byte[] readFile(File file) {
        // Open file
        RandomAccessFile f = null;
        try {
            f = new RandomAccessFile(file, "r");
            // Get and check length
            long longlength = f.length();
            int length = (int) longlength;
            if (length != longlength)
                throw new IOException("File size >= 2 GB");
            // Read file and return data
            byte[] data = new byte[length];
            f.readFully(data);
            return data;
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                f.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new byte[]{};
    }
    public static void deleteFile(String fileName) {
        try {
            String dir = MainActivity.getInstance().getFilesDir().getAbsolutePath();
            File file = new File(dir, fileName);
            if (file.exists()) {
                file.delete();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}