我想将资产中的文件内容作为字符串读取.例如,位于的文本文档src/main/assets/
原始问题
我发现这个问题主要用作阅读资产文件的"FAQ",因此我总结了上述问题.以下是我最初的天真问题
我正在尝试将资源文件作为字符串读取,我在这里尝试了20个答案,但它们对我不起作用.
我的资产文件夹中有一个文件:data.opml,我必须将内容放在一个字符串中.我发送它像:
OPML.importFromFile(string, MainTabActivity.this);
Run Code Online (Sandbox Code Playgroud)
收到它像:
importFromFile(String filename, Context context);
Run Code Online (Sandbox Code Playgroud)
有效的东西(但它不是资产文件):
OPML.importFromFile(new StringBuilder(Environment.getExternalStorageDirectory().toString()).append(File.separator).append(fileNames[which]).toString(),MainTabActivity.this);
Run Code Online (Sandbox Code Playgroud)
我试过了:
AssetFileDescriptor descriptor = getAssets().openFd("data.opml");
FileReader reader = new FileReader(descriptor.getFileDescriptor());
And also:
InputStream input = getAssets().open("data.opml");
Reader reader = new InputStreamReader(input, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
Maby我做错了什么,但它只是不起作用,因为它项目给出错误(分别:OPML不能用于参数文件阅读器和阅读器),如果有人知道如何做到这一点,我将非常感激!
Com*_*are 128
getAssets().open()会回来的InputStream.使用标准Java I/O读取:
Java:
StringBuilder sb = new StringBuilder();
InputStream is = getAssets().open("book/contents.json");
BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8 ));
String str;
while ((str = br.readLine()) != null) {
sb.append(str);
}
br.close();
Run Code Online (Sandbox Code Playgroud)
科特林:
val str = assets.open("book/contents.json").bufferedReader().use { it.readText() }
Run Code Online (Sandbox Code Playgroud)
Mic*_*vin 32
有一个小错误CommonsWare的代码 - 换行字符被丢弃而不添加到字符串中.这是一些准备复制+粘贴的固定代码:
private String loadAssetTextAsString(Context context, String name) {
BufferedReader in = null;
try {
StringBuilder buf = new StringBuilder();
InputStream is = context.getAssets().open(name);
in = new BufferedReader(new InputStreamReader(is));
String str;
boolean isFirst = true;
while ( (str = in.readLine()) != null ) {
if (isFirst)
isFirst = false;
else
buf.append('\n');
buf.append(str);
}
return buf.toString();
} catch (IOException e) {
Log.e(TAG, "Error opening asset " + name);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e(TAG, "Error closing asset " + name);
}
}
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您也可以这样做,而无需使用循环。这很简单
AssetManager assetManager = getAssets();
InputStream input;
String text = "";
try {
input = assetManager.open("test.txt");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
text = new String(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("TAG", "Text File: " + text);
Run Code Online (Sandbox Code Playgroud)
小智 5
嗨,我认为这是最干净的方法:
public static String loadTextFromAssets(Context context, String assetsPath, Charset charset) throws IOException {
InputStream is = context.getResources().getAssets().open(assetsPath);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int length = is.read(buffer); length != -1; length = is.read(buffer)) {
baos.write(buffer, 0, length);
}
is.close();
baos.close();
return charset == null ? new String(baos.toByteArray()) : new String(baos.toByteArray(), charset);
}
Run Code Online (Sandbox Code Playgroud)
因为读者可能会遇到换行问题。
| 归档时间: |
|
| 查看次数: |
59409 次 |
| 最近记录: |