Java IO: System.in, System.out, and System.error
来源:互联网 发布:mysql主键 编辑:程序博客网 时间:2024/05/21 08:56
转自:
http://tutorials.jenkov.com/java-io/system-in-out-error.html
The 3 streams System.in
, System.out
, and System.err
are also common sources or destinations of data. Most commonly used is probably System.out
for writing output to the console from console programs.
These 3 streams are initialized by the Java runtime when a JVM starts up, so you don't have to instantiate any streams yourself (although you can exchange them at runtime).
System.in
System.in
is an InputStream
which is typically connected to keyboard input of console programs. System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, or configuration files. In applications with GUI the input to the application is given via the GUI. This is a separate input mechanism from Java IO.
System.out
System.out
is a PrintStream
. System.out
normally outputs the data you write to it to the console. This is often used from console-only programs like command line tools. This is also often used to print debug statements of from a program (though it may arguably not be the best way to get debug info out of a program).
System.err
System.err
is a PrintStream
. System.err
works like System.out
except it is normally only used to output error texts. Some programs (like Eclipse) will show the output to System.err
in red text, to make it more obvious that it is error text.
Simple System.out + System.err Example:
Here is a simple example that uses System.out
and System.err
:
try { InputStream input = new FileInputStream("c:\\data\\..."); System.out.println("File opened...");} catch (IOException e){ System.err.println("File opening failed:"); e.printStackTrace();}
Exchanging System Streams
Even if the 3 System
streams are static members of the java.lang.System
class, and are pre-instantiated at JVM startup, you can change what streams to use for each of them. Just set a new InputStream
for System.in
or a new OutputStream
for System.out
or System.err
, and all further data will be read / written to the new stream.
To set a new System
stream, use one of th emethods System.setIn()
, System.setOut()
orSystem.setErr()
. Here is a simple example:
OutputStream output = new FileOutputStream("c:\\data\\system.out.txt");PrintStream printOut = new PrintStream(output);System.setOut(printOut);
Now all data written to System.out
should be redirected into the file "c:\\data\\system.out.txt". Keep in mind though, that you should make sure to flush System.out
and close the file before the JVM shuts down, to be sure that all data written to System.out
is actually flushed to the file.
- Java IO: System.in, System.out, and System.error
- Java IO: System.in, System.out, System.err
- java.io中的System.in、 System.out和System.err
- java IO笔记(System.int/out/error)
- Java IO操作——System类对IO的支持(System.out、System.err、System.in)
- Java基础----System.in、System.out
- Java中的System.in,System.out,System.err
- System.in与System.out
- Java IO流->处理流->标准输入输出流:System.in&System.out
- Java System 类详解 - in, out, err
- System.in.read(); System.out.write();
- System.in与System.out流
- 4.5.7.1System.in和System.out
- 关于System.in和System.out
- java System.out.println()
- Java: 省略System.out
- 简析System.out、System.in、System.err流
- 黑马程序员-IO(System.out、in与Scanner)
- English——限定性定语从句与非限定性定语从句(二)
- 用phpMyadmin给MySQL数据表创建触发器遇到的问题
- 3.5 复习课
- Eclipse使用BlueStacks作为android模拟器
- 如何恢复相机不小心删除的照片呢
- Java IO: System.in, System.out, and System.error
- 12 rip-2 配置命令
- mysql 5.6.10.1安装时root 密码问题
- Android检查网络是否连接
- Android --- windows下搭建Linux环境下载编译Android源代码--解决下载android系统源码错误问题
- ACM-简单题之Ignatius and the Princess II——hdu1027
- Activity 知识点分类
- 生成excel表报的控件FlexCel Studio for .NET控件介绍
- 13 ERGIP