3

I am writing a application that will run inside JBoss EAP 6.3.1 on a CentOS 6.5

During this application i have to save a file to the disk and when restarting the application i have to read it back into the application.

All this is working.

The problem is that i want to save to file in the working directory of the application.

What is happening right now is that the file: foo.bar will be saved at the location where i run the standalone.sh (or .bat on Windows).

public void saveToFile() throws IOException {
    String foo = "bar";

    Writer out = new OutputStreamWriter(new FileOutputStream("/foo.bar"), "UTF-8");
    try {
        out.write(foo);
    } finally {
        out.close();
    }
}
Cirou
  • 1,420
  • 12
  • 18
TheMiXeD
  • 125
  • 1
  • 1
  • 7

1 Answers1

2

You could try to use an absolute path to save your file:

String yourSystemPath = System.getProperty("jboss.home.url") /*OPTIONAL*/ + "/want/to/save/here";
File fileToSave = new File(yourSystemPath,"foo.bar");  
Writer out = new OutputStreamWriter(new FileOutputStream(fileToSave), "UTF-8");

Basically here, I'm creating a File object using a yourSystemPath variable where I stored the path to save the file in, then I'm creating the new FileOutputStream(fileToSave) using the previously created object File

Please ensure that your JBoss server has write permissions for yourSystemPath

Cirou
  • 1,420
  • 12
  • 18
  • This would be indeed the solution if i wanted to save the file at a fix place. I more would like to solution to write in the working directory where the application is running. – TheMiXeD Nov 03 '14 at 11:02
  • So what do you mean with "i want to save to file in the working directory of the application" ? Isn't always the same path? – Cirou Nov 03 '14 at 11:06
  • Every application deployed on a JBoss server gets it's own working directory. When i would deploy the same application 4 times there should be 4 working directory's for each application one. The application have to stay within the boundaries of JBoss – TheMiXeD Nov 03 '14 at 11:11
  • Ok I got it... so, I think you can use the jboss constant to get the jboss root path with `System.getProperty("jboss.home.url")` to build your path, so this will work on every system/server. I modified my answer after comments – Cirou Nov 03 '14 at 11:12
  • With your help i found this link to support it: [link](https://developer.jboss.org/wiki/JBossProperties) – TheMiXeD Nov 03 '14 at 11:34
  • Yes, that's it. As you can see you can relate to a lot of properties to get your path ;) – Cirou Nov 03 '14 at 11:36