搜索所有在互联网上,但没有找到工作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();