如何以编程方式在 Eclipse 中的编辑器上打开文件?
How to programatically open a file on Editor in eclipse?
在 Java 中,我尝试使用 Eclipse 在编辑器上以编程方式打开文件。编辑器是 IFileEditorInput 的实例对我来说很重要。我使用了以下代码:
IPath path = new Path(filePath);
System.out.println("PATH:"+path);
IFile ifile=ResourcesPlugin.getWorkspace().getRoot().getFile(path);
System.out.println("IFILE: "+ifile);
//IFileEditorInput editorInput= new FileEditorInput(ifile);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditor(page, ifile);
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
后面某个时候需要访问文件,具体在Editorclass的init()方法中,如下,
@Override //IEditor
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
if (input instanceof IFileEditorInput) {
try {
setSite(site);
setInput(input);
IFile ifile=((IFileEditorInput)input).getFile();
File file=null;
if(ifile.getLocation()==null)
{
System.out.println("file location is NULL..exiting");
System.exit(0);
}
else
file = ifile.getLocation().toFile();
问题是 ifile.getLocation() 总是 returns null,因此,我无法使用 File class。我做错了什么?谢谢。
编辑: 我程序的输出是:
PATH:D:/EbticDATA/Etisalat/Zaid/.EBTIC8/Service_Corridor.xml
IFILE: L/EbticDATA/Etisalat/Zaid/.EBTIC8/Service_Corridor.xml
file location is NULL..exiting
你给的路径
ResourcesPlugin.getWorkspace().getRoot().getFile(path);
必须是相对于工作区中存在的文件的工作区根目录的路径。
您可以使用getFileForLocation
指定一个完整的文件系统路径:
ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
但是这只有在生成的文件在工作区中时才有效。
通常 IFile
仅适用于工作区中的文件 - 但您可以创建 'links' 工作区外的文件。
如果要编辑不在工作区中的文件,请使用
public static IEditorPart openEditor(IWorkbenchPage page, URI uri,
String editorId, boolean activate)
IDE
方法传入要编辑的文件的 URI
和编辑器 ID。
在这种情况下,在编辑器中 IEditorInput
将是 IURIEditorInput
的一个实例, 而不是 IFileEditorInput
.
在 Java 中,我尝试使用 Eclipse 在编辑器上以编程方式打开文件。编辑器是 IFileEditorInput 的实例对我来说很重要。我使用了以下代码:
IPath path = new Path(filePath);
System.out.println("PATH:"+path);
IFile ifile=ResourcesPlugin.getWorkspace().getRoot().getFile(path);
System.out.println("IFILE: "+ifile);
//IFileEditorInput editorInput= new FileEditorInput(ifile);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditor(page, ifile);
} catch (PartInitException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
后面某个时候需要访问文件,具体在Editorclass的init()方法中,如下,
@Override //IEditor
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
if (input instanceof IFileEditorInput) {
try {
setSite(site);
setInput(input);
IFile ifile=((IFileEditorInput)input).getFile();
File file=null;
if(ifile.getLocation()==null)
{
System.out.println("file location is NULL..exiting");
System.exit(0);
}
else
file = ifile.getLocation().toFile();
问题是 ifile.getLocation() 总是 returns null,因此,我无法使用 File class。我做错了什么?谢谢。
编辑: 我程序的输出是:
PATH:D:/EbticDATA/Etisalat/Zaid/.EBTIC8/Service_Corridor.xml
IFILE: L/EbticDATA/Etisalat/Zaid/.EBTIC8/Service_Corridor.xml
file location is NULL..exiting
你给的路径
ResourcesPlugin.getWorkspace().getRoot().getFile(path);
必须是相对于工作区中存在的文件的工作区根目录的路径。
您可以使用getFileForLocation
指定一个完整的文件系统路径:
ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
但是这只有在生成的文件在工作区中时才有效。
通常 IFile
仅适用于工作区中的文件 - 但您可以创建 'links' 工作区外的文件。
如果要编辑不在工作区中的文件,请使用
public static IEditorPart openEditor(IWorkbenchPage page, URI uri,
String editorId, boolean activate)
IDE
方法传入要编辑的文件的 URI
和编辑器 ID。
在这种情况下,在编辑器中 IEditorInput
将是 IURIEditorInput
的一个实例, 而不是 IFileEditorInput
.