實(shí)驗(yàn)11輸入輸出流_第1頁(yè)
實(shí)驗(yàn)11輸入輸出流_第2頁(yè)
實(shí)驗(yàn)11輸入輸出流_第3頁(yè)
已閱讀5頁(yè),還剩10頁(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、實(shí)驗(yàn)?zāi)康?1) 掌握 Java I/O 基本原理。(2) 掌握標(biāo)準(zhǔn)輸入/輸出流和Seanner類的基本使用方法。(3) 掌握 FilelnputStream> FileOutputStream 抽象類的基木使用方法。 實(shí)驗(yàn)任務(wù)(1) 閱讀給定的 Java Applieation 程序,按要求回答問(wèn)題并寫(xiě)出運(yùn)行結(jié)果。(2) 按要求編寫(xiě)一個(gè) Java Applieation 程序,并編譯、運(yùn)行這個(gè)程序。 實(shí)驗(yàn)內(nèi)容1、從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù)import java.io.*;import java.util.*;publie elass Stdlnput publie statie void mai

2、n(String args) throws Exeeption nH1: System.out.println( input: );2: byte b=new byte512;3: int eount=System.in.read(b);Hn4: System.out.println( eount=: +eount);5: System.out.println( noutput:n);6: for(int i=0;i<eount;i+)7: System.out.print(bi+ n ” );(1) 分析程序代碼,寫(xiě)出運(yùn)行結(jié)果。 (2) 將編號(hào)為 6: 和 7: 的兩行改成一行語(yǔ)句 S

3、ystem.out.println ;輸出的結(jié)果 如何?分析為什么?(3)如果將編號(hào)為2:的那一行代碼改成double b=new double512JP么修改其 他的地方,完成正確輸入和輸出 double類型的數(shù)組元素,請(qǐng)寫(xiě)出修改后的完整的 運(yùn)行 代碼和結(jié)果。import java .io.*;import java .u til .*;public class Stdlnput public static void main(String args) throws Exception Syste m.out. println( ninput:");double b=new dou

4、ble512;Scanner sin=new Scanner(System .i n);int count=System .in.read();/Syste m. out .println( ncount=: n4-count);Syste m.out. println( noutput:n);for(i nt i=0;i<b .l ength;i+4-)bi=sin .n extDouble();System.out.pri nt(bi+ n n);層冋題 Javadoc寫(xiě)肓明幻錯(cuò)誤日志Stdlnput ( 1 ) Java 應(yīng)用程序D:Frogramin put: output:23

5、 45.6 78.923.0 45.6 78.92、編寫(xiě)程序?qū)崿F(xiàn)如下操作過(guò)程:先在指定的當(dāng)前目錄下創(chuàng)建名"temp"的子目 錄,在“temp”錄下創(chuàng)建兩個(gè)文件 “templ.txt和”temp2.txt,然后列表顯示temp目錄下的所有文件;接下來(lái)再重命名“tempi, tx乃為“temp3.txt刪除文件“temp2.txt再, 在temp目錄下創(chuàng)建tp子目錄,在“tp目錄下創(chuàng)建兩個(gè)文件“temp4.txt和, utemp5.txt,J,然后再次顯示temp目錄下的所有文件。import java.io?* ;public class FileDirectory publ

6、ic static void main( Stri ng args)throws Exception Filecurre ntPath二 new File (n?");File tempPath=new File(currentPath,ntemp n); tempPath ?mkdir();File templ=new File(tempPath,"tempi? txt");tempi?createNewFile ();temp2?createNewFile()【代碼 1 】Filetemp2=new File(tempPath,ntemp2?txt n);Sy

7、stem ? out: ? print丄 n (” 第一次的:目建文文件tempg .txtlist subdir(tempPath);File newf =new File(tempPath , ntemp3?txtn);tempi?re nameTo(newf);temp2 . delete ();【代碼 2 / 刪除 temp2? txt 文件File tempPathl=new File(tempPath, ntp n);tempPathl?mkdir()【代們3】/創(chuàng)建子目錄tpFiletemp4=new File (tempPathl z ntemp4?txtn );temp4?c

8、reateNewFile();【代碼 4/ 創(chuàng)建文件 temp4 .txtFile temp5=n ew File(tempPathl, Htemp5?txt");temp5?createNewFile();System, out .printin ("更改后的目錄文件:n ); listsubdir(t empPat h);/遞歸顯示指定目錄的內(nèi)容方法static void listsubdir(File curre ntPath)throws Exception Stri ngfilenames=currentPath? list();for(int i=O;i<

9、filenames?length;i+)if(f?isDirectory ()【代碼5/判斷£是否是目錄File f =new File(currentPath,filenamesi); System, out. printin(”仍然是一個(gè)目錄,進(jìn)行遞歸調(diào)用n + f?getAbsolutePath(); 丄 istsubdir(f) ; else System,out. print In (f ? getName ();寫(xiě)出運(yùn)行結(jié)果 :3. 將如下三組不同類型的數(shù)據(jù)利用 DatalnputStream 和 DataOutputStream 寫(xiě)入文件, 然 后從文件中讀出。三組數(shù)據(jù)

10、如下: 19.99, 9.99, 15.99,3.99, 4.99 ; 12, & 13, 29, 50 ; nJava T-shirt", "Java Mug", ” Duke Juggling Dolls, "Ja”va Pin'V'Java Key Chain'1import java ? io;public class S2 public static void main(String args) throws IOException double prices =19.99,9.99,8? 97;int unit

11、s =12f 8,9;String describle ="java t-shirtn,"java mug","java coat"DataOutputStream out = new DataOutputStream(newFileOutputStream("test .txt ” ) ; / 倉(cāng) ll 建數(shù)據(jù)輸出流for (int i = 0; i < prices? 1ength; i +) out.writeDouble(pricesi);out ? writelnt(unitsi); 【代碼 / 寫(xiě)入數(shù)量 out ?

12、writeChar(1t 1);out?writeChar(1t 1);out?writeChars(describlei); out?writeChar(1n 1 );out. close () ; / 關(guān)閉數(shù)據(jù)輸出流DatalnputStream in = new DatalnputStream(創(chuàng)建數(shù)據(jù)輸入流double price; int unit;String desc;double total = 0.0;try利用數(shù)據(jù)輸入流讀文件內(nèi)容while (true)price = in ? readDouble ();【代碼3】/讀出價(jià)格in .readChar();/throwsou

13、tthetabunit = in. readlnt();in?readChar();/throwsoutthetabdesc= in. readLine();System ? out: .printIn(”你已經(jīng)訂+ unit+ n數(shù)量的” +desc + n購(gòu):n價(jià)格為:”+ price);total = total + unit * price;catch (EOFException e)捕獲異常e. printStackTrace();Syste m.out. print In(”,也價(jià)格為:$H + total);in ? close () ;/關(guān)閉數(shù)據(jù)輸入流4、將上題中的三組不同類型

14、的數(shù)據(jù)利用對(duì)象串行化的方式寫(xiě)入文件,然后從文 件中讀岀。import java?i o.class prices implements Serializable 代碼 2】 double price; 定義 prices 類int unit;String product;prices(double pc,int unz String pt)price=pc;unit=un;product= pt;public class SI public static void main(String args) throws IOException, ClassNotFoundException doub

15、le pri =19.99,9.99,8.97;int units =12 r 8,9;String describle = H java t-shirt n , n java mugH , n java coatn ; for (int i=0;i<prilength;i+)prices ps=new prices(prii,unitsi,describlei); FileOutpu 七 Stream fos=new FileOutputStream(n f2.txt n);Object Output S t ream oout=new ObjectOutputStream(fos);

16、 / 實(shí)例化輸出對(duì)象 ooutoout .writeObject (ps); 【代碼 2】/ 寫(xiě)入對(duì)象txtH;oout ? flush (); ps=null;Object 工 nputStream oin=new Object 工 nputStream(new Fileinputstream (nf2 . 代碼 3】 ps=(prices)oin.readObject(); oin ? close ();System? out: .println (”輸出 prices / 實(shí)例化輸入對(duì)象 oin+類的信,息、:");print (”jiage: " +ps ? pri

17、ce);print (” 七” );print (n shuliang : n +ps ? unit); print (”t ”);System ? System ?System ?System ? System ?System ?out:? print (n”) ; out:? out:? out:?out:?out ? print (”migncheng: " +ps ? product); 5、比較下面兩段代碼,執(zhí)行之后有什么區(qū)別,說(shuō)明原因。第一段代碼:import java?io.File;import java.io.FileWriter;import java.io.Wr

18、iter; public class WriterDemo03 public static void main(String args) throws Exception / 第 1 步:使用 File 類找到一個(gè)文件File f = new File (nd: n + File ? sepaEatoir + "test .txtn) ; / /聲明 File對(duì)象/ 第 2 步:通過(guò)子類實(shí)例化父類對(duì)象FileWriter out = new FileWriter (f) ;/ 進(jìn)行實(shí)例化/ 第 3 步:進(jìn)行寫(xiě)操作String str = "Hello World! ! !n

19、; / 準(zhǔn)備一個(gè)字符串out .write (str) ;/ 將內(nèi)容寫(xiě)入文件/out. f lush () ;/ 強(qiáng)制性清空緩沖區(qū)中的內(nèi)容/ 第 4 步:關(guān)閉輸出流/ out?close ();/ 此時(shí)沒(méi)有關(guān)閉第二段代碼:import java.io.public class Output St reamDemo 0 5 public static void main(String args) throws Exception / 第 1 步:使用 File 類找到一個(gè)文件File f = new File ("test. 七乂七”);/ 聲明 File 對(duì)象/ 第 2 步:通過(guò)子類實(shí)例化父類對(duì)象F i leOutput Stream out =new FileOutput St ream (f) ;

溫馨提示

  • 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)論