![](/img/trans.png)
[英]How do I get the classpath, as file paths, of an IJavaProject in an Eclipse plugin?
[英]How do I get the File API to look at my classpath?
我正在eclipse項目中讀取ascii文件。
默認輸出文件夾設置為
my_proj/classes
它在我的源路徑上,在我構建時進入我的類目錄。 一切都好。 但是,當我嘗試通過執行以下操作來獲取代碼的句柄時
File myFile = new File("myFile.txt")
它不起作用,因為該API會查看我的項目根目錄。 不在我的課程目錄中。
當我做:
System.out.println("Current path is = " + new File(".").getAbsolutePath ());
項目roo回來了,而不是我的課程目錄。
因此,我無法處理我的文件。
我只想將文件放在類路徑上,並讓代碼來拾取它
有小費嗎?
您可以使用getClass().getClassLoader().getResource("Your_File")
然后使用類似
File file=new File(url.toURI());
編輯:如Sotirios Delimanolis和jb-nizet所指出的,如果“ Your_File”不是文件系統上的文件,而是捆綁在jar中,在這種情況下,您將不得不使用inputstream getClass().getClassLoader().getResourceAsStream("Your_File")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.