BufferedWriter write() method not working

When I check the "Dir" directory, the file is created properly (with name writtenfile1), but nothing gets written inside and I do not understand why.

Is this a common pitfall with BufferedWriter? Because my code looks perfectly reasonable.

    int i = 1;
    Path path = Paths.get("Dir//writtenfile" + i + ".txt");
    Charset charset = Charset.defaultCharset();
    try {
        BufferedWriter writer = Files.newBufferedWriter(path, charset);
        writer.write("Message written!");
        //writer.write("This is file number " + i);
    } catch (Exception e) {
        System.out.println(e);
    }

Answers


As the name BufferedWriter implies, the data is buffered. It or the last part is only written on explicitly flushing or closing the writer instance.

This is not a bug; it is normal behaviour of this class.


Need Your Help

Cannot get Quicktime to Play in IE 9

html video internet-explorer-9 html5-video quicktime

I am trying to get a movie uploaded from a mobile device to play in a browser.

Can't get WinAppDeployCmd for Appx deployment to work

windows-phone win-universal-app windows-10

During the App Packaging and Deployment for Universal Windows Apps presentation (fast forward to 00:36:00) one specific command line utility - WinAppDeployCmd - was used for deployment Windows 10