如何使用 opencsv 写入 Android 的内部存储中的文件?

How can I write to a file in Android's internal storage using opencsv?

我有一个帮手 class,我需要用它来处理我的应用程序的数据。

我已将其设置为从 URL 读取文件。读取本身有效,但我很难将此文件写入应用程序的内部存储。

根据 Android 教程,我使用了 FileOutputStream 来写入文件。但是,我发现很难找到编写 FileOutputStream 并使用 CSVWriter 构造函数解析它的解决方案。

代码很长,所以如果您需要有关我的代码正在做什么的更多信息,我会 post 一个要点,但这里是导致我出现问题的地方:

BufferedReader in = new BufferedReader(new InputStreamReader(file_url.openStream()));
            String test;
            CSVReader reader = new CSVReader(in, ';');
            FileOutputStream file_out = app_context.openFileOutput(file_name, Context.MODE_PRIVATE);
            CSVWriter writer = new CSVWriter(<What goes here?>, ';');

https://gist.github.com/anonymous/4cde37a8614d1c69cc03ec678d36a9d7

CSVWriter 抛出异常 writer = new CSVWriter(String.valueOf(file_out), ';');:

08-19 14:22:30.794 29439-30099/com.example.a1003137m.profitgraph W/System.err: java.io.FileNotFoundException: java.io.FileOutputStream@dcfb9b3: open failed: EROFS (Read-only file system)
08-19 14:22:30.794 29439-30099/com.example.a1003137m.profitgraph W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
08-19 14:22:30.794 29439-30099/com.example.a1003137m.profitgraph W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
08-19 14:22:30.794 29439-30099/com.example.a1003137m.profitgraph W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
08-19 14:22:30.794 29439-30099/com.example.a1003137m.profitgraph W/System.err:     at java.io.FileWriter.<init>(FileWriter.java:80)
08-19 14:22:30.794 29439-30099/com.example.a1003137m.profitgraph W/System.err:     at com.example.a1003137m.profitgraph.FileProcessor.processFile(FileProcessor.java:50)
08-19 14:22:30.794 29439-30099/com.example.a1003137m.profitgraph W/System.err:     at com.example.a1003137m.profitgraph.FileProcessor.run(FileProcessor.java:40)
08-19 14:22:30.795 29439-30099/com.example.a1003137m.profitgraph W/System.err: Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
08-19 14:22:30.795 29439-30099/com.example.a1003137m.profitgraph W/System.err:     at libcore.io.Posix.open(Native Method)
08-19 14:22:30.795 29439-30099/com.example.a1003137m.profitgraph W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
08-19 14:22:30.795 29439-30099/com.example.a1003137m.profitgraph W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
08-19 14:22:30.795 29439-30099/com.example.a1003137m.profitgraph W/System.err:  ... 5 more

new CSVReader(in, ';'); 现在 in 是一个 InputStream。那么你会用什么 new CSVWriter( out, ';'); ?确实:OutputStream!。同样对于 reader,您使用了 BufferedReaderInputStreamReader

所以做类似的事情:BufferedWriterOutputStreamWriter