我开发了一个Android应用程序,在主要活动中我想使用以下代码行列出特定包中的所有文件:
String[] files = new File("src/com/android/app/").list();
它返回null!有什么不对 ? 感谢。
答案 0 :(得分:2)
File
课程的Javadocs:
用户界面和操作系统使用与系统相关的路径名 用于命名文件和目录的字符串。这堂课提出了一个 抽象的,与系统无关的分层路径名视图。一个 abstract pathname有两个组件:
An optional system-dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or "\\\\" for a Microsoft Windows UNC pathname, and A sequence of zero or more string names.
所以你似乎错过了前缀String。换句话说,路径名不是相对的,它是绝对的。
答案 1 :(得分:0)
我解决了这个问题,而在Dalvik虚拟机编译的所有类中,我们必须使用DexFile类Dex File documentation。
public List<String> getClasses(Context context, String package) {
List<String> classes = new LinkedList<String>();
try {
DexFile dexFile = new DexFile(context.getPackageCodePath());
for (Enumeration<String> iter = dexFile.entries(); iter.hasMoreElements();) {
String currentClass = iter.nextElement();
if (currentClass.contains(package)) {
classes.add(currentClass);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return classes;
}