在我的应用程序的资产目录中列出所有文件和文件夹(也包括子文件夹)并检查资产是文件还是文件夹
List all files and folders (also Subfolders) in Assets Directory in my App and check if an asset is a file or a folder
我想检查我的 Android 应用程序的资产目录中的资产是文件还是目录。
我已经尝试过互联网上任何地方可用的所有解决方案,包括 Whosebug。
大多数解决方案使用
File myFile = new File(path);
然后检查
myFile.isDirectory();
要么
myFile.isFile();
但只有当我的文件和目录位于 Assets
目录以外的其他任何地方时,它才有效。
任何建议将不胜感激。
这是我的问题的解决方案,我发现它可以 100% 列出所有目录和文件,甚至 sub-directories 和子目录中的文件。它也适用于多个级别。
注意:以我为例
- 文件名有一个 .在他们中。即 .htm .txt 等
- 目录名没有任何 .在他们里面。
注:关于--路径--
获取Assets根目录下的所有文件和目录
String path = ""; // assets
或获取子文件夹中的文件和目录,使用其名称作为路径
String path = "html"; // <<-- assets/html
listAssetFiles2(path); // <<-- Call function where required
//function to list files and directories
public void listAssetFiles2 (String path){
String [] list;
try {
list = getAssets().list(path);
if(list.length > 0){
for(String file : list){
System.out.println("File path = "+file);
if(file.indexOf(".") < 0) { // <<-- check if filename has a . then it is a file - hopefully directory names dont have .
System.out.println("This is a folder = "+path+"/"+file);
if(path.equals("")) {
listAssetFiles2(file); // <<-- To get subdirectory files and directories list and check
}else{
listAssetFiles2(path+"/"+file); // <<-- For Multiple level subdirectories
}
}else{
System.out.println("This is a file = "+path+"/"+file);
}
}
}else{
System.out.println("Failed Path = "+path);
System.out.println("Check path again.");
}
}catch(IOException e){
e.printStackTrace();
}
}
谢谢
与其让名称中的点决定应该发生什么,不如检查它是文件还是目录。通过尝试为 file/directory 打开一个 InputStream
来创建您自己的 isDirectory(name)
函数。如果你成功了,它就是一个文件。如果您遇到异常,则它是一个目录。完成后不要忘记关闭流。
我想检查我的 Android 应用程序的资产目录中的资产是文件还是目录。
我已经尝试过互联网上任何地方可用的所有解决方案,包括 Whosebug。
大多数解决方案使用
File myFile = new File(path);
然后检查
myFile.isDirectory();
要么
myFile.isFile();
但只有当我的文件和目录位于 Assets
目录以外的其他任何地方时,它才有效。
任何建议将不胜感激。
这是我的问题的解决方案,我发现它可以 100% 列出所有目录和文件,甚至 sub-directories 和子目录中的文件。它也适用于多个级别。
注意:以我为例
- 文件名有一个 .在他们中。即 .htm .txt 等
- 目录名没有任何 .在他们里面。
注:关于--路径--
获取Assets根目录下的所有文件和目录
String path = ""; // assets
或获取子文件夹中的文件和目录,使用其名称作为路径
String path = "html"; // <<-- assets/html
listAssetFiles2(path); // <<-- Call function where required //function to list files and directories public void listAssetFiles2 (String path){ String [] list; try { list = getAssets().list(path); if(list.length > 0){ for(String file : list){ System.out.println("File path = "+file); if(file.indexOf(".") < 0) { // <<-- check if filename has a . then it is a file - hopefully directory names dont have . System.out.println("This is a folder = "+path+"/"+file); if(path.equals("")) { listAssetFiles2(file); // <<-- To get subdirectory files and directories list and check }else{ listAssetFiles2(path+"/"+file); // <<-- For Multiple level subdirectories } }else{ System.out.println("This is a file = "+path+"/"+file); } } }else{ System.out.println("Failed Path = "+path); System.out.println("Check path again."); } }catch(IOException e){ e.printStackTrace(); }
}
谢谢
与其让名称中的点决定应该发生什么,不如检查它是文件还是目录。通过尝试为 file/directory 打开一个 InputStream
来创建您自己的 isDirectory(name)
函数。如果你成功了,它就是一个文件。如果您遇到异常,则它是一个目录。完成后不要忘记关闭流。