android读取sd卡上文件中的数据

从sd卡上的文件中读取数据

第1种方法:

public static String readFileMsg(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return "";
        }
        BufferedReader reader = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                return "";
            }
            reader = new BufferedReader(new FileReader(file));
            String line;
            StringBuilder content = new StringBuilder();
            while((line = reader.readLine()) != null) {
                content.append(line);
            }
            return content.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

第2种方法:

public static String readFileMsg(String filePath) {
InputStream inStream = null;
BufferedReader reader = null;
try {
    StringBuilder content = new StringBuilder();
    File file = new File(filePath);
    if (!file.exists()) {
        return "";
    }
    inStream = new FileInputStream(file);
    if (inStream != null) {
        InputStreamReader inputReader = new InputStreamReader(inStream);
        reader = new BufferedReader(inputReader);
        String line;
        while ((line = reader.readLine()) != null) {
            content.append(line);
        }
        reader.close();
        return content.toString();
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if(inStream != null) {
        try {
            inStream.close();
        } catch(IOException e) {
            
        }
    }
}
return "";
}

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 11:26:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 11:26:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 11:26:03       87 阅读
  4. Python语言-面向对象

    2024-03-15 11:26:03       96 阅读

热门阅读

  1. Android studio存储之SharedPreferences

    2024-03-15 11:26:03       45 阅读
  2. nginx笔记

    2024-03-15 11:26:03       40 阅读
  3. C语言实现冒泡排序

    2024-03-15 11:26:03       41 阅读
  4. Qt应用开发(安卓篇)——安卓广播机制

    2024-03-15 11:26:03       43 阅读
  5. docker部署mysql5

    2024-03-15 11:26:03       39 阅读
  6. Python进阶学习(5)反射

    2024-03-15 11:26:03       40 阅读
  7. 举例说明计算机视觉(CV)技术的优势和挑战

    2024-03-15 11:26:03       43 阅读
  8. 三维的旋转平移矩阵形式

    2024-03-15 11:26:03       44 阅读
  9. openGauss 脚本源码浅析(1)—— simpleInstall

    2024-03-15 11:26:03       31 阅读
  10. SpringCloud Stream笔记整理

    2024-03-15 11:26:03       42 阅读
  11. 智障版本GPT3实现

    2024-03-15 11:26:03       44 阅读