如何获取我通过 System.out.println 发送的值?
How to get the value which I sent via System.out.println?
抱歉,我搜索了很多,但找不到答案。如果有,我很抱歉,请告诉我。
如果我通过 System.out.println
或 .print
发送了一个值,有什么办法可以得到它吗?我的意思是在发送后获取通过 System.out 发送的所有值和最后发送的值 ?
System.out.println("Hi");
String val = System.\something
String last = System.\something else
谢谢
好像对System.out.println的作用有点误解。 System.out.println 将字符串发送到 Java 程序的输出流 (stdout)。所以返回给操作系统。这通常用于使输出对用户可见,但也对其他应用程序可见。另一个应用程序可以用 System.in.read.
读取它
在您的情况下,您希望在同一个应用程序中使用输出,这是不必要的,因为该应用程序知道它自己的数据。
如果您需要存储输出数据的历史记录,您当然可以将历史记录保存在您自己的应用程序中——(如评论中所建议的那样)经过修饰的 PrintStream 可以完成这项工作。
如果您是 Java 的初学者,编写一个新方法来存储您的历史记录可能会更容易。例如。您可以将以下内容添加到您的 class:
private static LinkedList<String> stdoutHistory;
public static void betterPrintln(String s)
{
System.out.println(s);
stdoutHistory.add(s);
}
// this method returns the last printed output
public static String getLastOutput()
{
return stdoutHistory.get(stdoutHistory.count()-1);
}
然后调用那个方法,打印一些东西
我想下面的代码会对你有用:
创建 class 并扩展 PrintStream
class StorePrintStream extends PrintStream {
public static List<String> printList = new LinkedList<String>();
public StorePrintStream(PrintStream org) {
super(org);
}
@Override
public void println(String line) {
printList.add(line);
super.println(line);
}
public void println(int line) {
this.println(String.valueOf(line));
}
// And so on for double etc..
}
现在使用上面的class来跟踪打印信息:
public class Test {
public static void main(String[] args) throws Exception {
System.setOut(new StorePrintStream(System.out));
System.out.println("print line");
Test2 t2 = new Test2();
t2.meth1();
System.out.println(StorePrintStream.printList);
}
}
class Test2 {
public void meth1() {
System.out.println("another print");
}
}
抱歉,我搜索了很多,但找不到答案。如果有,我很抱歉,请告诉我。
如果我通过 System.out.println
或 .print
发送了一个值,有什么办法可以得到它吗?我的意思是在发送后获取通过 System.out 发送的所有值和最后发送的值 ?
System.out.println("Hi");
String val = System.\something
String last = System.\something else
谢谢
好像对System.out.println的作用有点误解。 System.out.println 将字符串发送到 Java 程序的输出流 (stdout)。所以返回给操作系统。这通常用于使输出对用户可见,但也对其他应用程序可见。另一个应用程序可以用 System.in.read.
读取它在您的情况下,您希望在同一个应用程序中使用输出,这是不必要的,因为该应用程序知道它自己的数据。
如果您需要存储输出数据的历史记录,您当然可以将历史记录保存在您自己的应用程序中——(如评论中所建议的那样)经过修饰的 PrintStream 可以完成这项工作。
如果您是 Java 的初学者,编写一个新方法来存储您的历史记录可能会更容易。例如。您可以将以下内容添加到您的 class:
private static LinkedList<String> stdoutHistory;
public static void betterPrintln(String s)
{
System.out.println(s);
stdoutHistory.add(s);
}
// this method returns the last printed output
public static String getLastOutput()
{
return stdoutHistory.get(stdoutHistory.count()-1);
}
然后调用那个方法,打印一些东西
我想下面的代码会对你有用:
创建 class 并扩展 PrintStream
class StorePrintStream extends PrintStream {
public static List<String> printList = new LinkedList<String>();
public StorePrintStream(PrintStream org) {
super(org);
}
@Override
public void println(String line) {
printList.add(line);
super.println(line);
}
public void println(int line) {
this.println(String.valueOf(line));
}
// And so on for double etc..
}
现在使用上面的class来跟踪打印信息:
public class Test {
public static void main(String[] args) throws Exception {
System.setOut(new StorePrintStream(System.out));
System.out.println("print line");
Test2 t2 = new Test2();
t2.meth1();
System.out.println(StorePrintStream.printList);
}
}
class Test2 {
public void meth1() {
System.out.println("another print");
}
}