通過FileSystem獲取插件運行時的文件和文件夾。
public static File toLocalFile(String folderName){
try {
URL url = Activator.getDefault().getBundle().getEntry(folderName);
url = FileLocator.toFileURL(url);
IFileSystem fs = EFS.getLocalFileSystem();
IFileStore file = fs.getStore(new Path(url.getPath()));
IFileStore fileStore = file.getFileStore(new Path(""));
File file2 = fileStore.toLocalFile(EFS.NONE, null);
return file2;
} catch (IOException e) {
e.printStackTrace();
} catch (CoreException e) {
e.printStackTrace();
}
return null;
}
需要把org.eclipse.core.filesystem這個jar包加入lib中,還要在dependencies選項卡中把這個jar包添加進去,否則會報classnotfoundexception。
轉載自:qq123zhz