zoukankan      html  css  js  c++  java
  • (文件操作)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;
        }
  • 相关阅读:
    CodeForces 500C New Year Book Reading
    CodeForces 460B Little Dima and Equation 枚举
    CodeForces 451B Sort the Array
    【jquery】jQuery实现轮播图
    【IDEA】IDEA技巧记录
    【eclipse】日常使用eclipse记录
    【SSM】spring配置文件中读取配置文件的三种方式
    【Git】IDEA克隆和提交项目于码云
    semantic UI—表单验证
    【spring Data Jpa】JPA生成数据库表
  • 原文地址:https://www.cnblogs.com/CVstyle/p/6368866.html
Copyright © 2011-2022 走看看