项目中可能需要读取项目路径下的模板等,我们可能需要知道模板的绝对路径。可以通过如下两种方法获取:
public void testGetPath() throws IOException {
/**
* 获取项目的路径
*/
// 1st projectPath==E:\Hunger\workspace\twpre
String projectPath = System.getProperty("user.dir");
System.out.println("projectPath==" + projectPath);
// 2nd projectPath2==E:\Hunger\workspace\twpre
File dir = new File("");// 参数为空
String projectPath2 = dir.getCanonicalPath();
System.out.println("projectPath2==" + projectPath2);
}
比如我的项目templates下有两个模板,如下图:
twapi_with_birthday_gender.ftl的绝对路径为:
String absPath = System.getProperty("user.dir") + "/src/main/resources/templates/twapi_with_birthday_gender.ftl";