好好看看 API;JAVA 的类库是JDK安装目录下src.zip的包。

解决方案 »

  1.   

    源码里的:public final class System {
    ......
    public final static InputStream in = nullInputStream();
    public final static PrintStream out = nullPrintStream();
    ......
        private static InputStream nullInputStream() throws NullPointerException {
    if (currentTimeMillis() > 0)
        return null;
    throw new NullPointerException();
        }    private static PrintStream nullPrintStream() throws NullPointerException {
    if (currentTimeMillis() > 0)
        return null;
    throw new NullPointerException();
        }
    ......}
      

  2.   

    src.zip的包中有好多,我找到了I/O包,那System库在哪里?我有点担心,如何从键盘中读入二进制数据等内容恐怕要涉及到一些操作系统的代码吧?我用的是windowsxp,java是不是只是简单的调用了那些操作系统的函数啊?那我觉得还能阅读。
      

  3.   

    System类在java.lang里,你看看吧,反正我看不明白。
      

  4.   

    安装了JDK后,你在JDK目录下可以发现一个 src.zip 或者 src.jar,不管是什么扩展名,都是 zip 格式的压缩包,解压开后在 java/lang 里可以找到 System.java,这就是源码。源码中有如下对 in, out 和 err 的申明:
        public final static InputStream in = nullInputStream();
        public final static PrintStream out = nullPrintStream();
        public final static PrintStream err = nullPrintStream();大约在 1014 行左右可以发现对 nullInputSteram 和 nullPrintStream 的定义如下:
        private static InputStream nullInputStream() throws NullPointerException {
    if (currentTimeMillis() > 0)
        return null;
    throw new NullPointerException();
        }    private static PrintStream nullPrintStream() throws NullPointerException {
    if (currentTimeMillis() > 0)
        return null;
    throw new NullPointerException();
        }也许这里看不出来为什么要这样做,所以我们看上面的注释:
        /**
         * The following two methods exist because in, out, and err must be
         * initialized to null.  The compiler, however, cannot be permitted to
         * inline access to them, since they are later set to more sensible values
         * by initializeSystemClass().
         */注释中说 in, out 和 err 都必须初始化为 null,之后由 initializeSystemClass() 设置值,所以再去阅读这个方法,大概在 1029 行左右。其中有这样几句: setIn0(new BufferedInputStream(fdIn));
    setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
    setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));setIn0,setOut0 和 setErr0 这三个方法都是 native 的,所以不知道是如何实现的,也不需要管了。不过可以看出来,in 是 BufferedInputStream,而 out 和 err 都是由 PrintSteam 包装的 BufferedOutputStream。