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

来源:互联网 发布:淘宝五十字好评大全 编辑:程序博客网 时间:2024/06/05 18:02
 /**  * 判断文件是否存在 * @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;      }

0 0
原创粉丝点击