How to list source code file names at Java run time?

旧时模样 提交于 2019-12-24 01:37:08

问题


When my program starts to run, how do I list available java source file names ? For example, I have a few dozen source files named "My_App_*.java" in my src directory, after I start my app, how can I call Java to list source files start with "My_App_" dynamically ?

Frank


回答1:


If you know where the source directory is:

File srcFolder = new File("./src");
String[] files = srcFolder.list();
for(String file : files){
    if(file.startsWith("My_App_")){
        System.out.println(file);
    }
}



回答2:


new File(".").list(new FilenameFilter()
{
  public boolean accept(File dir, String name)
  {
    return name.startsWith("My_App_") && name.endsWith(".java");
  }
});

Replace . with the directory where the files are.

But why do you need to do that?




回答3:


Use java.io.File.list and its related methods. You can either get a String[] of filenames, or File[]. You can provide a FilenameFilter or a FileFilter, or you can filter the returned array afterward.



来源:https://stackoverflow.com/questions/2366733/how-to-list-source-code-file-names-at-java-run-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!