设为首页 加入收藏

TOP

Android 下载文件及写入SD卡(二)
2014-11-23 23:57:02 来源: 作者: 【 】 浏览:14
Tags:Android 下载 文件 写入
eturn
* @throws IOException
*/
public File createSDFile(String fileName) throws IOException{
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}

/**
* 在SD卡上创建目录
* @param dirName
* @return
*/
public File createSDDir(String dirName){
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}

/**
* 判断SD卡上的文件夹是否存在
* @param fileName
* @return
*/
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();
}

/**
* 将一个InputStream里面的数据写入到SD卡中
* @param path
* @param fileName
* @param input
* @return
*/
public File write2SDFromInput(String path,String fileName,InputStream input){
File file = null;
OutputStream output = null;
try {
createSDDir(path);
file = createSDFile(path + fileName);
output = new FileOutputStream(file);
byte[] buffer = new byte[FILESIZE];


/*真机测试,这段可能有问题,请采用下面网友提供的
while((input.read(buffer)) != -1){
output.write(buffer);
}
*/


/* 网友提供 begin */
int length;
while((length=(input.read(buffer))) >0){
output.write(buffer,0,length);
}
/* 网友提供 end */


output.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}


}


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇在树莓派上用Python控制LED 下一篇Dom4j 存在命名空间导致document...

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: