System類對IO的支援
阿新 • • 發佈:2018-12-13
public static final PrintStream err
public static final PrintStream out
public static final InputStream in
我們平時經常用到的System.out.println()其實就是PrintStream類的println()方法。這個PrintStream類物件是由系統自動例項化好的,不用我們手動去例項化。
System.err.println()和System.out.println()差不多,就是顯示的顏色變為紅色而已。
System.in是鍵盤輸入流。
public class Main { public static void main(String[] args) throws IOException { InputStream in=System.in; System.out.print("請輸入內容:"); byte[] bytes=new byte[1024]; in.read(bytes); System.out.print("輸入的內容為:"+new String(bytes,0,bytes.length)); } }
執行結果:
請輸入內容:hello 世界
輸入的內容為:hello 世界
但是這樣接收輸入流有一個缺陷,我們開闢了一個1024的byte陣列,浪費了空間。