Home >Backend Development >C#.Net Tutorial >Apply absolute and relative paths
This problem depends on where your configuration file is placed. If it is placed in the Classes directory (or subdirectory) of the project, you can use **.Class.getResource('relative path') to get the configuration file path .If it is another directory, then you can only obtain the project root directory + configuration file directory through ServletContext when the project is started to determine the path. And put the path to a place where the class file can be referenced.
The following is when I am working on the project A class written to obtain the path may not be well written. But I still hope it will be helpful to you:
package com.example.web;
import java.io.File;
import java.net. URL;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
/**
* Path acquisition class
**/
public class WebPath {
/**
* Get the absolute path to the project root directory
*
* @return such as: F:TongJianpengJ2EEUtil
**/
public static String getAbsolutePathWithProject () {
return System.getProperty("user.dir");
}
/**
* Get the drive letter where the project is located
**/
public static String getDriverPathWithProject() {
return new File("/").getAbsolutePath();
}
/**
* Get the absolute path to the project root directory
*
* @return the project root directory. For example
F:tomcatwebappsJ2EEUtil
**/
public static String getAbsolutePathWithWebProject(
HttpServletRequest request) {
return request.getSession().getServletContext().getRealPath("/");
}
/**
* Get the absolute path of the specified directory under the project root directory
*
* @param The specified directory under the project root directory
* * . For example: /login/
* @return The specified directory under the project root directory. For example:
F:tomcatwebappsJ2EEUtillogin
**/
public static String getAbsolutePathWithWebProject(
HttpServletRequest request, String path) {
return request.getSession().getServletContext().getRealPath(path);
}
/**
* Get the absolute path to the project root directory
*
* @return the project root directory. For example
F:tomcatwebappsJ2EEUtil
**/
public static String getAbsolutePathWithWebProject(ServletContext context) {
return context.getRealPath("/");
}
/**
* Get the absolute path of the specified directory under the project root directory
*
* @param The specified directory under the project root directory
* * . For example: /login/
* @return The specified directory under the project root directory. For example:
F:tomcatwebappsJ2EEUtillogin
**/
public static String getAbsolutePathWithWebProject(ServletContext context,
String path) {
return context.getRealPath( path);
}
/**
* Get the absolute path of the project classpath directory
*
* @return the absolute path of the classes directory
* * file:/F:/tomcat/webapps/J2EEUtil/WEB-INF/classes/
**/
public static URL getAbsolutePathWithClass() {
return WebPath.class.getResource("/");
}
/**
* Get the absolute path to the specified directory under the project classPath directory
*
* @param path
* The specified directory under the classes directory. For example:/com/
* @return file:/F:/tomcat/webapps/J2EEUtil/ WEB-INF/classes/com/
**/
public static URL getAbsolutePathWithClass(String path) {
return WebPath.class.getResource(path);
}
/**
* Get the absolute path of the directory where the specified class file is located
*
* @param clazz
* Class
* @return The absolute path of the class file. For example:
Package com.Aries.Util.Web Main.java class.
**/
public static URL getAbsolutePathWithClass(Class clazz) {
return clazz.getResource("");
}
}