I am creating a file called jfv.properties and I want to write a simple string into the file. In this the file is getting created the string is not getting printed. Is there problem in the below code ? I have run in the debug mode, there are no exceptions thrown.
File file = new File(filePath,"jfv.properties");
FileOutputStream fo = null;
try {
fo = new FileOutputStream(file);
PrintWriter p = new PrintWriter(fo);
p.println("some string");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (SecurityException s){
s.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(fo != null ){
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The
PrintWriteris not flushed. Useor use
autoFlushor just
closeit.autoFlushworks forprintln,printfandformat. See the javadoc of PrintWriter.Details
The
PrintWritercan be constructed either with anotherWriter, aFileor filename or anOutputStream. In case of instantiating it using anOutputStreamthe javadoc says:and the javadoc of OutputStreamWriter says:
EDIT
So your code
will lead to this stream model
Therefore a
printlnwill not directly write the string to theFileOutputStream