几乎所有的都用过这个System类吧,因为大家学习的第一个语句大概就是
package jdk.lang;
public class SystemInspection {
public static void main(String[] args) throws Exception {
System.out.println(“Hello World”);
}
}
本文将一一详解这个类的标准输入(in),输出(out)和错误流(err)使用和原理。先看一个更复杂的例子:
package jdk.lang;
public class SystemIOE {
public static void main(String[] args) throws Exception {
System.out.println("Hello Out");
System.err.println("Hello Error");
byte[] b = new byte[1024];
int count = System.in.read(b);
System.out.println(new String(b, 0, count));
}
}
在eclipse上运行,输出:
Hello Out
Hello Error
Hello In
Hello In
分析:
其实out, in 和 err是它的静态属性:
public final static InputStream in = nullInputStream();
public final static PrintStream out = nullPrintStream();
public final static PrintStream err = nullPrintStream();
他们其实是在private static void initializeSystemClass()中初始化的:
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setIn0(new BufferedInputStream(fdIn));
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));
FileDescriptor.out,FileDescriptor.in和FileDescriptor.err其实是
public static final FileDescriptor in = standardStream(0);
public static final FileDescriptor out = standardStream(1);
public static final FileDescriptor err = standardStream(2);
再往下就是native的调用了。
同时System类还提供方法让你设置in,out和err,这样你就可以实现自己的日志系统了。运行下面的代码:
package jdk.lang;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
public class SystemIOE {
public static void main(String[] args) throws Exception {
redirect();
helloIO();
}
private static void helloIO() throws IOException {
System.out.println("Hello Out");
System.err.println("Hello Error");
byte[] b = new byte[1024];
int count = System.in.read(b);
System.out.println(new String(b, 0, count));
}
public static void redirect() throws FileNotFoundException {
InputStream in = new BufferedInputStream(new FileInputStream(new File(
"c:/in.txt")));
System.setIn(in);
PrintStream out = new PrintStream(new FileOutputStream(new File(
"c:/out.log")));
System.setOut(out);
PrintStream err = new PrintStream(new FileOutputStream(new File(
"c:/err.log")));
System.setErr(err);
}
}
后面我再看看logback的实现是不是用到了这个类。待续。。。
分享到:
相关推荐
下面小编就为大家带来一篇java基础(System.err和System.out)详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
错误 101 (net--ERR_CONNECTION_RESET).docx
System.err.println("Error loading classifier file \"" + classifierName + "\"."); System.exit(1); } // The available FrameGrabber classes include OpenCVFrameGrabber (opencv_videoio), // DC1394...
耐力训练诱导AMPK对小鼠骨骼肌PGC-1/ERRα结合的影响,吕媛媛,石越丹,目的:作为能量代谢的关键调节因子,转录辅激活因子PGC-1能与ERRα结合,共同调节靶基因的转录。本研究旨在探讨耐力训练中骨骼肌细�
Generate the code for adding user defined Gaussian Noise (in dB) and a. Filter out the same using average filter (generate function) b. Determine the frequency plot c. Determine error image, SNR and ...
*** These critical programs are missing or too old: as ld
Java 常见错误整理
System.err.println(PFILE +" file does not exist!"); } m_props = new Properties(); try { m_props.load(new FileInputStream(PFILE)); } catch(Exception e) { e.printStackTrace(); } }...
pr-food-err
国际电信联盟的无源音质客观评价标准P.563算法源代码,以及技术说明文档,代码直接能编译使用,测试确实对音质有比较明显的评分差异,直接输出MOS分
import java.awt.Component; import javax.swing.JOptionPane; /** * This class <code>ExceptionManager</code> and its subclasses are a form of ... System.err.println(errorMessage); } }
异步超时async-timeout是一个简单的异步任务包装... async.parallel([ timeout(function noop(){ // never call back }, 1000, 'FALLBACK RESP IN CASE OF TIMEOUT') ], function(err, res) { console.log(res[0]); });
import java.sql.*; import jdbc.DBManager; import jdbc.DBManagerTest; /** * <p>Title: * <p>Description: * <p>Copyright: Copyright (c) 2002 * <p>Company: * @author * @version 1.0 */ /** *...
libcom_err-devel-1.42.9-19.el7.i686.rpm
本文将为大家介绍交换机出现err-disable的原因及解决方法。
unix网络编程卷1 卷2 TCP-IP详解卷1 TCP-IP详解卷2 TCP-IP详解卷3 UNIX环境高级编程 源码 源代码 apue.h apue_err.c 三合一打包上传,解决了不能编译的问题,只要加上后面两个文件进行编译就可以了
安装VS2003时找不到H2INC.ERR文件问题的解决方案
ML.exe ML.err H2inc.exe H2inc.err Win.inc Readme.txt Errmsg.txt Support.txt
Out: outCount - Number of elements in this list. Returns: Returns EDS_ERR_OK if successful. In other cases, see EDSDKError.pas. --------------------------------------------------------------------...
针对性能参数的配置要特别注意,因为很有可能导致我们部署的网站不稳定,而且前后端没有错误日志,只能通过工具抓包去排查发现问题,这个文件告诉你如何去调整相关的参数来避免ERR_CONNECTION_TIMED_OUT 200的错误