亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

通過Java IO流的形式實現鍵盤錄入的疑問

瀏覽:63日期:2023-12-31 15:18:24

問題描述

通過Java IO流的形式實現鍵盤錄入的疑問

問題解答

回答1:

因為System.in就是一個靜態的InputStream實例,你可以在java api文檔上看看

回答2:

首先,System.in是一個InputStream類型的對象,在源碼里是這樣的:

/** * The 'standard' input stream. This stream is already * open and ready to supply input data. Typically this stream * corresponds to keyboard input or another input source specified by * the host environment or user. */public final static InputStream in = null;

可見System.in屬于標準輸入,可以通過鍵盤或其他方式輸入數據。但在源碼里,該對象并沒有(顯式)初始化的方法,通過閱讀源碼,可發現下面這個方法:

/** * Reassigns the 'standard' input stream. * * <p>First, if there is a security manager, its <code>checkPermission</code> * method is called with a <code>RuntimePermission('setIO')</code> permission * to see if it’s ok to reassign the 'standard' input stream. * <p> * * @param in the new standard input stream. * * @throws SecurityException *if a security manager exists and its *<code>checkPermission</code> method doesn’t allow *reassigning of the standard input stream. * * @see SecurityManager#checkPermission * @see java.lang.RuntimePermission * * @since JDK1.1 */public static void setIn(InputStream in) { checkIO(); setIn0(in);}private static native void setIn0(InputStream in);

閱讀注釋可見該方法用于設定研究setIn0(in),可見該方法是通過調用底層接口來實現in的設定,那么在軟件運行時,是如何初始化的呢?System類中有如下代碼:

/* register the natives via the static initializer. * * VM will invoke the initializeSystemClass method to complete * the initialization for this class separated from clinit. * Note that to use properties set by the VM, see the constraints * described in the initializeSystemClass method. */private static native void registerNatives();static { registerNatives();}/** * Initialize the system class. Called after thread initialization. */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));...}

由此段代碼可知,軟件運行時,先運行靜態代碼塊,調用registerNatives()這個底層方法對System類進行初始化,該方法則是調用initializeSystemClass()方法來初始化System類的,這兩步都是通過VM實現的,然后閱讀initializeSystemClass()這個方法,可見到調用setIn0,setOut0,steErr0三段代碼,分別初始化了標準輸入,標準輸出,標準錯誤三種輸出流,至此System.in、out、err的初始化流程就都弄清楚了。標記為native的方法是JVM調用其他代碼實現的功能,這個和底層有關系,我覺得這個感覺就像是Bootstrap Classloader實際上是C實現,但由JVM調用來加載各個基礎JAR classes相似吧。另外你說InputStream是抽象類(接口),實際上接口類抽象類都可以作為參數,但實現肯定不是他們實現的,他們不能被實現,但可以作為參數,畢竟父類出現的地方子類都可以替代,所以這里不存在問題

標簽: java
相關文章:
主站蜘蛛池模板: 国产制服 国产制服一区二区 | 国产亚洲精品一区久久 | 国产美女一区精品福利视频 | 亚洲午夜在线 | 国产在线视欧美亚综合 | 久久婷婷成人综合色 | 国产精品亚洲精品日韩己满十八小 | 久久乐国产精品亚洲综合m3u8 | 欧美日韩高清在线 | 国产精品欧美亚洲韩国日本 | 成人小视频免费 | 国产区亚洲区 | 一区二区精品在线观看 | 久久精品2019www中文 | 国产露脸真实作爱视频 | 日韩h片在线观看 | 欧美毛片一级的免费的 | 99久久精品国产综合一区 | 午夜视频你懂的 | 日韩免费视频网站 | 亚洲人人草 | 国产自产视频在线观看香蕉 | 亚洲精品欧美在线 | 99精品视频一区在线视频免费观看 | 免费观看成人www精品视频在线 | 国产私密视频 | 高清中文字幕 | 成人黄页网站免费观看大全 | 2022国内精品免费福利视频 | 91tv最新永久在线地址 | 麻豆网站视频国产在线观看 | 国产激情自拍视频 | 成人午夜在线观看国产 | 欧美a级片在线观看 | 欧美一级欧美一级毛片 | 三级福利视频 | 国产精品欧美一区喷水 | 大尺度福利视频在线观看网址 | 欧美片能看的一级毛片 | 国内精品一区二区三区最新 | 亚洲天堂视频网站 |