Usually when your application is supposed to write files it must do so in a place where it has write access. The only place where you can be sure (in most cases) that your app will be allowed to have such access is the user home directory. You might have noticed that other java based applications usually create a directory named ".some-application"
in your home folder (in your case "C:\Users\Rohan Kandwal"
). The reason (one of them) why they do so is write access.
You should do the same and forget about using Class.getResourceAsStream(...)
for writeable files. You can get the path to your home directory via System.getProperty("user.home")
.
Note taht the reason why Class.getResourceAsStream(...)
as used in your example is getting a file from your build
directory is because you are running it within your IDE. If you run it outside the IDE this path will change. This is because the path to the class file (xmlhandler.class
) will most likely change and Class.getResourceAsStream(...)
relies on it to find the resource.
Also note that your src
directory will most likely not even exist when the application is deployed. Even if it did, there's no way to know if your users will have it deployed in a location with write access. So what you are trying to do doesn't make much sense.
Edit01
This is an example of how you could do what you want. This will create a home directory for your application where you can write files any way you wish. If the file you want to preserve between application runs does not exist, it will be created and filled with the content of your resource file. Note that this is just an example.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ConfigDir {
private static final String APP_DIR = ".myappdir";
private static final String FILE_NAME = "shutdownscheduler.xml";
private File fillFileFromTemplate(InputStream template, File file) throws FileNotFoundException, IOException {
OutputStream out = null;
try {
out = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = template.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
return file;
} catch (FileNotFoundException ex) {
throw ex;
} finally {
if (out != null) {
out.close();
}
}
}
public static void main(String[] argv) throws IOException {
String userdir = System.getProperty("user.home"); // user home directory
// OR if you know that your program will run from a directory that has write access
// String userdir = System.getProperty("user.dir"); // working directory
if (!userdir.endsWith(System.getProperty("file.separator"))) {
userdir += System.getProperty("file.separator");
}
String myAppDir = userdir + APP_DIR;
File myAppDirFile = new File(myAppDir);
myAppDirFile.mkdirs(); // create all dirs if they do not exist
String myXML = myAppDir + System.getProperty("file.separator") + FILE_NAME;
File myXMLFile = new File(myXML); // this is just a descriptor
ConfigDir cd = new ConfigDir();
if (!myXMLFile.exists()) {
// if the file does not exist, create one based on your template
// replace "ConfigDir" with a class that has your xml next to it in src dir
InputStream template = ConfigDir.class.getResourceAsStream(FILE_NAME);
cd.fillFileFromTemplate(template, myXMLFile);
}
// use myXMLFile java.io.File instance for read/write from now on
/*
DocumentBuilderFactory documentbuilderfactory=DocumentBuilderFactory.newInstance();
DocumentBuilder documentbuilder=documentbuilderfactory.newDocumentBuilder();
Document document=(Document)documentbuilder.parse(myXMLFile);
document.getDocumentElement();
...
*/
}
}