Java 在android中的原始文件夹中找不到文件

Java 在android中的原始文件夹中找不到文件,java,android,Java,Android,我有一个文件dictionary.txt,我需要从原始文件夹中获取该文件,所以我搜索了一下,发现了以下内容: 下面是一个例子: "android.resource:///"+getPackageName()+ "/" + R.raw.dictionary; 但它不起作用,你知道吗 编辑: 这就是我正在做的 for(String line: FileUtils.readLines(file)) {

我有一个文件
dictionary.txt
,我需要从原始文件夹中获取该文件,所以我搜索了一下,发现了以下内容:

下面是一个例子:

"android.resource:///"+getPackageName()+ "/" + R.raw.dictionary;
但它不起作用,你知道吗

编辑:

这就是我正在做的

                for(String line: FileUtils.readLines(file))
                {

                    if(line.toLowerCase().equals(b.toLowerCase()))
                    {
                        valid = true;
                    }
                }

您可以使用以下方法获取原始资源上的输入流:

getResources().openRawResource(R.raw.dictionary);
编辑:

从您的编辑中,您确实没有理由特别需要该文件,而不仅仅是使用
openrawsource(int-id)
方法提供的输入流……只需要使用现有的java类


很遗憾,您不能直接从
raw
文件夹创建
文件
对象。您需要将其复制到
SD卡中或应用程序的缓存中

您可以通过这种方式检索文件的
InputStream

    InputStream in = getResources().openRawResource(R.raw.yourfile);

  try {
       int count = 0;
       byte[] bytes = new byte[32768];
       StringBuilder builder = new StringBuilder();
       while ( (count = is.read(bytes,0 32768) > 0) {
           builder.append(new String(bytes, 0, count);
       }

       is.close();
       reqEntity.addPart(new FormBodyPart("file", new StringBody(builder.toString())));
   } catch (IOException e) {
       e.printStackTrace();
   }
编辑:

要将其复制到内部存储器,请执行以下操作:

File file = new File(this.getFilesDir() + File.separator + "fileName.ext");
try {
        InputStream inputStream = resources.openRawResource(R.id._your_id);
        FileOutputStream fileOutputStream = new FileOutputStream(file);

        byte buf[]=new byte[1024];
        int len;
        while((len=inputStream.read(buf))>0) {
            fileOutputStream.write(buf,0,len);
        }

        fileOutputStream.close();
        inputStream.close();
    } catch (IOException e1) {}

现在您有了一个
文件
,您可以在任何需要的地方访问它。

将该文件放在
资产
文件夹中,然后像这样打开它

getResources().getAssets().open("dictionary.txt");

请张贴相关代码。“不起作用”告诉我们什么都没有。它找不到文件我需要它是一个文件。你能解释一下为什么/你用它做什么吗?我在使用apache commons IOM的很多东西。你可以使用输入流(或文件描述符,如果你使用openRawResourceFd(ID)将文件复制到你创建的文件位置(然后,希望能知道通往…)的道路,但如果不知道你想做什么,很难提出替代方案。对不起,我是android新手,不知道这会有什么帮助:(你想用它做什么?来自apache commons的东西io@Welsar55我们可以更好地帮助您,如果您发布符合您确切要求的代码,比如您需要将其传递给的函数,可能是因为它有一些其他重载,或者查看我在internal Storages中创建文件的答案上的编辑添加了我正在做的事情
getResources().getAssets().open("dictionary.txt");