Java 从Android中的assets文件夹读取文件的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9674815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:00:32  来源:igfitidea点击:

Trouble with reading file from assets folder in Android

javaandroidreadfile

提问by Kr?lleb?lle

This question is with regards to this one. Since it is a specific question I moved that question by itself here. I have tried creating a text file, "foo.txt", an read it into my Activity doing:

这个问题是关于这个的。由于这是一个特定的问题,因此我将这个问题移到了这里。我尝试创建一个文本文件“foo.txt”,并将其读入我的活动:

File file = new File("/assets/foo.txt");
if ( file.exists() ){
    txtView.setText("Exists");
}
else{
    txtView.setText("Does not exist");
}

The "foo.txt" file is located in my assets folder and I have verified that it exists in the OS. My TextView always gets the text "Does not exist" from the code above. I have tried going

“foo.txt”文件位于我的资产文件夹中,我已经验证它存在于操作系统中。我的 TextView 总是从上面的代码中获取文本“不存在”。我试过去

File file = new File("/assets/foo.txt");
Scanner in = new Scanner(file);

as well, but this produces the following inlineerror: "Unhandled exception type FileNotFoundException". Eclipse then suggest to involve try/catch, which removes the error but it doesn't work properly then either.

同样,但这会产生以下内联错误:“未处理的异常类型 FileNotFoundException”。Eclipse 然后建议使用 try/catch,这会消除错误,但它也不能正常工作。

I have also tried setting my assets folder to "Use as source folder", but this does not make any difference. I have also tried using a raw folder as several people suggests to no use. I am out of options and really need help for this one. Should be easy...

我也尝试将我的资产文件夹设置为“用作源文件夹”,但这没有任何区别。我也尝试过使用原始文件夹,因为有几个人建议没有用。我别无选择,真的需要帮助来解决这个问题。应该很容易...

Another try is to go

另一个尝试是去

AssetManager assetManager = getResources().getAssets();
InputStream is = assetManager.open("assets/foo.txt");

but this produces the inlineerror in the second line: "Unhandled exception type IOException".

但这会在第二行中产生内联错误:“未处理的异常类型 IOException”。

采纳答案by MByD

I'm with CommonsWare in that case (that's the safe side :) ), but it should be:

在这种情况下,我使用 CommonsWare(这是安全的一面:)),但它应该是:

AssetManager assetManager = getResources().getAssets();
InputStream inputStream = null;

    try {
        inputStream = assetManager.open("foo.txt");
            if ( inputStream != null)
                Log.d(TAG, "It worked!");
        } catch (IOException e) {
            e.printStackTrace();
        }

Do not use InputStream is = assetManager.open("assets/foo.txt");

不使用 InputStream is = assetManager.open("assets/foo.txt");

回答by CommonsWare

You do not access assets/at runtime using File. You access assets/at runtime using AssetManager, which you can get via getResources().getAssets().

您不能assets/在运行时使用File. 您可以assets/在运行时使用访问AssetManager,您可以通过 获得getResources().getAssets()

回答by ρяσ?ρ?я K

try this :

尝试这个 :

    private AssetManager am;
     am=getAssets();

     InputStream inputStream = null ;  
        try   
        {  
            inputStream = am.open("read.txt");  
        }   
        catch (IOException e) {}