17編寫字符界面應(yīng)用(上).ppt_第1頁
17編寫字符界面應(yīng)用(上).ppt_第2頁
17編寫字符界面應(yīng)用(上).ppt_第3頁
17編寫字符界面應(yīng)用(上).ppt_第4頁
17編寫字符界面應(yīng)用(上).ppt_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、編寫字符界面應(yīng)用(上),目標(biāo),掌握命令行參數(shù)的使用 了解常用系統(tǒng)屬性 掌握Properties類的用法 掌握標(biāo)準(zhǔn)輸入輸出,命令行參數(shù),在windows下,通過java.exe可執(zhí)行程序來運(yùn)行Java程序,格式如下 java ClassName para_list 在啟動Java應(yīng)用程序時(shí)可以一次性地向應(yīng)用程序中傳遞0多個(gè)參數(shù)-命令行參數(shù); 命令行參數(shù)通過public static void main(String args)中的main方法接收,命令行參數(shù)例子,public class ConsoleParams public static void main(String args) if(

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

3、ty()方法返回一個(gè)String來代表系統(tǒng)屬性。 在命令行中可用java D來加入一個(gè)系統(tǒng)屬性,Properties類,Properties類實(shí)現(xiàn)了從名字到值的映射 propertyNames()方法返回一個(gè)包含所有屬性名的Enumeration對象 getProperty()方法返回一個(gè)代表該屬性值的字符串 使用load()或store()方法能從文件讀入屬性集或?qū)傩约瘜懭胛募?Properties在java.util包中,系統(tǒng)屬性例子,import java.util.Properties; import java.util.Enumeration; public class TestP

4、roperties 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.println(pr

5、operty + prop_name + is + property + ); ,從文件中讀取屬性的例子,oracle_url=jdbc:oracle:thin:localhost:1521:O920 oracle_name = O920 oracle_user = scott oracle_pwd= tiger file_path=c:cctvfiles virtual_path=examples/,import java.util.*; import java.io.*; public class ReadPro private String oracle_url,oracle_name,

6、oracle_user,oracle_pwd; private String file_path,virtual_path; public ReadPro() try Properties 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(IOExcepti

7、on e) System.out.println(e); . . ,控制臺輸入/輸出,System.out可向標(biāo)準(zhǔn)輸出設(shè)備輸出 它是一個(gè)PrintStream對象 System.in可從標(biāo)準(zhǔn)的輸入設(shè)備輸入 它是一個(gè)InputStream對象 System.err可向標(biāo)準(zhǔn)的錯(cuò)誤設(shè)備輸出 它是一個(gè)PrintStream對象,鍵盤輸入示例,import java.io.*; public class KeyboardInput public static void main (String args) String s; /創(chuàng)建一個(gè)BufferedReader對象從鍵盤逐行讀入數(shù)據(jù) InputStr

8、eamReader 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 / 每讀入一行,向標(biāo)準(zhǔn)輸出設(shè)備輸出 while (s = in.readLine() != null) System.out.println(Read: + s); / 關(guān)閉流,這步動作在對流的操作完成后一定要做。 in.c

9、lose(); catch (IOException e) / Catch any IO exceptions. e.printStackTrace(); ,向標(biāo)準(zhǔn)設(shè)備輸出,使用System.out.println/System.out.print兩個(gè)常用的方法向標(biāo)準(zhǔn)設(shè)備輸出 println()方法將參數(shù)打印出來,并加上”n”字符。 print()方法,打印參數(shù),但不加新行 print和println方法對多數(shù)簡單數(shù)據(jù)類型進(jìn)行了重載(boolean, char, int, long, float, double)和char, Object以及String print(Object)或println(Object)將會調(diào)用該對象的toString()方法,打印它的返回字符串,向標(biāo)準(zhǔn)設(shè)備輸出示例,public class Echo public static void main(String args) int a = 100; boolean b = true; System.out.print(echo an int primitive type data:); System.out.println(a); System.out.print(echo a boolean primitive typ

溫馨提示

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

最新文檔

評論

0/150

提交評論