android开发打开文件TXT和回报内容 [英] android development open file txt and return content

查看:97
本文介绍了android开发打开文件TXT和回报内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索所有在互联网上,但没有找到工作code。 我怎样才能得到一个txt文档的内容,并返回。

Search all over the internet and could not find a working code. How can i get content of a txt document and return it.

假设我有(SRC / my.proovi.namespace / data.txt中)一个txt文件 我创建了一个方法中调用refresh_all_data();在这里我想对数据进行收集和返回。 在主要活动方式,我只是需要得到的内容(字符串内容= refresh_all_data())和多数民众赞成它。

Suppose i have a txt file in ( src/my.proovi.namespace/data.txt ) And i created a method called refresh_all_data(); where i want the data to be collected and returned. In the main activity method i just need to get the content as ( String content = refresh_all_data(); ) And thats it.

应该是容易的,但都无法找到有效的答案。 非常感谢你。

Should be easy but just cant find a working answer. Thank you very much.

推荐答案

把文件中的 /资产文件夹中,那么你就可以得到的 的InputStream 通过打开它throught的 AssetManager

Put the file in the /assets folder of your project, then you can get an InputStream by opening it throught the AssetManager:

InputStream in = getAssets().open("data.txt");

您就可以从文件中读取行,并将它们添加到的 的StringBuilder 通过使用的 阅读

You can then read lines from the file and add them to a StringBuilder by using a Reader:

//The buffered reader has a method readLine() that reads an entire line from the file, InputStreamReader is a reader that reads from a stream.
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//This is the StringBuilder that we will add the lines to:
StringBuilder sb = new StringBuilder(512);
String line;
//While we can read a line, append it to the StringBuilder:
while((line = reader.readLine()) != null){
    sb.append(line);
}
//Close the stream:
reader.close();
//and return the result:
return sb.toString();

这篇关于android开发打开文件TXT和回报内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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