RCP 文件路徑問題

自己慢慢添加相關內容的筆記

內部IFile(org.eclipse.core.resources.IFile) 轉化成File (java.io.File) 可用於xml解析

org.eclispe.core.resources.IFile iFile = new IFile(....);

java.io.File file = iFile.getLocation().toFile();

以下是轉載原文

終於找到直接可以用的了 

Java代碼 
  1. path = FileLocator.toFileURL(  
  2.         Platform.getBundle(IAppConstants.APPLICATION_ID)  
  3.                 .getEntry("")).getPath().toString();  



RCP 文件路徑問題 
String path =FileLocator.toFileURL(Platform.getBundle("工程名").getEntry("")).getPath 

得到eclipse rcp plugin 內目錄文件絕對路徑的方法 
import java.io.IOException; 
import java.net.URL; 

import org.eclipse.core.runtime.FileLocator; 
import org.eclipse.core.runtime.Platform; 
import org.osgi.framework.Bundle; 

import com.work.base.exception.DataException; 
import com.work.view.Activator; 

public class BundlePathUtil { 

    public static String getRealPath(String bundleID, String entry) 
            throws DataException { 
        URL urlentry; 
        String strEntry; 
        try { 
            Bundle bundle = Platform.getBundle(bundleID); 
            if (bundle==null) 
                throw new DataException("請檢查文件的路徑",new NullPointerException()); 
            // get path URL 
            urlentry = bundle.getEntry(entry); 
            if (urlentry==null) 
                throw new DataException("請檢查文件的路徑",new NullPointerException()); 
            strEntry = FileLocator.toFileURL(urlentry).getPath(); 
        } catch (IOException e1) { 
            throw new DataException("請檢查文件的路徑", e1); 
        } 
        return strEntry; 
    } 
    
    public static String getPluginPath(){        
        return Activator.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath(); 
        
    }   
    
} 

另外,獲取插件/rcp 的workspace的路徑: 


Platform.getInstanceLocation().getURL().getPath() 




從插件/RCP中取得文件路徑的方法 

最近社區裏問這個問題的人特別多,所以在這裏將自己用到的幾個方法寫出來。假如以後還有其他的方法,會進行更新。 

從插件中獲得絕對路徑: 
    AaaaPlugin.getDefault().getStateLocation().makeAbsolute().toFile().getAbsolutePath()); 


通過文件得到Project: 

IProject project = ((IFile)o).getProject(); 


通過文件得到全路徑: 

String path = ((IFile)o).getLocation().makeAbsolute().toFile().getAbsolutePath(); 



得到整個Workspace的根: 

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


從根來查找資源: 

IResource resource = root.findMember(new Path(containerName)); 

從Bundle來查找資源: 

Bundle bundle = Platform.getBundle(pluginId); 
URL fullPathString = BundleUtility.find(bundle, filePath); 


得到Appliaction workspace: 

Platform.asLocalURL(PRODUCT_BUNDLE.getEntry("")).getPath()).getAbsolutePath(); 

得到runtimeworkspace: 
Platform.getInstanceLocation().getURL().getPath(); 

從編輯器來獲得編輯文件: 

IEditorPart editor = ((DefaultEditDomain)(parent.getViewer().getEditDomain())).getEditorPart(); 
IEditorInput input = editor.getEditorInput(); 
if(input instanceof IFileEditorInput){ 
IFile file = ((IFileEditorInput)input).getFile(); 

}

文章原址:RCP 文件路徑問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章