Android 资源 txt 文件的路径

发布于 2024-10-13 19:47:24 字数 200 浏览 3 评论 0原文

我正在做:

FileReader fin = new FileReader("file:///android_asset/myFile.txt");

在 Android 项目和许多变体中。在运行时我得到一个文件未找到异常。该文件在资产文件夹中存在且正确,因此我的路径一定是错误的。

我这里需要的绝对路径是什么?

I'm doing:

FileReader fin = new FileReader("file:///android_asset/myFile.txt");

in an Android project and many variations. At runtime I get a file not found exception. The file is present and correct in the assets folder, so my path must be wrong.

What is the absolute path I need here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

失退 2024-10-20 19:47:24
AssetFileDescriptor descriptor = getAssets().openFd("myfile.txt");
FileReader reader = new FileReader(descriptor.getFileDescriptor());

尝试将上述内容与 FileDescriptors 一起使用。这似乎是我发现的收集资产路径的最简单的方法。

AssetFileDescriptor descriptor = getAssets().openFd("myfile.txt");
FileReader reader = new FileReader(descriptor.getFileDescriptor());

Try using the above with FileDescriptors. Seems to be the most foolproof way I've found to gather asset paths.

北凤男飞 2024-10-20 19:47:24
    InputStream is = getResources().getAssets().open("terms.txt");
    String textfile = convertStreamToString(is);

public static String convertStreamToString(InputStream is)
            throws IOException {
            Writer writer = new StringWriter();

            char[] buffer = new char[2048];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,
                        "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            String text = writer.toString();
            return text;
    }
    InputStream is = getResources().getAssets().open("terms.txt");
    String textfile = convertStreamToString(is);

public static String convertStreamToString(InputStream is)
            throws IOException {
            Writer writer = new StringWriter();

            char[] buffer = new char[2048];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,
                        "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            String text = writer.toString();
            return text;
    }
倾城°AllureLove 2024-10-20 19:47:24

你能用类似的东西吗

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(context.getAssets().open("fileName.txt")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Can you use something like

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(context.getAssets().open("fileName.txt")));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
疯狂的代价 2024-10-20 19:47:24

它不会读取它,因为资产文件夹中的所有资产都被压缩,尝试将其扩展名更改为 .mp3 然后读入,这应该会阻止它被压缩。

Its not reading it because all assets in assets folder are compressed, try changing its extension to .mp3 then read it in, that should stop it from being compressed.

倥絔 2024-10-20 19:47:24

我发现如果您使用像 Eclipse 这样的 IDE,您可能需要进行清理——或者删除 bin 目录中的 API。当您进行构建时,资产似乎没有得到更新。

I found that if you are using an IDE like Eclipse you may need to do a clean -- or delete the API in the bin directory. It seems that the assets isn't getting updated when you do a build.

ゃ懵逼小萝莉 2024-10-20 19:47:24
AssetManager am = context.getAssets();
InputStream fs = am.open("myFile.txt");
AssetManager am = context.getAssets();
InputStream fs = am.open("myFile.txt");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文