FileReader csv : FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

柔情痞子 提交于 2019-12-10 12:23:08

问题


I would like to read a csv file. For that, I found CSVReader which I use like this :

List myEntries = null;
    try {
        AssetFileDescriptor descriptor = getAssets().openFd("file.csv");
        CSVReader csvReader = new CSVReader(new FileReader(descriptor.getFileDescriptor()), ',', '"');
        myEntries = csvReader.readAll();
        csvReader.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

But I got this error: FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed.

My file is located in assets folder.

I have already seen this thread: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed It did not help me at all.

So, I must do something wrong, but what? Should I put my file on another folder and open it by another method?


回答1:


Android compresses all assets, except for the following types:

".jpg", ".jpeg", ".png", ".gif",
".wav", ".mp2", ".mp3", ".ogg", ".aac",
".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
".amr", ".awb", ".wma", ".wmv"

If you have sufficient control over the build process, you can use the "-0" flag with zip or aapt to add the assets.

-0 specifies an additional extension for which such files will not be stored compressed in the .apk. An empty string means to not compress any files at all.

If you have the chance to do it with your CSVReader, deposit your csv file somewhere other than the assets folder, like sdcard.



来源:https://stackoverflow.com/questions/11678043/filereader-csv-filenotfoundexception-this-file-can-not-be-opened-as-a-file-de

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!