package cn.com.basic.face.util;
|
|
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;
|
|
public class FileUtil {
|
|
|
public static File writeToFile(String fileName, byte[] fileBytes) {
|
try {
|
String dir = MainActivity.getInstance().getFilesDir().getAbsolutePath();
|
|
File file = new File(dir, fileName);
|
if (file.exists()) {
|
file.delete();
|
}
|
file.createNewFile();
|
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(dir, fileName)));
|
bos.write(fileBytes);
|
bos.flush();
|
bos.close();
|
return new File(dir, fileName);
|
}catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
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[]{};
|
}
|
|
|
}
|