我正在尝试在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);

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

我是否需要调用任何方法来将文件置于“limbo”之外?状态?

有帮助吗?

解决方案

主题确实提到了 createFile 调用,但也指一个 FileEditorInput 来打开它:

  

而不是 java.io.File ,您应该使用 IFile.create(..) IFile.createLink(..) 。您需要首先使用 IProject.getFile(..)从项目中获取 IFile 句柄,然后使用该句柄创建文件。
  创建文件后,您可以从中创建 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);
  }  

另见 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;
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top