将 PrintWriter 与另一个 PrintWriter 进行比较

发布于 2025-01-17 09:35:02 字数 508 浏览 4 评论 0 原文

我有两个 PrintWriter:

PrintWriter pw1 = new PrintWriter("testPrint.txt");
PrintWriter pw2 = new PrintWriter("testPrint.txt");

我想检查它们是否都打印到同一个文件,但与

pw1.equals(pw2)

Objects.equals(pw1, pw2)

进行比较不起作用。另外,我希望能够检查像这两个这样的 PrintWriters 是否打印到同一个地方,而不是带有文件路径的 String:

PrintWriter pw1 = new PrintWriter(System.out, true);
PrintWriter pw2 = new PrintWriter(System.out, true);

有没有办法做到这一点?

I have two PrintWriters:

PrintWriter pw1 = new PrintWriter("testPrint.txt");
PrintWriter pw2 = new PrintWriter("testPrint.txt");

I want to to check if both of them print to the same file, but comparing with

pw1.equals(pw2)

or

Objects.equals(pw1, pw2)

doesn't work. Also, instead of String with a path to file I want to be able to check if PrintWriters like those two print to the same place in general:

PrintWriter pw1 = new PrintWriter(System.out, true);
PrintWriter pw2 = new PrintWriter(System.out, true);

Is there a way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千鲤 2025-01-24 09:35:02

不,不是您写的两个印刷作家。通常,有两个作家写信到同一目的地是您应该避免的问题。相反,如果您想将多个源编写到同一文件,请使用一个打印稿和一条编写的消息队列。

您可能还需要查看可以锁定的Filechannel。请参阅:

No, not from two Print writers as you have written it. In general, having two writers writing to the same destination is a problem you should avoid. Instead, if you want multiple sources writing to the same file, use one PrintWriter and a queue of messages to be written.

You may also want to check out FileChannel, which can be locked. See: can we open multiple FileWriter stream to the same file at the same time?

醉生梦死 2025-01-24 09:35:02

无法从外部检查 PrintWriter 的底层流。有一个 out 成员变量 但它是一个受保护的属性,只能由子类访问。

受保护的写入器输出

PrintWriter 的底层字符输出流。

即使您有权访问该字段,它仍然不像比较两个实例那么容易。两个底层写入器可能是写入同一位置的不同对象。这正是当您使用 PrintWriter(String) 构造函数

package java.io;

public class PrintWriter extends Writer {
    // ...

    public PrintWriter(String fileName) throws FileNotFoundException {
        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
             false);
    }

    // ...
}

它将创建两个 BufferedWriter,它们指向不同的 OutputStreamWriter >s,它将指向不同的FileOutputStream。一路下来都是乌龟。

There's no way from the outside to check a PrintWriter's underlying stream. There is an out member variable but it's a protected property only accessible by subclasses.

protected Writer out

The underlying character-output stream of this PrintWriter.

Even if you had access to this field, though, it still wouldn't be as easy as comparing two instances. The two underlying writers could be different objects that write to the same place. That is exactly what will happen when you use the PrintWriter(String) constructor from your first example:

package java.io;

public class PrintWriter extends Writer {
    // ...

    public PrintWriter(String fileName) throws FileNotFoundException {
        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
             false);
    }

    // ...
}

It would create two BufferedWriters, which would point to distinct OutputStreamWriters, which would point to distinct FileOutputStreams. It's turtles all the way down.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文