简体   繁体   中英

How to get and use the path of a folder in eclipse project?

I create a new folder Fold inside my eclipse project Proj. How do I get the path of Fold relative to Proj ? This folder will be used as place to store serialized objects. Will I be able to serialize and de-serialize my code using this relative path ?

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("path/folder");

or

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

This code gets the path -

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM