android - CSVReader 和 android

标签 android csv file-io

我想从 csv 文件读取数据并将其存储到数据库中。这就是我保存 csv 文件的方式(这不会出现错误 - 只是为了显示我计划使用 CSVreader 读取的文件的存储位置和方式):

synchronized public void readFromUrl(String url, String outputFile, Context context) throws FileNotFoundException {
        URL downloadLink = null;
        try {
            downloadLink = new URL(url);
        } catch (MalformedURLException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        BufferedReader in = null;

        try {
                        in = new BufferedReader(
                                new InputStreamReader(downloadLink.openStream(), "UTF8"));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        FileOutputStream fstream = context.openFileOutput(outputFile,Context.MODE_PRIVATE);

        Writer out = new OutputStreamWriter(fstream);

        Log.d(TAG, "BufferedReader "+in); 
        String inputLine = null;
        try {
            while ((inputLine = in.readLine()) != null){
                out.write(inputLine+"\n");
                //logger.debug("DOWNLOADED: "+inputLine);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            fstream.close();
            out.close();
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

到目前为止,我用于读取 csv 文件的代码:

public void readInCSVFile(String filename, Context context) throws IOException {
        IDbHelper dbHelper = new DbHelper(); ; 
        CSVReader products = null;

        products = new CSVReader(new InputStreamReader(context.getAssets().open(filename)));

我收到 NoClassDefFoundError 异常。

我的 android 项目的引用库中有 opencsv.jar。

预先感谢您的帮助。

最佳答案

您的项目在 Eclipse IDE 中吗?如果是,则查看“项目属性 -> Java 构建路径 -> 库”中是否设置了 lib (opencsv.jar),并且在选项卡中选中了它:“Order and导出”也。在“订购和导出”下,将库移至列表顶部。 然后清理并重建。

PS:如果这没有帮助,请提供错误的完整堆栈跟踪。

关于android - CSVReader 和 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14749189/

相关文章:

java - 图像亮度对比度搜索栏

android - View 绑定(bind)应该取代数据绑定(bind)吗?

java - 使用 BeanIO 将列表写入 CSV

c# - 使用 C# 写入 CSV 文件

objective-c - 在 iOS 上使用 Cordova/Phonegap 1.6.1 一次写入多个文件

android - 在 Android 中滑动时缩小的 Seekbar 按钮(使用自己的拇指 xml)

android - 在 Android 的首选项屏幕中使用 TimePicker 或 DatePicker

python - 从 csv 文件中获取每个提取字符的行索引

c++ - 打开文件以在 C++ 中显示内容

php - Apache:如果在文件复制期间请求,则返回损坏的文件