문제

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);
    }
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top