您的位置:首页 > 移动开发 > Android开发

Android 文件处理

2016-12-05 00:00 281 查看
public static void copy(InputStream fis, OutputStream fos) throws IOException{
try{
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf))>0){
fos.write(buf, 0, len);
}
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos !=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static byte[] getBytes(InputStream fis) throws IOException{
ByteArrayOutputStream fos = null;
byte[] data = null;
try{
fos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf))>0){
fos.write(buf, 0, len);
}
return fos.toByteArray();
}finally{
if(fos !=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static boolean saveBitmap(Bitmap bmp, String filePath){
if(bmp == null){
return false;
}
File f = new File(filePath);
FileOutputStream fos = null;
try{
f.delete();
f.createNewFile();
fos = new FileOutputStream(f);
bmp.compress(CompressFormat.PNG, 100, fos);
fos.close();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: