use*_*772 0 android text assets file fragment
我应该如何从中读取文本文件assets?
我在这里使用片段。看起来我不能使用任何东西,getAssets();因为然后我得到:Error:(88, 37) error: cannot find symbol method getAssets()目标是能够读/写几个字符串。
例如:
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("hello.txt");
}
catch (IOException e){
Log.e("message: ",e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
我希望能够在一个地方读取值,然后在代码的另一个地方写入值。请帮忙,我很绝望,我在网上找到的任何东西都不起作用
public String loadTextFileFromAsset() {
String content = null;
try {
InputStream is = getContext().getAssets().open("dora.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
content = new String(buffer,"UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return content;
}
Run Code Online (Sandbox Code Playgroud)
在 src/main/assets 文件夹中添加文本文件(比如 dora.txt)