简体   繁体   English

访问类路径servlet中存在的文件

[英]access File present in classpath servlet

In DynamicWebProject - Eclipse, How to get File Object for file present in src folder, i mean to say class path. 在DynamicWebProject-Eclipse中,如何获取src文件夹中存在的文件的文件对象,我的意思是说类路径。

Below program works fine but new update to file is present in wtpwebapps, instead i want to get changed in file present in src folder of Eclipse only(How to get FileOutputStream for file present in classpath). 下面的程序工作正常,但是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();

you can use getServletContext()/filename in your code ie 您可以在代码中使用getServletContext()/ filename,即

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

or 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM