




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、使用Java輸入輸出常用的一些例子import java.util.Scanner;public class Main1 public static void main(String ars)Scanner s=new Scanner(System.in);System.out.println("請輸入數(shù)組行數(shù)和列數(shù)");int x=s.nextInt();int y=s.nextInt();int awarry=new intxy;/初始化數(shù)組System.out.println("請輸入數(shù)組元素");for(int i=0;i<x;i+)/循環(huán)
2、輸入for(int j=0;j<y;j+)awarryij=s.nextInt();System.out.println("你輸入的數(shù)組為");for(int i=0;i<x;i+)/循環(huán)輸出for(int j=0;j<y;j+)System.out.print(awarryij+"t");System.out.println();Scanner是SDK1.5新增的一個類,可是使用該類創(chuàng)建一個對象. Scanner reader=new Scanner(System.in); 然后reader對象調(diào)用下列方法(函數(shù)),讀取用戶在命令行輸
3、入的各種數(shù)據(jù)類型: next.Byte(),nextDouble(),nextFloat,nextInt(),nextLine(),nextLong(),nextShot() 上述方法執(zhí)行時都會造成堵塞,等待用戶在命令行輸入數(shù)據(jù)回車確認(rèn)import java.util.*; public class Example public static void main(String args) System.out.println("請輸入若干個數(shù)每輸入一個數(shù)用回車確認(rèn)"); System.out.println("最后輸入一個非數(shù)字結(jié)束輸入操作"); Sca
4、nner reader=new Scanner(System.in); double sum=0; int m=0; while(reader.hasNextDouble() double x=reader.nextDouble(); m=m+1; sum=sum+x; System.out.printf("%d個數(shù)的和為%fn",m,sum); System.out.printf("%d個數(shù)的平均值是%fn",m,sum/m); /實例:讀取并分析文本文件:hrinfo.txt,文本文件的內(nèi)容如下:老趙,28,feb-01,true小竹,22,dec-
5、03,false阿波,21,dec-03,false凱子,25,dec-03,true 程序:import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class readhuman private static void readfile(String filename) try Scanner scanner = new Scanner(new File(filename); scanner.useDelimiter(System.getProperty("
6、;line.separator"); while (scanner.hasNext() parseline(scanner.next(); scanner.close(); catch (FileNotFoundException e) System.out.println(e); private static void parseline(String line) Scanner linescanner = new Scanner(line); linescanner.useDelimiter(","); /可以修改usedelimiter參數(shù)以讀取不同分隔符分
7、隔的內(nèi)容String name = linescanner.next(); int age = linescanner.nextInt(); String idate = linescanner.next(); boolean iscertified = linescanner.nextBoolean(); System.out.println("姓名:"+name+" ,年齡:"+ age+" ,入司時間:"+ idate+" ,驗證標(biāo)記:"+iscertified ); public static void m
8、ain(String args) if (args.length != 1) System.err.println("usage: java readhuman file location"); System.exit(0); readfile(args0); / Scanner例子/輸入兩個正整數(shù)m和n,輸出它們的最小公倍數(shù)和最大公約數(shù)import java.util.*;public class Test40014 public static void main(String args) Scanner in =new Scanner(System.in); int g
9、cd, lcm, m, n,r; int repeat, ri; System.out.println("input repeat:");repeat=in.nextInt(); for(ri = 1; ri <= repeat; ri+) m=in.nextInt(); n=in.nextInt(); if(m <= 0 | n <= 0) System.out.println("m <= 0 or n <= 0"); elseint yu,tmp; int ji = m*n; if(m<n) tmp=m; m=n;
10、 n=tmp; while(yu=m%n)!=0) m=n; n = yu; gcd=n; lcm=ji/n; System.out.println("the least common multiple:"+lcm+", the greatest common divisor:"+gcd); Java中從命令控制臺輸入數(shù)據(jù)的幾種常用方法1、使用標(biāo)準(zhǔn)輸入串對象System.in System.in.read( )一次只讀入一個字節(jié)數(shù)據(jù),而我們通常要取得一個字符串或一組數(shù)字,這就很不適合,需要其他方法取得這樣的輸入,這時可以使用java.util.Scann
11、er類。2、使用Scanner:取得一個字符串或一組數(shù)字import java.util.Scanner; public class ScannerDemo. public static void main( String args ) . Scanner sc = new Scanner( System.in ); System.out.print( "Please enter a string : " ); System.out.print( "Your input is : " + sc.next( ) ); 在新增一個Scanner對象時需要一個
12、System.in對象,因為實際上還是System.in在取得用戶輸入。Scanner的next( )方法用以取得用戶輸入的字符串;nextInt( )將取得的輸入字符串轉(zhuǎn)換為整數(shù)類型;同樣,nextFloat( )轉(zhuǎn)換成浮點(diǎn)型;nextBoolean( )轉(zhuǎn)換成布爾型。 3、使用BufferedReader:取得含空格的輸入 Scanner取得的輸入以space, tab, enter 鍵為結(jié)束符,要想取得包含space在內(nèi)的輸入,可以用java.io.BufferedReader類來實現(xiàn)。例如:import java.io.*; public class BufferedReaderDem
13、o public static void main(String args) throws IOException /使用BufferedReader的readLine( )方法 /必須要處理java.io.IOException異常 BufferedReader br = new BufferedReader(new InputStreamReader( System.in ) ) /java.io.InputStreamReader繼承了Reader類 String tx = br.readLine( ); System.out.println( tx ); java初學(xué)者往往對從控制臺輸
14、入數(shù)據(jù)感到困難,本文提供了一種簡單的方法從控制臺輸入數(shù)據(jù)。java程序控制臺輸入數(shù)據(jù)的一種方法import java.awt.*; import javax.swing.*; class Aapublic static void main(String args) String ss=JOptionPane.showInputDialog("","請輸入一個數(shù)"); try int i=Integer.parseInt(ss); System.out.println("i="+i); catch(Exception e) System
15、.out.println("輸入的數(shù)據(jù)類型不對,程序?qū)⑼顺?quot;); System.exit(0); /FileInputStream例子import java.io.*;public class TestFileInputStream public static void main(String args) int b = 0;FileInputStream in = null;try in = new FileInputStream("d:sharejavaioTestFileInputStream.java"); catch (FileNotFound
16、Exception e) System.out.println("找不到指定文件"); System.exit(-1);try long num = 0;while(b=in.read()!=-1)System.out.print(char)b); num+;in.close(); System.out.println();System.out.println("共讀取了 "+num+" 個字節(jié)"); catch (IOException e1) System.out.println("文件讀取錯誤"); Syst
17、em.exit(-1);/ FileOutputStream例子import java.io.*;public class TestFileOutputStream public static void main(String args) int b = 0;FileInputStream in = null;FileOutputStream out = null;try in = new FileInputStream("d:/share/java/HelloWorld.java");out = new FileOutputStream("d:/share/ja
18、va/io/HW.java");while(b=in.read()!=-1)out.write(b);in.close(); out.close(); catch (FileNotFoundException e2) System.out.println("找不到指定文件"); System.exit(-1); catch (IOException e1) System.out.println("文件復(fù)制錯誤"); System.exit(-1);System.out.println("文件已復(fù)制");/輸入流其實是獲取信息
19、,而輸出流是將信息輸出public void upload(String path,String dest) FileInputStream in = null; FileOutputStream out = null; try in = new FileInputStream(path);/讀取文件 path為文件所在路徑 out = new FileOutputStream(dest);/將文件輸出到目標(biāo)地址 byte bytes = new byte1024; while(in.read(bytes)!=-1)/每次讀取1024個字節(jié) out.write(bytes);/將讀取的文件輸出
20、 catch(IOException e) e.printStackTrace(); catch(FileNotFoundException e) e.printStackTrace(); finally in.close(); out.close(); / InputStreamReader,OutputStreamWriter例子import java.io.*;public class I_O_Stream_RandW public static void main(String args)throws Exception char temp=new char100; InputStre
21、amReader isr=new InputStreamReader(System.in); OutputStreamWriter osw=new OutputStreamWriter(System.out); isr.read(temp,0,99); isr.close(); osw.write(temp,0,99); osw.close(); /* * 如果用字節(jié)緩沖流的話,會先寫進(jìn)內(nèi)存,直到讀寫完,才寫入硬盤,緩沖不傷硬盤 * 如果不用緩沖的話,就讀一個字節(jié)往硬盤里寫一次. */public static void main(String args) File inFile = new
22、File("d:/1.txt"); / 實例 輸入的 File 類 File outFile = new File("d:/2.txt"); / 實例 輸出的 File 類 BufferedInputStream bis = null; / 實例 輸入的緩沖 流類 BufferedOutputStream bos = null; / 實例 輸出的緩沖 流類 try byte buffered = new byte1024; / 讀文件的 字節(jié)緩沖區(qū) 1 個字節(jié) / 通過 輸入的 File 類 來實例 文件輸入流 ,通過實例好的文件輸入流來實例 緩沖流 b
23、is = new BufferedInputStream(new FileInputStream(inFile); / 通過 輸出的 File 類 來實例 文件輸出流 ,通過實例好的文件輸入流來實例 緩沖流 bos = new BufferedOutputStream(new FileOutputStream(outFile); while(bis.read(buffered) != -1) / 一次一次的往 緩沖區(qū) 里讀(1字節(jié)) bos.write(buffered); /寫 catch (FileNotFoundException e) e.printStackTrace(); catc
24、h (IOException e) e.printStackTrace(); finally try bos.flush(); / 把還殘留在內(nèi)存里的字節(jié)流 flush 進(jìn)文件里 bos.close(); / 關(guān)閉 bis.close(); / 關(guān)閉 catch (IOException e) e.printStackTrace(); /最新需要將一些數(shù)據(jù)寫進(jìn)日志里面,因此用到了Java的IO包。l 創(chuàng)建文件String filename = “test.txt”;/創(chuàng)建多級文件夾的路徑為dir1dir2dir3File newfile = new File(filename);/check
25、if the file exists,or create itif(!newfile.exists()trynewfile.createNewFile();/file.mkdir()創(chuàng)建文件夾,file.mkdirs()創(chuàng)建多級文件夾catch(Exception e)System.out.println(e.toString();l 一行一行讀取數(shù)據(jù)public static void readFile(String filename)tryFileReader fr = new FileReader(filename); /建立FileReader對象,并實例化為frBufferedRea
26、der br=new BufferedReader(fr); /建立BufferedReader對象,并實例化為brString line = br.readLine(); /從文件讀取一行字符串while(line != null)System.out.println(line);line = br.readLine(); /從文件讀取下一行字符串br.close();fr.close();catch(Exception e)System.out.println(e.toString();l 分行寫進(jìn)文本文檔,覆蓋原來的數(shù)據(jù)public static void writeFile(String filename)try FileWriter fw = new FileWriter(filename);BufferedWriter bw = new BufferedWriter(
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 玉米加工與農(nóng)業(yè)產(chǎn)業(yè)鏈優(yōu)化考核試卷
- 玻璃成分分析與調(diào)整考核試卷
- 航空航天器推進(jìn)劑供給系統(tǒng)考核試卷
- 篷布制造與智能化生產(chǎn)流程的改進(jìn)考核試卷
- 景區(qū)旅游安全生產(chǎn)責(zé)任追究制度考核試卷
- 船舶貨運(yùn)與跨境電商的融合考核試卷
- 玻璃水刀切割技術(shù)考核試卷
- 羊毛混紡紗線生產(chǎn)工藝試題考核試卷
- 影視劇服裝定制設(shè)備租賃與知識產(chǎn)權(quán)保護(hù)合同
- 電商倉儲物流服務(wù)及倉儲設(shè)施租賃及倉儲管理合同
- JT-T 1495-2024 公路水運(yùn)危險性較大工程專項施工方案編制審查規(guī)程
- 幸福心理學(xué)智慧樹知到期末考試答案章節(jié)答案2024年浙江大學(xué)
- 個人工勞務(wù)分包合同
- 5月8日世界微笑日微笑的力量生活中保持微笑宣傳課件
- 2024年四川省自然資源投資集團(tuán)有限責(zé)任公司招聘筆試參考題庫附帶答案詳解
- 2022智慧健康養(yǎng)老服務(wù)與管理專業(yè)人才培養(yǎng)調(diào)研報告
- 酒店網(wǎng)評分提升方案
- 石油化工設(shè)備維護(hù)檢修規(guī)程設(shè)備完好標(biāo)準(zhǔn)SHS010012004-副本
- 妊娠合并垂體侏儒的護(hù)理查房
- 廚房消防安全培訓(xùn)課件
- 全國工會財務(wù)知識競賽題庫及答案
評論
0/150
提交評論