何时在Java中使用哪个Writer子类;常见做法 [英] When to use which Writer subclass in Java; common practices

查看:207
本文介绍了何时在Java中使用哪个Writer子类;常见做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直对Java中不同IO实现的数量略有混淆,现在我完全陷入了项目开发阶段,我花时间阅读有用的东西。



我已经意识到没有新手友好的比较(除了 Writer Writer类的API ) c $ c>上课。所以我想我会解决这个问题,那些不同的子类有什么用呢?



例如,我通常使用包含 BufferedWriter FileWriter $ c>对于我的文件输出,但我总是因为没有 println()之类的方法而烦恼,并且必须使用 newLine()每隔一行(使输出人类可读)。 PrintWriter println()方法,但没有支持附加的构造函数...



如果你能从你的经验中给我两分钱,或者你可能偶然发现一个好的指南/方法,我真的很感激。



编辑:感谢大家的回复,我非常感谢这里传递的信息。有点不幸的是,整个 append()的事情最终成为焦点,它仅仅意味着它作为一个例子。我的问题主要是指所有不同实现的需要和使用,我想在一些答案中有所提及。



很难选择一个被接受的答案,因为有三个非常可靠的答案,每个答案都有助于我对问题的理解。我将不得不和Anon一起去,因为他的代表数量最少。积分(我认为他是SO的新人)。他有15个答案,其中一些确实很好,并提出了0个问题。我会说,这是值得推广的好贡献。



话虽如此,ColinD和Jay也提供了非常好的答案,并指出了有趣的想法。特别是Jay关于Java自动包装 BufferedWriter 的评论值得注意。再次感谢大家,非常感谢!

解决方案

java.io 类一般遵循装饰者模式。因此,虽然 PrintWriter 没有您可能想要的特定构造函数,但它确实有一个构造函数需要另一个 Writer ,所以你可以做类似以下的事情:

  FileOutputStream fos = null; 
try
{
fos = new FileOutputStream(foo.txt);
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(fos,UTF-8)));
//做你想做的事
out.flush();
out.close();
}
最后
{
//悄然关闭FileOutputStream(参见Jakarta Commons IOUtils)
}

作为一般使用说明,您总是希望包装一个低级Writer(例如 FileWriter OutputStreamWriter )在 BufferedWriter 中,以最小化实际的IO操作。但是,这意味着您需要显式刷新并关闭最外层的Writer,以确保写入所有内容。



然后您需要关闭低级Writer在 finally 块中,以确保您不会泄漏资源。



编辑



查看 MForster 的回答让我再看看FileWriter的API。我意识到它不需要一个明确的字符集,这是一个非常糟糕的事情。所以我编辑了我的代码片段,使用 FileOutputStream 包装的 OutputStreamWriter ,它采用了明确的字符集。 / p>

I have always been slightly confused with the amount of different IO implementations in Java, and now that I am completely stuck in my project development, I was taking my time to read up on useful stuff meanwhile.

I have realized that there is no newbie-friendly comparison (apart from short explanation at API for Writer class) between the different subclasses of the Writer class. So I figured I'd fire away the question, what are those different subclasses good for?

For example, I usually use a FileWriter wrapped with a BufferedWriter for my outputs to files but I have always been irritated by the fact that there is no println() like method, and one has to use newLine() every second line (to make the output human readable). PrintWriterhas the println() method but no constructor that supports appending however...

I'd really appreciate if you could give me your two cents from your experience, or a nice guide/how-to you might have stumbled upon.

EDIT: Thanks for the replies everyone, I really appreciate the info passed on here. It's a bit unfortunate that the whole append() thing ended up being in focus, it merely meant it as an example. My question was mostly referring to the need and use of all the different implementations, which I guess was mentioned somewhat in a couple of the answers.

It's hard to pick one answer as accepted, since there are three really solid answers, each has contributed to my understanding of the problem. I am gonna have to go with Anon, this time as he's got the least amount of rep. points (I presume he's new on SO). He's 15 answers some of which are really well formulated, and 0 questions asked. Good contribution I'd say, and that is worth promoting.

That being said, ColinD and Jay also provided really good answers, and have pointed out interesting ideas. Especially Jay's comment about Java automatically wrapping a BufferedWriter was worth noting. Thanks again guys, really appreciated!

解决方案

The java.io classes generally follow the Decorator pattern. So, while PrintWriter does not have the specific constructor you might want, it does have a constructor that takes another Writer, so you can do something like the following:

FileOutputStream fos = null;
try
{
    fos = new FileOutputStream("foo.txt");
    PrintWriter out = new PrintWriter(
                          new BufferedWriter(
                              new OutputStreamWriter(fos, "UTF-8")));
    // do what you want to do
    out.flush();
    out.close();
}
finally
{
    // quietly close the FileOutputStream (see Jakarta Commons IOUtils)
}

As a general usage note, you always want to wrap a low-level Writer (eg FileWriter or OutputStreamWriter) in a BufferedWriter, to minimize actual IO operations. However, this means that you need to explicitly flush and close the outermost Writer, to ensure that all content is written.

And then you need to close the low-level Writer in a finally block, to ensure that you don't leak resources.

Edit:

Looking at MForster's answer made me take another look at the API for FileWriter. And I realized that it doesn't take an explicit character set, which is a Very Bad Thing. So I've edited my code snippet to use a FileOutputStream wrapped by an OutputStreamWriter that takes an explicit character set.

这篇关于何时在Java中使用哪个Writer子类;常见做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆