我正在尝试创建一个插件,它会给我一个在 eclipse 中打开的项目中所有文件的绝对路径列表。
我试过了,但我只能得到 Activity 窗口的路径..
我的操作代码是:
IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
if (file == null)
try {
throw new FileNotFoundException();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String path = file.getRawLocation().toOSString();
System.out.println("path: " + path);
这里我只获取 Activity 窗口的路径..但我想要项目中所有文件的绝对路径列表..主要是 src 文件夹下的文件...
请指导我是否可以用相同的方式来做,或者我是否需要为此使用一些不同的 API。
最佳答案
经过研究,我发现下面的代码可以获取 Eclipse 当前工作区的项目目录的路径:
//get object which represents the workspace
IWorkspace workspace = ResourcesPlugin.getWorkspace();
//get location of workspace (java.io.File)
File workspaceDirectory = workspace.getRoot().getLocation().toFile()
注意:您需要导入 org.eclipse.core.resources
和 org.eclipse.core.runtime
才能使用这些 API
关于java - 如何使用插件获取eclipse中项目文件的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15246736/