为什么 BufferedWriter 没有将内容写入文件?

huangapple go评论61阅读模式
英文:

Why isn't BufferedWriter writing into file?

问题

package com.mycompany.mavenproject1;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.math.BigDecimal;

public class Main {
    public BufferedWriter writer;
    public Main() {
        Charset charset = Charset.forName("US-ASCII");
        try {
            this.writer = Files.newBufferedWriter(Paths.get("test.txt"), charset);
        } catch (IOException e) {
            System.err.format("IOException: %s%n", e);
        }
    }

    public void print_fees(String msg, BigDecimal b) {
        try {
            int msg_len = msg.length();
            int t;
            t = 34 - msg_len;
            t = t - 6;
            this.writer.write(msg + String.format("%" + t + "s", b));
            this.writer.newLine();
        } catch (IOException e) {
            System.err.format("IOException: %s%n", e);
        }
    }

    public static void main(String args[]) {
        BigDecimal b = new BigDecimal(2);
        Main obj = new Main();        
        try {
            obj.print_fees("Fee: ", b);
        } catch (Exception e) {
            System.err.format("Exception: %s%n", e);
        }
    }
}

为什么 BufferedWriter 没有将内容写入文件?

英文:
package com.mycompany.mavenproject1;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.math.BigDecimal;
public class Main {
public BufferedWriter writer;
public Main() {
Charset charset = Charset.forName("US-ASCII");
try {
this.writer = Files.newBufferedWriter(Paths.get("test.txt"), charset);
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
}
public void print_fees(String msg, BigDecimal b) {
try {
int msg_len = msg.length();
int t;
t = 34 - msg_len;
t = t - 6;
this.writer.write(msg + String.format("%" + t + "s", b));
this.writer.newLine();
} catch (IOException e) {
System.err.format("IOException: %s%n", e);
}
}
public static void main(String args[]) {
BigDecimal b = new BigDecimal(2);
Main obj = new Main();        
try {
obj.print_fees("Fee: ", b);
} catch (Exception e) {
System.err.format("Exception: %s%n", e);
}
}
}

Why isn't BufferedWriter writing into file?

答案1

得分: 2

BufferedWriter的作用是在写入底层实现之前将写入的内容缓冲到内存中。当内存中的缓冲区被填满时,它将进行批量写入。这通常可以提高写入性能,因为对每个小写入进行直接的文件/磁盘写入可能会因I/O阻塞而影响性能。

您需要显式地刷新流,以指示您希望将内存中的整个缓冲区写入底层实现(在这种情况下是文件写入器),方法是在写入到写入器之后调用BufferedWriter#flush()。例如,
this.writer.flush()

此外,当您使用完流(OutputStreamWriter)后,最好养成关闭它们的习惯,方法是使用BufferedWriter#close()。BufferedWriter将自动为您调用BufferedWriter#flush(),将所有写入写入底层实现,而无需显式调用BufferedWriter#flush()。例如,在完成对Writer的写入后,请关闭您的写入器:this.writer.close()

英文:

The point of BufferedWriter is to buffer written content into memory prior to writing it to whatever underlying implementation. When the buffer is filled in memory, it will write in bulk. This generally improves performance of writing, as doing a direct file/disk write for each small write can kill performance due to I/O blocking.

You need to flush the stream explicitly to indicate you want the entire buffer in memory to be written to the underlying implementation (in this case, a file writer) by invoking BufferedWriter#flush() after you have written to the writer. For example,
this.writer.flush().

Also, you should make a habit of closing open OutputStreams/Writers when you are done with them with BufferedWriter#close(). BufferedWriter will automatically invoke BufferedWriter#flush() for you, writing all your writes to whatever the underlying implementation is, without needing to explicitly invoke BufferedWriter#flush(). For example, close your writer once you have finished writing to your Writer: this.writer.close()

答案2

得分: 1

this.writer.write(msg + String.format("%" + t + "s", b));
this.writer.flush();

英文:
this.writer.write(msg + String.format("%" + t + "s", b));
this.writer.flush();

huangapple
  • 本文由 发表于 2020年10月2日 06:33:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64164020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定