package com.example.downloadHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.os.Environment;
import android.util.Log;
public class FileUnits {
/**
* 判断文件夹是否存在
* true表示存在 false表示不存在
*/
public Boolean CheckPath(String path){
File file = new File(path);
return file.exists();
}
/**
* 判断文件夹,如不存在则创建文件夹
*/
public void CreatePath(String path){
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
}
/**
* 判断文件是否存在,如不存在则新建文件
*/
public File CreateNewFile(String path,String filename) throws IOException{
File file = new File(path + filename);
file.createNewFile();
return file;
}
/**
* 保存文件
*/
public File SaveFile(String path,String filename,InputStream is) {
File file= null;
FileOutputStream output = null;
try {
CreatePath(path);
file = CreateNewFile(path,filename);
output = new FileOutputStream(file);
byte[]bt=new byte[4*1024];
while (is.read(bt)!=-1) {
output.write(bt);
}
//刷新缓存,
output.flush();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
/**
* 保存文件到SD卡
*/
public File SaveFileToSDCard(String path,String filename,InputStream is) {
File file = null;
FileOutputStream output = null;
try {
//追加SD位置
path += Environment.getExternalStorageDirectory()+"/";
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//创建文件夹
CreatePath(path);
//创建文件
file = CreateNewFile(path,filename);
output = new FileOutputStream(file);
byte[]bt=new byte[4*1024];
while (is.read(bt)!=-1) {
output.write(bt);
}
//刷新缓存,
output.flush();
} else {
Log.e("SD", "SD卡无写权限");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.os.Environment;
import android.util.Log;
public class FileUnits {
/**
* 判断文件夹是否存在
* true表示存在 false表示不存在
*/
public Boolean CheckPath(String path){
File file = new File(path);
return file.exists();
}
/**
* 判断文件夹,如不存在则创建文件夹
*/
public void CreatePath(String path){
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
}
/**
* 判断文件是否存在,如不存在则新建文件
*/
public File CreateNewFile(String path,String filename) throws IOException{
File file = new File(path + filename);
file.createNewFile();
return file;
}
/**
* 保存文件
*/
public File SaveFile(String path,String filename,InputStream is) {
File file= null;
FileOutputStream output = null;
try {
CreatePath(path);
file = CreateNewFile(path,filename);
output = new FileOutputStream(file);
byte[]bt=new byte[4*1024];
while (is.read(bt)!=-1) {
output.write(bt);
}
//刷新缓存,
output.flush();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
/**
* 保存文件到SD卡
*/
public File SaveFileToSDCard(String path,String filename,InputStream is) {
File file = null;
FileOutputStream output = null;
try {
//追加SD位置
path += Environment.getExternalStorageDirectory()+"/";
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//创建文件夹
CreatePath(path);
//创建文件
file = CreateNewFile(path,filename);
output = new FileOutputStream(file);
byte[]bt=new byte[4*1024];
while (is.read(bt)!=-1) {
output.write(bt);
}
//刷新缓存,
output.flush();
} else {
Log.e("SD", "SD卡无写权限");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}