【问题标题】:How can I write to a file in Android's internal storage using opencsv?如何使用 opencsv 写入 Android 内部存储中的文件?
【发布时间】:2016-08-19 23:01:17
【问题描述】:

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

我已经设置它从 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

【问题讨论】:

  • &lt;What goes here?&gt;。你试过file_out 吗?
  • 是的,它告诉我用 String.valueOf() 包装它,但它会引发异常。
  • 你不知道是哪一个?不聪明!发布 logcat。
  • it tells me to wrap it with String.valueOf() ??谁告诉你的?
  • file_url.openStream()file_url 是什么?请显示代码。

标签: android opencsv


【解决方案1】:

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

所以做类似的事情:BufferedWriterOutputStreamWriter

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多