1

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

}
Jeevan Varughese
  • 2,159
  • 2
  • 15
  • 20

3 Answers3

1

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

    }

    }
tk_
  • 16,415
  • 8
  • 80
  • 90
1
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();
            }
        }
sunysen
  • 2,265
  • 1
  • 12
  • 13
1

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)
René Link
  • 48,224
  • 13
  • 108
  • 140
  • PrinterWriter will do the job. For every run, we are closing the stream, atleast trying to (Something I have missed). Why would we want to run it for every fresh run ? – Jeevan Varughese Nov 19 '14 at 05:54
  • @JeevanVarughese In your case you could also just close the `PrintWriter` which will lead to the same result. But you wondered `No string printed into the file` and the reason is because `PrintWriter` has not written the bytes to the `FileOutputStream` yet - it has not been flushed yet. Of course closing a `PrintWriter` flushes it too. – René Link Nov 19 '14 at 05:59
  • Thanks !! That helps. Lemme give a quick try... somehow missed that concept ! Thanks ! – Jeevan Varughese Nov 19 '14 at 06:30
  • @JeevanVarughese I updated the answer in order to make it more clear – René Link Nov 19 '14 at 06:40