Eclipse插件:创建一个新文件

erw*_*wan 8 eclipse eclipse-plugin file

我正在尝试在eclipse插件中创建一个新文件.它不一定是Java文件,例如它可以是HTML文件.

现在我这样做:

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);
Run Code Online (Sandbox Code Playgroud)

文件被创建,但问题是它不会被识别为任何类型; 我无法在任何内部编辑器中打开它.直到我重新启动Eclipse(刷新或关闭然后打开项目没有帮助).重新启动后,该文件完全可用,并在其类型的正确默认编辑器中打开.

有什么方法我需要调用才能将文件置于"无效"状态之外吗?

Von*_*onC 7

线程确实提到了这个createFile调用,但也指的FileEditorInput是打开它:

而不是java.io.File,你应该使用IFile.create(..)IFile.createLink(..).您需要先IFile使用项目获取句柄IProject.getFile(..),然后使用该句柄创建文件.
创建文件后,您可以从中创建FileEditorInput并使用IWorkbenchPage.openEditor(..)在编辑器中打开文件.

现在,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);
  }  
Run Code Online (Sandbox Code Playgroud)

另见SDOModelWizard打开一个新的编辑器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;
  }
Run Code Online (Sandbox Code Playgroud)