




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
Java常用類本節(jié)主要內容System類中和屬性有關的操作標準輸入輸出Math類八種基本類型的包裝類字符串類文件操作日期操作控制臺輸入/輸出很多的程序在運行過程中要和用戶交互:接受用戶的鍵盤輸入,然后將結果顯示在標準輸出設備顯示器上.這種程序被稱為控制臺應用程序.這種設備叫標準I/O或控制臺I/O.java.lang.System類的上成員變量提供了I/O操作功能System.out可向標準輸出設備輸出它是一個staticfinalPrintStream對象,它被初始關聯(lián)到顯示器System.in可從標準的輸入設備輸入它是一個staticfinalInputStream對象,被初始關聯(lián)到鍵盤System.err可向標準的錯誤設備輸出它是一個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接收鍵盤輸入向標準設備輸出使用System.out.println/System.out.print兩個常用的方法向標準設備輸出println()方法將參數(shù)打印出來,并加上”\n”字符。print()方法,打印參數(shù),但不加新行print和println方法對多數(shù)簡單數(shù)據(jù)類型進行了重載(boolean,char,int,long,float,double)和char[],Object以及Stringprint(Object)或println(Object)將會調用該對象的toString()方法,打印它的返回字符串向標準設備輸出例子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標準輸出Math類Math類中包含了一組數(shù)學函數(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); }正則表達式初步正則表達式,主要可以用來做字符串處理,可以描述特定的字符模式,如:”a{2}”表示由兩個字符“a”構成的字符串,等同于普通字符串“aa”,如”\d”代表任意一個數(shù)字0~9publicbooleanmatches(Stringregex),返回此字符串是否匹配給定的正則表達式。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","中"));
}}類型轉換String,基本類型,包裝器類型之間互相轉換數(shù)字類DecimalFormat用于控制數(shù)字的顯示格式java.math.BigDecimalBigDecimal可以精確計算,特別是財務數(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字符序列構建器: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類不僅可以操作文件,還可以操作目錄和文件名相關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標準時間1970年1月1日00:00:00這一刻相差的毫秒數(shù)java.text.DateFormat:對日期進行格式化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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 養(yǎng)殖合作協(xié)議合同范本
- 加工及測試合同范本
- 2025年錫林郭勒盟c1貨運從業(yè)資格證模擬考試題
- 東莞物業(yè)服務合同范本
- 六座車買賣合同范本
- 買賣貨款利息合同范本
- 勞動關系托管合同范本
- 勞務服務費合同范本
- 萬瑞地產(chǎn)合同范本
- 辦公商品采購合同范本
- 現(xiàn)代康復治療腕管綜合癥
- 福建公安基礎知識真題匯編2
- 2024年金融理財-特許金融分析CFA考試近5年真題附答案
- 2024橋式門式起重機大修項目及其技術標準
- 部編版七年級歷史下冊全冊導學案
- 酒店住宿投標方案(技術標)
- 2024風力發(fā)電葉片維保作業(yè)技術規(guī)范
- 公路工程監(jiān)理工作指導手冊
- 中建分供方資源管理辦法
- 小小演說家演講技巧教學設計
- 住院患者跌倒、墜床、壓力性損傷的風險評估及管理
評論
0/150
提交評論