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
PrintWriter
is not flushed. Useor use
autoFlush
or just
close
it.autoFlush
works forprintln
,printf
andformat
. See the javadoc of PrintWriter.Details
The
PrintWriter
can be constructed either with anotherWriter
, aFile
or filename or anOutputStream
. In case of instantiating it using anOutputStream
the javadoc says:and the javadoc of OutputStreamWriter says:
EDIT
So your code
will lead to this stream model
Therefore a
println
will not directly write the string to theFileOutputStream