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

下載本文檔

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

文檔簡(jiǎn)介

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

2、ring args)if(args.length!=2)System.out.println(請(qǐng)按下列方式執(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()方法會(huì)返回系統(tǒng)屬性值。 System.get

3、Property()方法返回一個(gè)String來(lái)代表系統(tǒng)屬性。 在命令行中可用java D來(lái)加入一個(gè)系統(tǒng)屬性Properties類 Properties類實(shí)現(xiàn)了從名字到值的映射 propertyNames()方法返回一個(gè)包含所有屬性名的Enumeration對(duì)象 getProperty()方法返回一個(gè)代表該屬性值的字符串 使用load()或store()方法能從文件讀入屬性集或?qū)傩约瘜?xiě)入文件 Properties在java.util包中系統(tǒng)屬性例子(示例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);. .控制臺(tái)輸入/輸出 System.out可向標(biāo)準(zhǔn)輸出設(shè)備輸出 它是一個(gè)PrintStream對(duì)象 System.in可從標(biāo)準(zhǔn)的輸入設(shè)備輸入 它是一個(gè)InputStream對(duì)象 System.err可向標(biāo)準(zhǔn)的錯(cuò)誤設(shè)備輸出 它是一個(gè)PrintStream對(duì)象從鍵盤(pán)輸入例子(示例9-4)import java.io.*;public class KeyboardInput public static void main (String args) String s; /創(chuàng)建一個(gè)BufferedReader對(duì)象從鍵盤(pán)逐行讀入

8、數(shù)據(jù) 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 / 每讀入一行,向標(biāo)準(zhǔn)輸出設(shè)備輸出 while (s = in.readLine() != null) System.out.println(Read: + s); / 關(guān)閉流,這步動(dòng)作在對(duì)流的操作完成

9、后一定要做。 in.close(); 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ù)打印出來(lái),并加上”n”字符。 print()方法,打印參數(shù),但不加新行 print和println方法對(duì)多數(shù)簡(jiǎn)單數(shù)據(jù)類型進(jìn)行了重載(boolean, char, int, long, float, double)和char, Object以及String print(Object)或println(Object)將會(huì)調(diào)用該對(duì)象的toString()方法,打印它的返回字符串向標(biāo)準(zhǔn)設(shè)備輸出例子(示例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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論