How to find file in project ?

I would like to find the Main.java file in my project. I tried:

  Project project = event.getData(PlatformDataKeys.PROJECT);
final VirtualFile projectFile = project.getProjectFile();
System.out.println(projectFile.findChild("Main.java"));

But System.out.println always returns null.

Regards
  Roger

2 comments

Please check the PsiCookBook http://confluence.jetbrains.com/display/IDEADEV/PSI+Cookbook
There should be a lot of examples there which will help you find the best solution for your prolem.

For instance you may want to use

PsiShortNamesCache.getInstance(project).getClassesByName("Main", GlobalSearchScope.projectScope(project))
But of course, consider your use-case appropriately and choose the correct method that suits your requirements :)

0

If You want find files, you could also use:
com.intellij.psi.search.FilenameIndex#getFilesByName for searching for PsiFiles

FilenameIndex.getFilesByName(project, fileName, searchScope);

or
com.intellij.util.indexing.FileBasedIndex#getContainingFiles for VirtualFiles

FileBasedIndex.getInstance().getContainingFiles(FilenameIndex.NAME, fileName, searchScope);

0

Please sign in to leave a comment.