在资产文件夹中列出文件的例外

时间:2014-10-06 17:45:07

标签: android

我想打印,用于测试资产文件夹中两个文件夹的内容。

文件夹由:

组成

资产

  • foo.txt的
  • foo1.txt

但程序告诉我没有文件。

AssetManager assetManager = getAssets();
            String[] files = null;
            try {
                files = assetManager.list("");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            for (int i = 0; i < files.length; i++) {
                File file = new File(files[i]);
                try {
                    Scanner scanner = new Scanner(file);
                    while (scanner.hasNextLine()) {
                        Log.e(" ", " " + scanner.next());
                    }
                    scanner.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

logcat的

10-06 19:38:16.328: W/System.err(12250): java.io.FileNotFoundException: /foo.txt: open failed: ENOENT (No such file or directory)
10-06 19:38:16.335: W/System.err(12250):    at libcore.io.IoBridge.open(IoBridge.java:416)
10-06 19:38:16.335: W/System.err(12250):    at java.io.FileInputStream.<init>(FileInputStream.java:78)
10-06 19:38:16.343: W/System.err(12250): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
10-06 19:38:16.351: W/System.err(12250):    at libcore.io.Posix.open(Native Method)
10-06 19:38:16.351: W/System.err(12250):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)

2 个答案:

答案 0 :(得分:1)

AssetManager.list提供了一个在资产中相对的路径。如果需要打开文件,则需要提供文件的绝对路径。对于资产文件,您可以使用AssetManager.open方法。

答案 1 :(得分:1)

您不能将File类用于assets文件夹中的'file'。这是因为File只能处理文件系统上的真实文件。相反,您唯一的选择是调用资产管理器打开资产文件的输入流并从该流中读取。示例已在此论坛上多次发布。