Java Servlet找不到文件

Java Servlet找不到文件,java,xml,Java,Xml,我有一个ServiceHandler类,我想本质上读入一个文件。在my init()函数中: private String languageDataSet = null; // This variable is shared by all HTTP requests for the servlet private static long jobNumber = 0; // The number of the task in the async queue private

我有一个ServiceHandler类,我想本质上读入一个文件。在my init()函数中:

    private String languageDataSet = null; // This variable is shared by all HTTP requests for the servlet
    private static long jobNumber = 0; // The number of the task in the async queue

    private File f;

    public void init() throws ServletException {
        ServletContext ctx = getServletContext(); // Get a handle on the application context
        languageDataSet = ctx.getInitParameter("LANGUAGE_DATA_SET"); // Reads the value from the <context-param> in web.xml

        // You can start to build the subject database at this point. The init() method is only ever called once during the life cycle of a servlet
        f = new File(languageDataSet);
    }

它似乎找不到该文件,因为我已验证该文件中有文本,因此它不应显示为0。知道为什么文件看起来找不到吗?(该文件与web.xml文件位于同一目录中)。

该文件位于何处?在罐子里/战争?如果它在应用程序容器中,则需要将其作为类路径资源而不是文件系统文件获取。@Compass我将该文件与web.xml-WebContent->web-INF放在同一目录中。那么它不是文件。它是war文件中的资源。文件存在于文件系统中。相对路径从当前目录解析,即服务器JVM启动的目录。用于使用中记录的路径读取此资源。如果您使用的是Spring,还可以利用资源解析程序:文件位于何处?在罐子里/战争?如果它在应用程序容器中,则需要将其作为类路径资源而不是文件系统文件获取。@Compass我将该文件与web.xml-WebContent->web-INF放在同一目录中。那么它不是文件。它是war文件中的资源。文件存在于文件系统中。相对路径从当前目录解析,即服务器JVM启动的目录。用于使用中所述的路径读取此资源。如果您使用的是Spring,还可以利用资源解析程序:
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   out.print("Language Dataset is located at " + languageDataSet + " and is <b><u>" + f.length() + "</u></b> bytes in size");
}
  <context-param>
    <param-name>LANGUAGE_DATA_SET</param-name>
    <param-value>wili-2018-Edited.txt</param-value>
  </context-param>
Language Dataset is located at wili-2018-Edited.txt and is 0 bytes in size