从另一个类读取 System.out.println(Text)

发布于 2024-10-03 07:29:12 字数 263 浏览 7 评论 0原文

基本上我正在创建一个类,该类应该从另一个类中读取一系列整数。

另一个类是 System.out.printing 按以下顺序排列整数:

i i i i
i i i i
... etc.

所以我基本上想制作一个字符串文件或任何有效的文件,我可以从第一个类中读取此输出。

我很迷茫,我不知道我是否应该创建一个扫描仪(System.in),如果是,该怎么办。

我想这是一个相当开放的问题,所以提前道歉。

Basically I am making a class that is supposed to read a series of integers from another class.

The other class is System.out.printing the integers in the following order:

i i i i
i i i i
... etc.

So I basically want to make a String file or whatever works, that I can read this output from in the first class.

I'm pretty lost, I don't know if I should create a Scanner(System.in), and if so, what to do with it.

I guess this is a pretty open question, so apologies in advance.

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

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

发布评论

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

评论(3

_畞蕅 2024-10-10 07:29:12

重定向 System.out 是一种解决方案,但这会重定向所有类的 System.out - 您无法(轻松)专门重定向这一类。

也许这个“其他类”在您的控制之下,您可以将此作为重组代码的机会。因此,也许您有类似的情况:

public void print(int[] values) {
  // ...
  System.out.println(result);
}

则可以将代码更改为并调用“prepareResult”方法。

public String prepareResult(int[] values) {
  // ...
  return result;
}

public void print(int[] values) {
  System.out.println(prepareResult(values));
}

如果您需要字符串中的输出,

Redirecting System.out is one solution, but that would redirect System.out for all classes - you can't (easily) redirect for this one class exclusivly.

Maybe this "other class" is under your control and you can take this as an oppertunity to restructure the code. So maybe you have something like:

public void print(int[] values) {
  // ...
  System.out.println(result);
}

then you could change the code to

public String prepareResult(int[] values) {
  // ...
  return result;
}

public void print(int[] values) {
  System.out.println(prepareResult(values));
}

and call the "prepareResult" method if you need the output in a String.

是伱的 2024-10-10 07:29:12

基本上我同意那些实际上说你的设计应该改变并且“其他类”应该只准备结果并返回它的人。

但如果这个“其他班级”不在你的控制之下,我建议你采取以下措施。

  1. 使用 System.setOut() 将 stdout 发送到特殊流。
  2. 实现这个特殊流如下:
    它应该获取堆栈跟踪并发现它是从哪里调用的。如果叫表格的话
    “其他类”做一些事情来通知它(将数据写入特殊队列等)
    否则只需打印到标准输出。

如果您不熟悉 API,这里有一些提示可以帮助您实现它。

  • new Throwable().getStackTrace()[0].getClassName() 返回调用此代码的类。
  • 实现您自己的输出流也很简单。

public class FilterOutputStream extends OutputStream {
    private OutputStream payload;
    public FilterOutputStream(OutputStream payload) {
        this.payload = payload;
    }

    public void write(int b) throws IOException {
        if (isSpecialClass()) {
            specialWrite(b);
        } else {
            payload.write(b);
        }
    }
    public void flush() throws IOException {
          payload.flush();
    }
    public void close() throws IOException {
         payload.close();
    }
    public boolean isSpecialClass() {
            return "MySpecialClass".equals(new Throwable().getStackTrace()[0].getClassName());
    }

}

我没有尝试这段代码,因此它可能甚至包含语法错误,但它说明了总体思路。我希望这有帮助。

Basically I agree with guys that actually said that your design should be changed and the "other class" should just prepare result and return it.

But if this "other class" is not under your control I'd suggest you the following.

  1. use System.setOut() to send stdout to special stream.
  2. implement this special stream as following:
    It should get stack trace and discover where it was invoked from. If it is called form
    the "other class" do something to notify it (write data to special queue or so on)
    Otherwise just print to stdout.

If you are not familiar with API, here are some tips that will help you to implement it.

  • new Throwable().getStackTrace()[0].getClassName() returns the class that called this code.
  • Implementing of your own outptut stream is simple too.

public class FilterOutputStream extends OutputStream {
    private OutputStream payload;
    public FilterOutputStream(OutputStream payload) {
        this.payload = payload;
    }

    public void write(int b) throws IOException {
        if (isSpecialClass()) {
            specialWrite(b);
        } else {
            payload.write(b);
        }
    }
    public void flush() throws IOException {
          payload.flush();
    }
    public void close() throws IOException {
         payload.close();
    }
    public boolean isSpecialClass() {
            return "MySpecialClass".equals(new Throwable().getStackTrace()[0].getClassName());
    }

}

I did not try this code, so it probably contains even syntax errors, but it illustrates the general idea. I hope this helps.

天涯沦落人 2024-10-10 07:29:12

您可以使用 System.setOut() 和 System.setErr() 将标准输出重定向到不同的流(然后将该流通过管道传输到您的输入)。

You could use System.setOut() and System.setErr() to redirect standard output on a different stream (and then pipe that stream to your input).

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