android 文件操作
package com.gcg.egis.utility;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileHelper {
/**
* 删除指定的文件
* @param fileName
*/
public static void deleteFile(String fileName){
File file = new File(fileName);
if(file.isDirectory()){
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
new File(fileName,files[i]).delete();
}
}else{
file.delete();
}
}
/**
* 保存指定的data内容到指定的文件中
* @param data
* @param fileName
*/
public static void saveFile(byte[] data,String fileName){
OutputStream os=null;
try {
File dir = new File(PARAM.SDCARD_PATH);
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(fileName);
if(!file.exists())
file.createNewFile();
os = new FileOutputStream(file);
os.write(data, 0, data.length);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(os!=null){
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static byte[] readImageByte(String imagePath){
byte[] tmp = new byte[4096];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
InputStream in = null;
try {
in = new FileInputStream(imagePath);
int len;
while((len = in.read(tmp)) != -1){
buffer.write(tmp,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in != null)
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer.toByteArray();
}
}
摘自 android小益的专栏
补充:移动开发 , Android ,