访问类路径servlet中的文件

时间:2013-04-03 14:40:01

标签: java java-ee servlets

在DynamicWebProject中 - Eclipse,如何获取src文件夹中存在的文件的文件对象,我的意思是说类路径。

下面的程序运行正常,但是wtpwebapps中存在对文件的新更新,而我只想在Eclipse的src文件夹中的文件中进行更改(如何获取类路径中存在的文件的FileOutputStream)。

                String filename1 = "/WEB-INF/classes/server123.properties";
                ServletContext context = req.getSession().getServletContext();
                String path1 = context.getRealPath(filename1);


            System.out.println("PATH 1 IS :"+path1);
            FileInputStream fis1 = new FileInputStream(new File(path1));
            int ch1;
            while ((ch1 = fis1.read())!=-1) {
                System.out.print((char)ch1);
            }
            fis1.close();

            FileOutputStream fos = new FileOutputStream(path1);
            fos.write("jayesh".getBytes());
            fos.close();

1 个答案:

答案 0 :(得分:0)

你可以在你的代码中使用getServletContext()/ filename,即

properties.load(new FileInputStream(getServletContext().getRealPath("/filename")));

properties.load(new FileInputStream(getServletContext().getRealPath(/filename")));