2016-08-19 139 views
0

我有一個幫助類,我需要爲我的應用程序處理數據。如何使用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作家=新CSVWriter(將String.valueOf(file_out), ';');:

08-19 14:22:30.794 29439-30099/com.example.a1003137m.profitgraph W/System.err: java.io.FileNotFoundException: [email protected]: 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 
+0

'<這裏有什麼?>'。你嘗試過'file_out'嗎? – greenapps

+0

是的,它告訴我用String.valueOf()包裝它,但它會拋出異常。 – cmackie21

+0

你不會告訴哪一個?不聰明!發佈logcat。 – greenapps

回答

1

new CSVReader(in, ';');現在inInputStream。那麼你會用new CSVWriter(out, ';');什麼?事實上:一個OutputStream!另外對於您使用BufferedReaderInputStreamReader的讀者。

所以做類似的事情:BufferedWriterOutputStreamWriter

相關問題