I am creating a file in java using
BufferedWriter out = new BufferedWriter(new FileWriter(FileName));
StringBuffer sb=new StringBuffer();
sb.append("\n");
sb.append("work");
out.write(sb.toString());
out.close();
But this file is getting created inside the bin folder of my server.I would like to create this file inside a user-defined folder.
How can it be achieved.
The simplest approach is to specify a fully qualified path name. You could select that as a
Fileand build a newFilerelative to it:Note:
FileOutputStreamwrapped in anOutputStreamWriterinstead of usingFileWriter, as you can’t specify an encoding withFileWritertry/finallyblock (or try-with-resources in Java 7) so that you always close the writer even if there’s an exception.