cmackie21 cmackie21 - 3 months ago 25
Android Question

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

I have a helper class which I need to process data for my application.

I have set it up so it reads the file from a URL. The reading itself works but I am having difficulty writing this file to the app's internal storage.

As per the Android tutorial I have used a FileOutputStream to write the file. However, I am finding it difficult finding a solution to write the FileOutputStream and resolving it with the CSVWriter constructor.

The code is very long so I will post a gist if you need more information about what my code is doing but here is the bit which is causing me problems:

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

Exception thrown with 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

Answer

new CSVReader(in, ';'); Now in is an InputStream. So what would you use with new CSVWriter( out, ';'); ? Indeed: an OutputStream!. Also for the reader you used BufferedReader and InputStreamReader.

So do something similar: BufferedWriter and OutputStreamWriter.