java - 逐行读取文件
问题描述
我正在创建一个 Android 应用程序来跟踪财务费用,我希望允许用户将财务交易批量导入我的应用程序。交易将以 .csv/.txt 文件的形式出现。
但是,我遇到了一个神秘的异常:
java.lang.AbstractMethodError: abstract method "android.content.IContentProvider android.content.ContentResolver.acquireUnstableProvider(android.content.Context, java.lang.String)" at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1780) at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1394) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247) at android.content.ContentResolver.openInputStream(ContentResolver.java:967) ....
工作流程如下:用户选择要导入的文本文件,应用程序导入内容。
启动文件选择器:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
//intent.addCategory("CATEGORY_OPENABLE");
startActivityForResult(intent, REEQUEST_CODE_IMPORT);
捕捉结果:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REEQUEST_CODE_IMPORT) {
// Make sure the request was successful
Uri path = data.getData();
if (resultCode == RESULT_OK && path != null) {
InputStream inputStream = null;
try {
ContentResolver contentResolver = new ContentResolver(getContext()) {};
// Error happens in the next line
inputStream = contentResolver.openInputStream(path);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(inputStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = "";
while((line = br.readLine()) != null) {
// Do something meaningful...
}
} catch () {
// Catch the exceptions ( I have removed some boiler plate code here...)
} finally {
// Close the path ( I have removed some boiler plate code here...)
inputStream.close();
}
}
}
}
解决方案
Android 文档帮助我解决了这个问题:Access document and other files from shared storage - Input Stream
以下代码适应工作:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REEQUEST_CODE_IMPORT) {
// Make sure the request was successful
Uri path = data.getData();
if (resultCode == RESULT_OK && path != null) {
StringBuilder stringBuilder = new StringBuilder();
try (InputStream inputStream = getActivity().getContentResolver().openInputStream(path);
BufferedReader reader = new BufferedReader(
new InputStreamReader(Objects.requireNonNull(inputStream)))) {
String line;
while ((line = reader.readLine()) != null) {
Log.d(TAG, line);
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
String content = stringBuilder.toString();
// Do something with it
}
}
}
推荐阅读
- message-hub - Message Hub & Confluent Kafka Connect S3
- ios - Crashlytics 上传 ipa 文件不会上传 DSYM 文件
- php - 使用 file_get_contents() 时 urlshortener api 的问题
- ios - 从另一个类触摸时的 Swift 按钮操作选择器:无法识别的选择器发送到实例
- javascript - 无法将 jQuery 按钮提交更改为下拉选择并提交
- python - 使用 Python Dict 中的值获取键名称
- javascript - 石头、纸、剪刀游戏 - 重复 5 轮
- c# - 找不到路径的一部分,但它存在
- python - OverFlowError:Python int 太大,无法在 Windows 上转换为 C long 以限制 CSV 字段大小
- dialogflow-es - 如何处理 DialogFlow 中的后备循环?