-1

I have the following code to write a string on a file (relative path loaded):

void writeFile(String string){
        try {
            //ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            ClassLoader classLoader = getClass().getClassLoader();
            File newFile = new File(classLoader.getResource("conf/passwords.txt").getFile());

            //Path of "passwords.txt" where it writes the info
            System.out.println("ABSOLUTE PATH: " +newFile.getAbsolutePath());

            FileWriter flux = new FileWriter(newFile);
            BufferedWriter writer = new BufferedWriter (flux);
            writer.write(string);

            writer.close();
        } catch (IOException e) {
            System.out.println("ERROR on file writing.\n" +e.getMessage());
            e.printStackTrace();
        }
    }

It works, but the string is not written on my original file that is located on the project path, as it follows: src/conf/passwords.txt

Instead of this, the string is saved on the following path: C:\Users\becario01\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\searchLef\WEB-INF\classes\conf\passwords.txt

I want to write the string on my original file, to show easily while I´m working on my workspace.

I would thank a lot any helpful reply.

Regards.

javabrett
  • 7,020
  • 4
  • 51
  • 73
César
  • 1
  • 2
  • 1
    Well, you've found the issue by yourself: you should provide a full path because the "root" folder points to the output `/classes` folder. – Nir Alfasi May 07 '15 at 15:59
  • 1
    [This](http://stackoverflow.com/questions/14209085/how-to-define-a-relative-path-in-java) may help you, or possibly [this](http://stackoverflow.com/questions/437382/how-do-relative-file-paths-work-in-eclipse) – Avantol13 May 07 '15 at 16:00
  • The approach you've chosen, to do it via the `ClassLoader.getResource()` method, it should be noted, will work if you do it in an "exploded WAR" situation, but will not work if the WAR is not exploded when it is deployed. If you're using Tomcat (as I'm guessing you are but could be wrong), you should do it relative to the environment variable `catalina.base` and not relative to the `ClassLoader`. – dcsohl May 07 '15 at 16:03

1 Answers1

0

To summarize, it looks like you are running Tomcat under Eclipse WST, your webapp writes-out a file at some point, and you want that file to appear in your Eclipse project source-code tree where you can easily reload/review it. In that case, as you have seen, using a classloader obtain a location for the file will result in it appearing in the webapp deployed runtime, rather than in your Eclipse project source.

What you want to do is to pass-in your Eclipse project location to your webapp somehow, then use that absolute path to open your File. There's a number of ways to do this - a simple one would be to change the Eclipse Tomcat launch to add a JVM property e.g. -Declipse.project.path=${workspace_loc}/searchLef, then read that system property and use it as the base path for your file, adding the relative path src/conf/passwords.txt. The file will then appear inside your source tree after you manually refresh the project.

javabrett
  • 7,020
  • 4
  • 51
  • 73