Eclipse 插件:创建一个新文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1624054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse plugin: create a new file
提问by erwan
I'm trying to create a new file in an eclipse plugin. It's not necessarily a Java file, it can be an HTML file for example.
我正在尝试在 eclipse 插件中创建一个新文件。它不一定是 Java 文件,例如可以是 HTML 文件。
Right now I'm doing this:
现在我正在这样做:
IProject project = ...;
IFile file = project.getFile("/somepath/somefilename"); // such as file.exists() == false
String contents = "Whatever";
InputStream source = new ByteArrayInputStream(contents.getBytes());
file.create(source, false, null);
The file gets created, but the problem is that it doesn't get recognized as any type; I can't open it in any internal editor. That's until I restart Eclipse (refresh or close then open the project doesn't help). After a restart, the file is perfectly usable and opens in the correct default editor for its type.
文件被创建,但问题是它没有被识别为任何类型;我无法在任何内部编辑器中打开它。直到我重新启动 Eclipse(刷新或关闭然后打开项目没有帮助)。重新启动后,该文件完全可用,并在正确的默认编辑器中打开。
Is there any method I need to call to get the file outside of that "limbo" state?
有没有我需要调用的方法来获取该“limbo”状态之外的文件?
采纳答案by VonC
That threaddoes mention the createFile
call, but also refers to a FileEditorInput
to open it:
该线程确实提到了createFile
调用,但也提到了FileEditorInput
打开它:
Instead of
java.io.File
, you should useIFile.create(..)
orIFile.createLink(..)
. You will need to get anIFile
handle from the project usingIProject.getFile(..)
first, then create the file using that handle.
Once the file is created you can createFileEditorInput
from it and useIWorkbenchPage.openEditor(..)
to open the file in an editor.
取而代之的是
java.io.File
,您应该使用IFile.create(..)
或IFile.createLink(..)
。您需要首先IFile
使用从项目中获取句柄IProject.getFile(..)
,然后使用该句柄创建文件。
创建文件后,您可以从中创建FileEditorInput
并用于IWorkbenchPage.openEditor(..)
在编辑器中打开文件。
Now, would that kind of method (from this AbstractExampleInstallerWizard
) be of any help in this case?
现在,这种方法(来自 this AbstractExampleInstallerWizard
)在这种情况下有什么帮助吗?
protected void openEditor(IFile file, String editorID) throws PartInitException
{
IEditorRegistry editorRegistry = getWorkbench().getEditorRegistry();
if (editorID == null || editorRegistry.findEditor(editorID) == null)
{
editorID = getWorkbench().getEditorRegistry().getDefaultEditor(file.getFullPath().toString()).getId();
}
IWorkbenchPage page = getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.openEditor(new FileEditorInput(file), editorID, true, IWorkbenchPage.MATCH_ID);
}
See also this SDOModelWizard
opening an editor on a new IFile
:
另请参阅SDOModelWizard
在 new 上打开编辑器IFile
:
// Open an editor on the new file.
//
try
{
page.openEditor
(new FileEditorInput(modelFile),
workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId());
}
catch (PartInitException exception)
{
MessageDialog.openError(workbenchWindow.getShell(), SDOEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
return false;
}