79829261

Date: 2025-11-25 02:34:02
Score: 1.5
Natty:
Report link

When you work with a buffer, always use flush(), as it forces the data from the buffer to be written to the final destination. Not using flush on a buffer can cause the data not to be written to your file.

public void writeToFile(String fullpath, String contents) {

    // Paths AṔI
    Path filePath = Paths.get(fullpath, "contents.txt");

    try {
        // Files AṔI    
        Files.createDirectories(filePath.getParent());
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    // Files AṔI    
     try (BufferedWriter bw = Files.newBufferedWriter(filePath)) {
        bw.write(contents);
        bw.flush(); // <<--- Flush force write in file
    } catch (IOException e) {
        e.printStackTrace();
    }
}

When working with files in Java, use the Paths and Files APIs.

This way, you don't need to worry about operating system issues.

Reference:

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Starts with a question (0.5): When you
  • Low reputation (0.5):
Posted by: Gil