Reputation: 503
i want to print,for testing the content of two folder inside assets folder.
the folder is composed:
assets
but the program says to me that there aren't files.
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)
Upvotes: 0
Views: 53
Reputation: 11214
You cannot use the File class for a 'file' in assets folder. This is because File can only handle real files on the file system. Instead your only option is to call assets manager to open an inputstream fot the assets file and read from that stream. Examples have been posted many times on this forum.
Upvotes: 1
Reputation: 15774
The AssetManager.list
gives a path that is relative within the assets. If you need to open a file, you need to provide an absolute path of the file. For assets files, you can use the AssetManager.open
method.
Upvotes: 1