java 没有 System.out 怎么办,要在控制台上打印?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13172817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What to do without System.out, to print on console?
提问by KDjava
I have been stuck with a problem that was asked recently at an interview. The problem is stated as--> Suppose you don't have access to System class in Jdk API, Also you cannot use ECHO, you are in JRE 5 environment, how will you print anything on console?? The question really started with -- Why has Java given us the PrintStream object System.out ??And why is it final?? Isn't there any other way to print anything on console.??
我一直被一个最近在面试中问到的问题所困扰。问题表述为--> 假设您无权访问 Jdk API 中的 System 类,而且您不能使用 ECHO,您在 JRE 5 环境中,您将如何在控制台上打印任何内容?问题真正开始于——为什么 Java 给了我们 PrintStream 对象 System.out ??为什么它是最终的?没有其他方法可以在控制台上打印任何内容吗??
回答by eis
You could bypass the System object if you want to. System.out does a lot of extra stuff (handling unicode, for instance), so if you really want just the raw output and performance, you actually probably even should bypass it.
如果您愿意,您可以绕过 System object。System.out 做了很多额外的事情(例如处理 unicode),所以如果你真的只想要原始输出和性能,你实际上甚至应该绕过它。
import java.io.*;
public class PrintOutTest {
public static void main(String args[]) throws IOException {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(FileDescriptor.out), "ASCII"), 512);
out.write("test string");
out.write('\n');
out.flush();
}
}
This has been elaborated a bit further in here.
此处已对此进行了详细说明。
回答by Aniket Inge
PrintStream is final because it does everything the windows console /can/ do. Also its the same with "Console" being a const class in C#. The classes encapsulate everything the console can do, and it does in one specific way only. You can't "make it better" because at one point, it is upto the OS to handle it.
PrintStream 是最终的,因为它完成了 Windows 控制台 /can/ 所做的一切。同样,“控制台”是 C# 中的 const 类。这些类封装了控制台可以执行的所有操作,并且仅以一种特定方式执行。你不能“让它变得更好”,因为在某一时刻,它是由操作系统来处理的。
There are plenty of ways to output something on screen:
有很多方法可以在屏幕上输出一些东西:
- Write your own OutputStream the way @eis did it
- Use JNI and invoke a method in a native DLL/SO that invokes a native function like
printf()
- Use
Runtime.getRuntime().exec()
and callecho
program and the list follows.
- 按照@eis 的方式编写您自己的 OutputStream
- 使用 JNI 并调用本机 DLL/SO 中的方法,该方法调用本机函数,例如
printf()
- 使用
Runtime.getRuntime().exec()
调用echo
程序及列表如下。
+1 to @eis
+1 给@eis
回答by Stephen C
The interview question as stated is pretty perverse (i.e. artificial), and solving it involves stepping outside of pure Java. This is certainly not something that you would consider doing under normal circumstances.
如上所述的面试问题非常反常(即人为),解决它涉及跳出纯 Java。这当然不是您在正常情况下会考虑做的事情。
Other answers have given you some semi-solutions ... if you really, really have to do this. (I say semi-solutions because they mostly don't deal with the case where the application's stdout / stderr streams have been redirected to somewhere other than the console. And that is the only "real" aspect of this problem ...)
其他答案已经给了你一些半解决方案......如果你真的,真的必须这样做。(我说半解决方案是因为它们大多不处理应用程序的 stdout/stderr 流被重定向到控制台以外的地方的情况。这是这个问题唯一的“真实”方面......)
If you canuse the System class (on JDK 6) ...the clean way to print to the console (e.g. if System.out
has been redirected) is to use System.console()
method to get a Console
object, and use that to get a Writer
.
如果您可以使用 System 类(在 JDK 6 上)......打印到控制台的干净方法(例如,如果System.out
已被重定向)是使用System.console()
方法来获取Console
对象,并使用它来获取Writer
.
Note however that if the JVM has no associated console, console()
will return null
.
但是请注意,如果 JVM 没有关联的控制台,console()
将返回null
.
The question really started with -- Why has Java given us the PrintStream object System.out ?? And why is it final?? Isn't there any other way to print anything on console.??
问题真正开始于 - 为什么 Java 给了我们 PrintStream 对象 System.out ?为什么它是最终的?没有其他方法可以在控制台上打印任何内容吗??
The answers are:
答案是:
- For convenience.
- So that random code doesn't accidentally clobber it. (Or deliberately if you need to worry about untrusted code running in your JVM.) But trusted code actually canchange
System.out
by callingSystem.setOut(...)
. This does some behind the scenes magic to safely change the state of thefinal
variable. (I believe that the JIT compiler is aware of this, and treats those 3final
variables differently.) - Yes. See above, and (yuck!) the other Answers.
- 为了方便。
- 这样随机代码就不会意外地破坏它。(或故意如果你需要担心你的JVM。运行不受信任的代码),但受信任的代码实际上可以改变
System.out
调用System.setOut(...)
。这做了一些幕后魔术来安全地改变final
变量的状态。(我相信 JIT 编译器意识到这一点,并以final
不同的方式对待这 3 个变量。) - 是的。见上文,以及(糟糕!)其他答案。
回答by Unni Kris
You can use the JDK's logger(java.util.logging.Logger).
您可以使用JDK 的记录器(java.util.logging.Logger)。
This is how you create a logger in your java class.
这就是您在 Java 类中创建记录器的方式。
import java.util.logging.Logger;
private final static Logger LOGGER = Logger.getLogger(MyClass.class .getName());
Also You could use log4j for the same purpose.
您也可以将 log4j 用于相同的目的。
回答by bsiamionau
As documantation says,
正如文档所说,
The System class contains several useful class fields and methods. It cannot be instantiated.
System 类包含几个有用的类字段和方法。它不能被实例化。
So, as you can see, System class is kind of container for different useful functions. All this fields and methods are static, so it's not any reason to extend it.
Java gives us static PrintStream out
because it's default way to communicate with program - using console.
If you're not agree with me, please, tell me.
因此,如您所见,System 类是一种用于不同有用功能的容器。所有这些字段和方法都是静态的,因此没有任何理由扩展它。
Java 给了我们,static PrintStream out
因为它是与程序通信的默认方式——使用控制台。
如果你不同意我的观点,请告诉我。
回答by Vinnu
I found out a way today to achieve the question. I am using org.apache.log4j for this answer. There are many other ways as said by many here. Thanks.
我今天找到了一种方法来解决这个问题。我正在使用 org.apache.log4j 来回答这个问题。正如这里的许多人所说,还有许多其他方式。谢谢。
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class HelloByLogger {
private final static Logger logger = Logger.getLogger(HelloByLogger.class);
public static void main(String[] args) {
BasicConfigurator.configure();
logger.info("From Logger... Ola!");
}
}