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



我有一个助手类,我需要处理我的应用程序的数据。

我已经设置了它,所以它从URL读取文件。阅读本身工作,但我有困难将此文件写入应用程序的内部存储。

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

代码很长,所以如果你需要更多关于我的代码的信息,我会发布一个要点,但这里是导致我问题的位:

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 !对于阅读器,您还使用了BufferedReaderInputStreamReader

所以做一些类似的:BufferedWriterOutputStreamWriter

相关内容

  • 没有找到相关文章

最新更新