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

判断文件是否存在:

    /**
     * 判断文件是否存在
      * 
     * @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;
    }
原文地址:https://www.cnblogs.com/CVstyle/p/6368866.html