0

In DynamicWebProject - Eclipse, How to get File Object for file present in src folder, i mean to say class path.

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).

                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();
Jayesh
  • 6,047
  • 13
  • 49
  • 81
  • 1
    why not loading with getClass().getClassLoader().getResourceAsStream(path) from within the classpath ? – BigMike Apr 03 '13 at 14:52
  • BigMike: FileOutputStream fos = new FileOutputStream("placeholder"); can you just tell me what to write in "placeholder" for accessing file present in classpath, so that whatever I make changes to file will get affected to file present in src folder and not into wtpwebapps(when we run through eclipse). – Jayesh Apr 03 '13 at 15:13
  • getResourceAsStream() gives you read access to something in classpath. For having an output stream you can use getResource() like in the chosen answer of http://stackoverflow.com/questions/2797367/write-to-a-file-stream-returned-from-getresourceasstream btw, when working with webapplication I usually store writable files in a common directory (out of application server domain), because each AS has its things with classpath... – BigMike Apr 03 '13 at 15:20
  • BigMike: thanks for pointing the right link, this is what I am need of. I will check and update. Thanks – Jayesh Apr 03 '13 at 15:22

1 Answers1

0

you can use getServletContext()/filename in your code i.e.

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

or

properties.load(new FileInputStream(getServletContext().getRealPath(/filename")));
Biswajit
  • 2,434
  • 2
  • 28
  • 35
  • getServletContext() ? guess there's a mispell in here ^^ – BigMike Apr 03 '13 at 15:00
  • Biswajit: I am not facing an issue while reading. my question is when I write to a file, it actually write into file present in wtpwebapps (present in .metadata folder when you run project using eclipse) instead actual file present in src folder of my project. – Jayesh Apr 03 '13 at 15:15
  • Jayesh Patel:You are right.When you get the classpath it return the path where this compile file store. It's good for you.Because of you will not face any problem when you put the code in some another pc.You have not worry about the path of this file. At last What you want to do? can you explain your purpose?so i can help you. – chintan Apr 03 '13 at 15:28