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

时间:2016-08-19 13:47:38

标签: android opencsv

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

我已将其设置为从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

1 个答案:

答案 0 :(得分:1)

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

执行类似的操作:BufferedWriterOutputStreamWriter