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

Android代码工具集——文件操作

2014-05-28 16:17 204 查看
/**
* 判断文件是否存在
* @param filePath
* @return
*/
public static boolean isExistFile(String filePath) {
File filedir = new File(filePath);
return filedir.exists();
}

/**
* 获取文件名
* @param url
* @return
*/
public static String getFileName(String url)
{
if(TextUtils.isEmpty(url)){
return "";
} else{
int index = url.lastIndexOf('/');
return index >= 0 ? url.substring(index + 1) : url;
}
}

/**
* 获取资源文件
* @param url ctx
* @return
*/
public static String getFromAssets(String fileName,Context ctx){
String result="";
InputStreamReader inputReader = null;
BufferedReader bufReader = null;
try {
inputReader = new InputStreamReader(ctx.getResources().getAssets().open(fileName));
bufReader = new BufferedReader(inputReader);
String line="";
while((line = bufReader.readLine()) != null)
result += line;
} catch (Exception e) {
e.printStackTrace();
} finally{
if(bufReader!=null){
try {
bufReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputReader!=null){
try {
inputReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
ctx.getResources().getAssets().close();
}
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐