android开发打开文件txt并返回内容

android开发打开文件txt并返回内容,第1张

android开发打开文件txt并返回内容

将文件放在

/assets
项目的文件夹中,然后
InputStream
通过打开文件即可
AssetManager

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

然后,您可以从文件中读取行,并将它们添加到

StringBuilder
使用
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();


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5095417.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存