No string printed into the file

91 Views Asked by At

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();
        }
    }

}
3

There are 3 best solutions below

4
On BEST ANSWER

The PrintWriter is not flushed. Use

p.println("some string");
p.flush();

or use autoFlush

PrintWriter p = new PrintWriter(fo, true);
p.println("some string");

or just close it.

autoFlush works for println, printf and format. See the javadoc of PrintWriter.

Details

The PrintWriter can be constructed either with another Writer, a File or filename or an OutputStream. In case of instantiating it using an OutputStream the javadoc says:

public PrintWriter(OutputStream out)

Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

and the javadoc of OutputStreamWriter says:

... The resulting bytes are accumulated in a buffer before being written to the underlying output stream. ...

EDIT

So your code

fo = new FileOutputStream(file);
PrintWriter p = new PrintWriter(fo);

will lead to this stream model

+-------------+           +--------------------+          +------------------+
| PrintWriter | --------> | OutputStreamWriter | -------> | FileOutputStream |
+-------------+           +--------------------+          +------------------+

Therefore a println will not directly write the string to the FileOutputStream

p.println("some string")
  +-> outputStreamWriter.write("some string")
      +--> write to internal buffer (as bytes)

p.flush();
  +-> outputStreamWriter.flush();
      +--> internal buffer flush
         +--> fileOutputStream.write(bytes)
1
On

Better if you include p.close() in the finally

 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();
        } finally {
    p.close();
if(fo != null ){
            try {
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    }
3
On
File file = new File(filePath, "jfv.properties");
        FileOutputStream fo = null;
        BufferedWriter bw = null;
        FileWriter fw = null;
        try {
            fo = new FileOutputStream(file);
            PrintWriter p = new PrintWriter(fo);
            p.println("some string");
            //change it 
            fw = new FileWriter(file, true);
            bw = new BufferedWriter(fw);
            bw.write("some string");
            bw.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException s) {
            s.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fo != null) {
                    fo.close();
                }
                if (fw != null) {
                    fw.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }