从阅读txt文件present数据在Android设备的SD卡 [英] Read data from txt file present in sdcard of an Android device

查看:148
本文介绍了从阅读txt文件present数据在Android设备的SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件中读取数据:

I want to read data from file:

    FileInputStream in = openFileInput(file);
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

但我不知道文件。我想用户告诉我该文件。我要让他搜索的文件与文件管理器。我该怎么办?
我希望我解释自己,遗憾的英语。先谢谢了。

But I don't know "file". I want that the user tell me where the file is. I want to make him search the file with a filemanager. what should I do? I hope I explained myself and sorry for the English. Thanks in advance.

推荐答案

试试这个code,你可以找到在网上这样的例子很多。

Try this code, you can find so many examples in the web

 File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
 BufferedReader br = new BufferedReader(new FileReader(file));
 String line;

 while ((line = br.readLine()) != null) {
    text.append(line);
    text.append('\n');
 }
} catch (IOException e) {
//You'll need to add proper error handling here
}

这篇关于从阅读txt文件present数据在Android设备的SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆