17

現在編集中のファイルを Eclipse で処理するプラグインを作成したいと考えています。しかし、ファイルのフルパスを正しく取得する方法がわかりません。

これは私が今していることです:

IFile file = (IFile) window.getActivePage().getActiveEditor.getEditorInput().
    getAdapter(IFile.class);

これで IFile オブジェクトができたので、そのパスを取得できます。

file.getFullPath().toOSString();

ただし、これでもワークスペースへの相対パスのみが表示されます。そこから絶対パスを取得するにはどうすればよいですか?

4

6 に答える 6

22

あなたがしたいように見えますIResource.getRawLocation()。これは、絶対パスを取得したことを二重に確認したい場合IPathのメソッドもあります。makeAbsolute()

于 2008-11-18T18:15:50.030 に答える
6

よりJavaに適した解決策は、以下を使用することだと思います。

IResource.getLocation().toFile()

これは、IPath API(getLocation()部分)を利用して、java.io.Fileインスタンスを返します。もちろん、他の答えはおそらくあなたがなりたい場所にあなたを連れて行くでしょう。

接線のメモで、私はIDEクラス(org.eclipse.ui.ide.IDE)がエディターに関しては有用なユーティリティリソースだと思います。

于 2009-09-02T23:50:20.520 に答える
5

私にとってうまくいった答え(そして私はそれをテストしました!)は次のとおりです。

// Get the currently selected file from the editor
IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart(); 
IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
if (file == null) throw new FileNotFoundException();
String path = file.getRawLocation().toOSString();
System.out.println("path: " + path);
于 2012-02-15T19:08:25.707 に答える
1

私は通常、IPath を返す IFile.getLocation() を呼び出してから、IPath.toOSString() を呼び出します。

file.getLocation().toOSString()
于 2008-11-19T17:23:37.297 に答える
0
IWorkspace ws      = ResourcesPlugin.getWorkspace();  
IProject   project = ws.getRoot().getProject("*project_name*");

IPath location = new Path(editor.getTitleToolTip());  
IFile file     = project.getFile(location.lastSegment());

into file.getLocationURI() it's the absolute path
于 2008-12-08T14:22:35.580 に答える
-2

私にとっては、これで問題ありません。

IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot();

ファイル file = workSpaceRoot.getRawLocation().makeAbsolute().toFile();

この場所からのファイルリスト:

File[] ファイル = file.listFiles();

于 2014-04-02T20:32:56.853 に答える