2013-04-03 43 views
0

我在我的eclipse項目Proj中創建了一個新文件夾Fold。如何獲得相對於Proj的摺疊路徑?該文件夾將用作存儲序列化對象的地方。我能用這個相對路徑序列化和反序列化我的代碼嗎?如何在eclipse項目中獲取和使用文件夾的路徑?

+0

右鍵單擊您的文件夾,以獲取其絕對路徑和從項目文件夾它的路徑。 'System.getResouce(「user.dir」);'給出項目的路徑。 – Justin

回答

0

這段代碼獲得路徑 -

String absolutePath = new File(".").getAbsolutePath(); 
System.out.println(absolutePath);// Shows you the path of your Project Folder 
int last = absolutePath.length()-1; 
absolutePath = absolutePath.substring(0, last);//Remove the dot at the end of path 
System.out.println(absolutePath); 
String filePath = "MyFolderInsideEclipseProject\\file.txt";//You know this 
System.out.println(absolutePath + filePath);//Get the full path. 
0
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
URL url = classLoader.getResource("path/folder"); 

URL url = getClass().getResource("path/folder"); 
+0

我不知道這些事情,但我期待答案將包含有關項目文件夾,用戶文件夾等的一些信息。看起來像只有當你知道絕對路徑時,你的答案纔有效。我知道絕對。我需要知道如何獲得相對路徑,並讓Java代碼能夠找到該路徑指定的文件夾。 –

+1

檢查鏈接http://stackoverflow.com/questions/11073702/java-file-relative-path-in-eclipse – Biswajit

+0

無論我放哪個路徑,代碼都會返回null。 –

相關問題