android 对SD卡文件的I/O操作

public class FileUtils {


private final static String LOG_MESSAGE = "MZZ_LOG : " ;
//得到手机SD卡的目录
private final static String SDPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";

//创建一个新的文件目录
public File createSDFile(String fileName) throws IOException{
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}

//创建一个新的目录
public File createSDDir(String dirName){
File file = new File(SDPATH + dirName) ;
return file ;
}

//判断目录是否存在
public boolean isFileExist(String path , String fileName){
File file = new File(SDPATH + path + "/" + fileName);
return file.exists() ;
}

//将非文本文件写到SD卡中
public File writeFile2SDFromInput(String path , String fileName , InputStream inputStream){
File file = null ;
OutputStream outputStream = null ;
try{
createSDDir(path);
file = createSDFile(path + "/" + fileName);
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[4 * 1024];
while(inputStream.read(buffer) != -1){
outputStream.write(buffer);
}
}catch(Exception e){
e.printStackTrace();
}

return file;
}

//将文本文件写到SD卡中
public File writeText2SDFromInput(String path , String fileName , InputStream inputStream){
File file = null ;
StringBuffer sb = new StringBuffer() ;
String temp = null ;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream , "GB2312"));
createSDDir(path);
file = createSDFile(path +"/" + fileName);
FileOutputStream os = new FileOutputStream(file,false);
OutputStreamWriter bw = new OutputStreamWriter(os,"GB2312");
while((temp = br.readLine()) != null){
sb.append(temp);
sb.append("\n");
}
bw.flush();
bw.write(sb.toString() , 1 , sb.toString().length()-1);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(LOG_MESSAGE + e);
}

return file ;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值