版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
Java常用類本節(jié)主要內(nèi)容System類中和屬性有關(guān)的操作標(biāo)準(zhǔn)輸入輸出Math類八種基本類型的包裝類字符串類文件操作日期操作控制臺輸入/輸出很多的程序在運行過程中要和用戶交互:接受用戶的鍵盤輸入,然后將結(jié)果顯示在標(biāo)準(zhǔn)輸出設(shè)備顯示器上.這種程序被稱為控制臺應(yīng)用程序.這種設(shè)備叫標(biāo)準(zhǔn)I/O或控制臺I/O.java.lang.System類的上成員變量提供了I/O操作功能System.out可向標(biāo)準(zhǔn)輸出設(shè)備輸出它是一個staticfinalPrintStream對象,它被初始關(guān)聯(lián)到顯示器System.in可從標(biāo)準(zhǔn)的輸入設(shè)備輸入它是一個staticfinalInputStream對象,被初始關(guān)聯(lián)到鍵盤System.err可向標(biāo)準(zhǔn)的錯誤設(shè)備輸出它是一個PrintStream對象從鍵盤輸入例子publicstaticvoidmain(String[]args){
InputStreamReaderreader=newInputStreamReader(System.in); BufferedReaderbuffer=newBufferedReader(reader); System.out.println("輸入exit退出程序"); Stringstr=null; try{ while((str=buffer.readLine())!=null){ if(str.equals("exit")) break; System.out.println("讀入的數(shù)據(jù):"+str); } buffer.close(); reader.close(); }catch(IOExceptione){ e.printStackTrace(); } }
代碼參見案例:17-1接收鍵盤輸入向標(biāo)準(zhǔn)設(shè)備輸出使用System.out.println/System.out.print兩個常用的方法向標(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以及Stringprint(Object)或println(Object)將會調(diào)用該對象的toString()方法,打印它的返回字符串向標(biāo)準(zhǔn)設(shè)備輸出例子publicclassSystemInOrOut{ publicstaticvoidmain(Stringargs[]) { inta=100; booleanb=true; System.out.print("echoanintprimitivetypedata:"); System.out.println(a); System.out.print("echoabooleanprimitivetypedata:"); System.out.println(b); System.out.print("echoanobject:"); Objecto=newObject(); System.out.println(o); } }代碼參見案例:17-2標(biāo)準(zhǔn)輸出Math類Math類中包含了一組數(shù)學(xué)函數(shù)截?。篶eil、floor、round變量的max、min、abs三角函數(shù):sin、cos、tan、asin、acos、atan、toDegrees和toRadians對數(shù)指數(shù):log和exp其它:sqrt、pow、random常數(shù):PI、EMath類使用例子publicclassTestMath{ publicstaticvoidmain(String[]args){ doublerad=Math.random();//得到一個隨機數(shù)
System.out.println(rad); //取0-100中的隨機數(shù)
intintrad=(int)Math.floor(rad*100); System.out.println(intrad); //得到PI System.out.println(Math.PI); }}代碼參見案例:17-3Math類的使用包裝類的類圖包裝類常用的方法除了boolean和Character外,其它的包裝類都有valueOf()和parseXXX方法,并且還具有byteVaue(),shortVaue(),intValue(),longValue(),floatValue()和doubleValue()方法,這些方法是最常用的方法String類String對象代表一組不可改變的Unicode字符序列它的方法可用來創(chuàng)造新的字符串:concat、replace、substring、toLowerCase、toUpperCase和trim。查找字符的方法:endWith、startWith、indexOf、lastIndexOf。比較字符的方法:equals、equalsIgnoreCase、compareTo。其它:charAt、length()String對象的創(chuàng)建法一:Stringstr=newString(“string”);法二:Stringstr=“string”;String對象創(chuàng)建(案例:17-4String對象的創(chuàng)建)Strings1=“Test”;//line1Strings2=“Test”;//line2Tests1s2Line1Line2
Strings1=newString(“Test”);//line1
Strings2=newString(“Test”);//line2Tests1s2Line1Line2TestString的不可改變性publicstaticvoidmain(String[]args){ Stringstr="ABCD"; System.out.println(str); Stringstr1=str.toLowerCase(); System.out.println(str1); System.out.println(str); }正則表達(dá)式初步正則表達(dá)式,主要可以用來做字符串處理,可以描述特定的字符模式,如:”a{2}”表示由兩個字符“a”構(gòu)成的字符串,等同于普通字符串“aa”,如”\d”代表任意一個數(shù)字0~9publicbooleanmatches(Stringregex),返回此字符串是否匹配給定的正則表達(dá)式。publicStringreplaceAll(Stringregex,Stringreplacement)
使用給定的replacement字符串替換publicclassStringTest05{
publicstaticvoidmain(String[]args) { Strings1="asdd33dfsdaf33ddsd55fdd3dssf4343sdf455ddsdddh565gggh55ddhg";
//將dd替換為"中" System.out.println(s1.replaceAll("dd","中"));
//將dd替換為"中" System.out.println(s1.replaceAll("d{2}","中"));
//將數(shù)字替換為"中" System.out.println(s1.replaceAll("\\d","中"));
//將非數(shù)字替換為"中" System.out.println(s1.replaceAll("\\D","中"));
}}類型轉(zhuǎn)換String,基本類型,包裝器類型之間互相轉(zhuǎn)換數(shù)字類DecimalFormat用于控制數(shù)字的顯示格式j(luò)ava.math.BigDecimalBigDecimal可以精確計算,特別是財務(wù)數(shù)據(jù)public
static
voidmain(String[]args){DecimalFormatformat=newDecimalFormat("#,###.##");Strings=format.format(1234.9887);System.out.println(s);format=newDecimalFormat("##.#%");doubled1=2.0/3.2;System.out.println(format.format(d1));format=newDecimalFormat("###,###.0000");s=format.format(1234.98);System.out.println(s);}StringBuffer類StringBuffer對象代表一組可改變的Unicode字符序列構(gòu)建器:StringBuffer()創(chuàng)建一個空的字符緩沖,長度為16個字符容量;StringBuffer(intcapacity)用指定的初始容量創(chuàng)建一個空的字符緩沖;StringBuffer(StringinitString)創(chuàng)建包含initString的字符緩沖,并加上16個字符的備用空間。緩沖的修改操作:append、insert、reverse、setCharAt、setLength。StringBuffer例子publicstaticvoidmain(String[]args){ StringBuffers1=newStringBuffer("Double"); s1.append("J"); s1.append(true); s1.append(""); s1.append(Math.PI); s1.insert(9,"blooean:"); s1.insert(22,"PI:"); System.out.println(s1.toString()); s1.delete(0,9); System.out.println(s1.toString());}代碼參見案例:17-5StringBuffer的使用File對象常用方法在java中,File類不僅可以操作文件,還可以操作目錄和文件名相關(guān)StringgetName()StringgetPath()StringgetAbsolutePath()StringgetParent()booleanrenameTo(FilenewName)文件檢測booleanexists()booleancanWrite()booleancanRead()booleanisFile()booleanisDirectory()File對象常用方法獲取常規(guī)文件信息
longlastModified()longlength()booleandelete()
目錄操作
booleanmkdir()String[]list()文件操作publicstaticvoidmain(String[]args){ Filefile=newFile("C:\\Borland\\JBuilder2006\\jdk1.5"); try{ file.createTempFile("wusz",".txt",file); }catch(IOExceptione){ e.printStackTrace(); } File[]files=file.listFiles(); for(inti=0;i<files.length;i++){ if(files[i].isFile()){ System.out.println("文件:"+files[i]); }else{ System.out.println("文件夾:"+files[i]); } }} }代碼參見案例:17-6文件操作Random類java.util.Random類提供了一系列產(chǎn)生隨機數(shù)的方法nextInt()產(chǎn)生下一個int類型的隨機數(shù),大于等于0nextInt(int
n)產(chǎn)生下一個int類型的隨機數(shù),值大于等于0,并且小于nnextFloat()產(chǎn)生一個float類型的隨機數(shù),大于等于0并且小于1.0nextDouble()產(chǎn)生一個double類型的隨機數(shù),大于等于0并且小于1.0nextLong()產(chǎn)生一個long類型的隨機數(shù),值位于long類型的取值范圍publicstaticvoidmain(String[]args){Randomrandom=newRandom();for(inti=0;i<5;i++){ System.out.print(""+random.nextInt(10));} }處理日期的類Java語言提供了2個類來處理日期java.util.Date:
包裝了一個long類型的數(shù)據(jù),表示與GMT標(biāo)準(zhǔn)時間1970年1月1日00:00:00這一刻相差的毫秒數(shù)java.text.DateFormat:對日期進(jìn)行格式化Date類Date類以毫秒來表示特定的日期
publicstaticvoidmain(String[]args){ Datedate=newDate();System.out.println(date); }DateFormat抽象類用于指定日期格式,它有一個具體的子類為java.text.SimpleDateFormat類,
publicstaticvoidmain(String[]args){ Datedate=newDate();SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");System.out.println(format.format(date)); }Date例子一publicstaticvoidmain(String[]args){ java.util.Datedate=newjava.util.Date();
//SimpleDateFormatsy1=newSimpleDateFormat("y-MM-ddHH:mm:ss"); SimpleDateFormatsy1=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); StringdateFormat=sy1.format(date);//如果希望分開得到年,月,日
SimpleDateFormatsy=newSimpleDateFormat("yyyy"); SimpleDateFormatsm=newSimpleDateFormat("MM"); SimpleDateFormatsd=newSimpleDateFormat("dd"); Stringsyear=sy.format(date); Stringsmon=sm.format(date); Stringsday=sd.format(date); System.out.println(dateFormat+"\n"+syear+"年"+smon+"月"+sday +"日");}代碼參見案例:17-7格式化日期Date例子二:計算時間差
publicstaticvoidmain(String[]args){ DateFormatdf=newSimpleDateFormat("yyyy-MM-dd"); try{ Dated1=df.parse("2006-3-23");
溫馨提示
- 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 神經(jīng)血管介入診療技術(shù)
- 2026中國科學(xué)院高能物理研究所文獻(xiàn)信息部中層領(lǐng)導(dǎo)人員崗位招聘2人備考題庫帶答案詳解
- 《兒科阿奇霉素注射使用的快速建議指南》解讀
- 2026云南怒江州氣象局招聘公益性崗位1人備考題庫參考答案詳解
- 2025中國人民大學(xué)通州校區(qū)建設(shè)部招聘1人備考題庫參考答案詳解
- 2025中國科學(xué)院信息工程研究所招聘4人備考題庫及答案詳解1套
- JIS C 8156-2011 普通照明用50V以上自鎮(zhèn)流LED燈安全要求
- 2026年能源數(shù)字孿生系統(tǒng)項目可行性研究報告
- 2026年智能寵物可調(diào)節(jié)碗項目公司成立分析報告
- 2026年智能PM10傳感器項目項目建議書
- 出租車頂燈設(shè)備管理辦法
- DB11∕T 637-2024 房屋結(jié)構(gòu)綜合安全性鑒定標(biāo)準(zhǔn)
- 2025年新疆中考數(shù)學(xué)真題試卷及答案
- 2025屆新疆烏魯木齊市高三下學(xué)期三模英語試題(解析版)
- DB3210T1036-2019 補充耕地快速培肥技術(shù)規(guī)程
- 混動能量管理與電池?zé)峁芾淼膮f(xié)同優(yōu)化-洞察闡釋
- T-CPI 11029-2024 核桃殼濾料標(biāo)準(zhǔn)規(guī)范
- 統(tǒng)編版語文三年級下冊整本書閱讀《中國古代寓言》推進(jìn)課公開課一等獎創(chuàng)新教學(xué)設(shè)計
- 《顧客感知價值對綠色酒店消費意愿的影響實證研究-以三亞S酒店為例(附問卷)15000字(論文)》
- 勞動仲裁申請書電子版模板
- 趙然尊:胸痛中心時鐘統(tǒng)一、時間節(jié)點定義與時間管理
評論
0/150
提交評論