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.