使用BufferedWriter写入字符串[复制](Using BufferedWriter to write to a string [duplicate])

编程入门 行业动态 更新时间:2024-10-25 00:30:04
使用BufferedWriter写入字符串[复制](Using BufferedWriter to write to a string [duplicate])

这个问题在这里已有答案:

bufferedwriter在写 3个答案 的过程中停止了

我正在尝试使用BufferedWriter在写入File和写入String之间切换,但我从未使用过BufferedWriter来写入除文件之外的任何内容。

拿这个可编辑的代码:

public static void main(String[] args) { try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(new File("file.txt"))); StringWriter sw = new StringWriter(); BufferedWriter stringWriter = new BufferedWriter(sw)) { LinkedList<Record> records = new LinkedList<>(); records.add(new Record("name1", "text1", 20.4)); records.add(new Record("name2", "text2", -78)); records.add(new Record("name3", "text3", 11.56)); records.add(new Record("name4", "text4", 56)); records.add(new Record("name3", "text3", -44)); for(Record record : records) { BufferedWriter writer; if(record.amount < 0) { writer = stringWriter; // write to string if amount is less than zero } else { writer = fileWriter; // write to file if not } writer.append(record.name); writer.append(","); writer.append(record.text); writer.append(","); writer.append(String.valueOf(record.amount)); writer.newLine(); } String less_than_zero_amounts = sw.toString(); System.out.println("Less than zero:" + less_than_zero_amounts); } catch (IOException e) { e.printStackTrace(); } } static class Record { String name; String text; double amount; public Record(String name, String text, double amount) { this.name = name; this.text = text; this.amount = amount; } public String getText() { return text; } public String getName() { return name; } public double getAmount() { return amount; } }

该文件的输出是(正确)

name1,text1,20.4 name3,text3,11.56 name4,text4,56.0

但是程序的输出不会打印StringWriter 。

不可否认,使用StringWriter并将其提供给BufferedWriter是一种预感。 我可以将BufferedWriter切换为输出到String任何方式都可以解决问题。

This question already has an answer here:

bufferedwriter stops in the middle of writing 4 answers

I am trying to use a BufferedWriter to switch between writing to a File and writing to a String, but I have never used a BufferedWriter to write to anything but a file.

Take this compilable code:

public static void main(String[] args) { try (BufferedWriter fileWriter = new BufferedWriter(new FileWriter(new File("file.txt"))); StringWriter sw = new StringWriter(); BufferedWriter stringWriter = new BufferedWriter(sw)) { LinkedList<Record> records = new LinkedList<>(); records.add(new Record("name1", "text1", 20.4)); records.add(new Record("name2", "text2", -78)); records.add(new Record("name3", "text3", 11.56)); records.add(new Record("name4", "text4", 56)); records.add(new Record("name3", "text3", -44)); for(Record record : records) { BufferedWriter writer; if(record.amount < 0) { writer = stringWriter; // write to string if amount is less than zero } else { writer = fileWriter; // write to file if not } writer.append(record.name); writer.append(","); writer.append(record.text); writer.append(","); writer.append(String.valueOf(record.amount)); writer.newLine(); } String less_than_zero_amounts = sw.toString(); System.out.println("Less than zero:" + less_than_zero_amounts); } catch (IOException e) { e.printStackTrace(); } } static class Record { String name; String text; double amount; public Record(String name, String text, double amount) { this.name = name; this.text = text; this.amount = amount; } public String getText() { return text; } public String getName() { return name; } public double getAmount() { return amount; } }

The output for the file is (correctly)

name1,text1,20.4 name3,text3,11.56 name4,text4,56.0

But the output for the program does not print the StringWriter.

Admittedly, using a StringWriter and giving that to the BufferedWriter was a hunch. Any way I can switch a BufferedWriter to output to a String would solve the problem.

最满意答案

您需要调用“flush()”来将缓冲区的内容刷新到输出: http : //docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#flush()

虽然我应该添加而不是在BufferedWriter对象之间进行选择,但您可能只想在Writer对象之间进行选择...... StringWriter有自己的缓冲区,因此无需在其上添加额外的BufferedWriter层。

You need to call "flush()" to flush the contents of the buffer to the output: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#flush()

Though I should add that rather than select between BufferedWriter objects, you probably want to simply select between Writer objects... the StringWriter has a buffer of its own, so there is no need to add an extra layer of a BufferedWriter on top of it.

更多推荐

本文发布于:2023-07-04 10:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   BufferedWriter   write   duplicate   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!