码迷,mamicode.com
首页 > 移动开发 > 详细

(文件操作)Android相关的File文件操作

时间:2017-02-06 00:20:16      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:new   length   str   exists   readline   ram   信息   adt   目录   

判断文件是否存在:

    /**
     * 判断文件是否存在
      * 
     * @param path 文件路径
      * @return [参数说明]
     * @return boolean [返回类型说明]
     */
    public static boolean isFileExist(String path) {
        if (TextUtils.isEmpty(path)) {
            return false;
        }
        File file = new File(path);
        return file.exists();
    }

判断文件是否存在时,需要根据文件路径生成一个File对象实例,并对该实例指向的文件存在性进行判断。

读取文件(以“一行”为一个指定信息)内容信息,并返回ArrayList实例:

    /**
     * <功能描述>读取指定目录资源文件,以ArrayList的形式返回文件中每行数据
      * 
     * @param fileName
     * @return [参数说明]
     * @return List<String> [返回类型说明]
     */
    public static List<String> readTxtFile(String fileName) {
        List<String> arrayList = new ArrayList<String>();
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        String tempStr = null;
        try {
            File file = new File(fileName);
            inputStream = new FileInputStream(file);
            bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream, "utf-8"));
        } catch (IOException ioException) {
            LogUtil.d(TAG,
                    "readTxtFile::ioException:" + ioException.getMessage());
        }
        if (bufferedReader == null) {
            return arrayList;
        }
        try {
            tempStr = bufferedReader.readLine();
            while (tempStr != null) {
                LogUtil.d(TAG, "tmpStr=" + tempStr);
                arrayList.add(tempStr.trim());
                tempStr = bufferedReader.readLine();
            }
            inputStream.close();
            bufferedReader.close();
        } catch (Exception exception) {
            LogUtil.d(TAG, "readTxtFile::exception" + exception.getMessage());
        }
        LogUtil.d(TAG, "arrayList.length=" + arrayList.size());
        return arrayList;
    }

(文件操作)Android相关的File文件操作

标签:new   length   str   exists   readline   ram   信息   adt   目录   

原文地址:http://www.cnblogs.com/CVstyle/p/6368866.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!