目前我的桌面上有一个文件。我将其称为
private static String fileListFileName= "/home/sowmya/Desktop/test.txt";
我将文件添加到WEB-INF / model文件夹,我需要引用WEB-INF / model /文件夹中的文件 如何在我的Java文件中引用该文件路径?
答案 0 :(得分:1)
您可以执行此操作来检查WEB-INF
文件夹
String [] f=new File(getServletContext().getRealPath("WEB-INF")).list();
如果您有以下情况并想要阅读属性文件
SampleApp.war
|
|-------- myApp.properties
|
|-------- WEB-INF
|
|---- classes
|
|----- org
|------ myApp
|------- MyPropertiesReader.class
你必须这样做
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream("myApp.properties");
Properties properties = new Properties();
System.out.println("InputStream is: " + inputStream);
// load the inputStream using the Properties
properties.load(inputStream);
您也可以从ServletContext
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletContext servletContext = getServletContext();
String contextPath = servletContext.getRealPath(File.separator);
PrintWriter out = response.getWriter();
out.println("<br/>File system context path (in TestServlet): " + contextPath);
}
另一种方式
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream stream = classLoader.getResourceAsStream("FileName");
if (stream == null) {
// File not nound
} else {
// User stream object
}