17編寫字符界面應用(上)_第1頁
17編寫字符界面應用(上)_第2頁
17編寫字符界面應用(上)_第3頁
17編寫字符界面應用(上)_第4頁
17編寫字符界面應用(上)_第5頁
已閱讀5頁,還剩7頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、編寫字符界面應用(上) 命令行參數 常用系統屬性 Properties類 System類中和屬性有關的操作 從屬性文件中讀取屬性 標準輸入輸出命令行參數 在windows下,通過java.exe可執行程序來運行Java程序,格式如下 java ClassName para_list 在啟動Java應用程序時可以一次性地向應用程序中傳遞0多個參數-命令行參數; 命令行參數通過public static void main(String args)中的main方法接收命令行參數例子(示例9-1)public class ConsoleParamspublic static void main(St

2、ring args)if(args.length!=2)System.out.println(請按下列方式執行:java ConsoleParams 參數1 參數2);System.exit(0);String param1 = args0;String param2 = args1;System.out.print(你好,+param1+,你今年);System.out.println(2004-Integer.parseInt(param2)+歲); 系統屬性 Java中系統屬性就是Java的環境變量 System.getProperties()方法會返回系統屬性值。 System.get

3、Property()方法返回一個String來代表系統屬性。 在命令行中可用java D來加入一個系統屬性Properties類 Properties類實現了從名字到值的映射 propertyNames()方法返回一個包含所有屬性名的Enumeration對象 getProperty()方法返回一個代表該屬性值的字符串 使用load()或store()方法能從文件讀入屬性集或將屬性集寫入文件 Properties在java.util包中系統屬性例子(示例9-2)import java.util.Properties;import java.util.Enumeration;public cla

4、ss TestProperties public static void main(String args) Properties props = System.getProperties(); Enumeration prop_names = pertyNames(); while ( prop_names.hasMoreElements() ) String prop_name = (String) prop_names.nextElement(); String property = props.getProperty(prop_name); System.out.pr

5、intln(property + prop_name + is + property + ); 從文件中讀取屬性的例子(示例9-3)oracle_url=jdbc:oracle:thin:localhost:1521:O920oracle_name = O920oracle_user = scottoracle_pwd= tigerfile_path=c:cctvfilesvirtual_path=examples/從文件重讀取屬性的例子(con.)import java.util.*;import java.io.*;public class ReadProprivate String or

6、acle_url,oracle_name,oracle_user,oracle_pwd;private String file_path,virtual_path;public ReadPro()tryProperties props = new Properties();File f=new File(C:OracleSperties);FileInputStream in = new FileInputStream(f);props.load(in);in.close();oracle_url = props.getProperty(oracle_url);. .catch

7、(IOException e)System.out.println(e);. .控制臺輸入/輸出 System.out可向標準輸出設備輸出 它是一個PrintStream對象 System.in可從標準的輸入設備輸入 它是一個InputStream對象 System.err可向標準的錯誤設備輸出 它是一個PrintStream對象從鍵盤輸入例子(示例9-4)import java.io.*;public class KeyboardInput public static void main (String args) String s; /創建一個BufferedReader對象從鍵盤逐行讀入

8、數據 InputStreamReader ir = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(ir); System.out.println(Unix: Type ctrl-d or ctrl-c to exit. + nWindows: Type ctrl-c to exit.); try / 每讀入一行,向標準輸出設備輸出 while (s = in.readLine() != null) System.out.println(Read: + s); / 關閉流,這步動作在對流的操作完成

9、后一定要做。 in.close(); catch (IOException e) / Catch any IO exceptions. e.printStackTrace(); 向標準設備輸出 使用System.out.println/System.out.print兩個常用的方法向標準設備輸出 println()方法將參數打印出來,并加上”n”字符。 print()方法,打印參數,但不加新行 print和println方法對多數簡單數據類型進行了重載(boolean, char, int, long, float, double)和char, Object以及String print(Object)或println(Object)將會調用該對象的toString()方法,打印它的返回字符串向標準設備輸出例子(示例9-5)public class Echopublic static void main(String args)int a = 100;boolean b = true;System.out.print(echo an int primitive type data:);System.out.println(a);Sys

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論