


版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實驗目的(1) 掌握 Java I/O 基本原理。(2) 掌握標準輸入/輸出流和Seanner類的基本使用方法。(3) 掌握 FilelnputStream> FileOutputStream 抽象類的基木使用方法。 實驗任務(1) 閱讀給定的 Java Applieation 程序,按要求回答問題并寫出運行結果。(2) 按要求編寫一個 Java Applieation 程序,并編譯、運行這個程序。 實驗內容1、從標準輸入讀取數據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) 分析程序代碼,寫出運行結果。 (2) 將編號為 6: 和 7: 的兩行改成一行語句 S
3、ystem.out.println ;輸出的結果 如何?分析為什么?(3)如果將編號為2:的那一行代碼改成double b=new double512JP么修改其 他的地方,完成正確輸入和輸出 double類型的數組元素,請寫出修改后的完整的 運行 代碼和結果。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寫肓明幻錯誤日志Stdlnput ( 1 ) Java 應用程序D:Frogramin put: output:23
5、 45.6 78.923.0 45.6 78.92、編寫程序實現如下操作過程:先在指定的當前目錄下創建名"temp"的子目 錄,在“temp”錄下創建兩個文件 “templ.txt和”temp2.txt,然后列表顯示temp目錄下的所有文件;接下來再重命名“tempi, tx乃為“temp3.txt刪除文件“temp2.txt再, 在temp目錄下創建tp子目錄,在“tp目錄下創建兩個文件“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】/創建子目錄tpFiletemp4=new File (tempPathl z ntemp4?txtn );temp4?c
8、reateNewFile();【代碼 4/ 創建文件 temp4 .txtFile temp5=n ew File(tempPathl, Htemp5?txt");temp5?createNewFile();System, out .printin ("更改后的目錄文件:n ); listsubdir(t empPat h);/遞歸顯示指定目錄的內容方法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(”仍然是一個目錄,進行遞歸調用n + f?getAbsolutePath(); 丄 istsubdir(f) ; else System,out. print In (f ? getName ();寫出運行結果 :3. 將如下三組不同類型的數據利用 DatalnputStream 和 DataOutputStream 寫入文件, 然 后從文件中讀出。三組數據
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 ” ) ; / 倉 ll 建數據輸出流for (int i = 0; i < prices? 1ength; i +) out.writeDouble(pricesi);out ? writelnt(unitsi); 【代碼 / 寫入數量 out ?
12、writeChar(1t 1);out?writeChar(1t 1);out?writeChars(describlei); out?writeChar(1n 1 );out. close () ; / 關閉數據輸出流DatalnputStream in = new DatalnputStream(創建數據輸入流double price; int unit;String desc;double total = 0.0;try利用數據輸入流讀文件內容while (true)price = in ? readDouble ();【代碼3】/讀出價格in .readChar();/throwsou
13、tthetabunit = in. readlnt();in?readChar();/throwsoutthetabdesc= in. readLine();System ? out: .printIn(”你已經訂+ unit+ n數量的” +desc + n購:n價格為:”+ price);total = total + unit * price;catch (EOFException e)捕獲異常e. printStackTrace();Syste m.out. print In(”,也價格為:$H + total);in ? close () ;/關閉數據輸入流4、將上題中的三組不同類型
14、的數據利用對象串行化的方式寫入文件,然后從文 件中讀岀。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、 / 實例化輸出對象 ooutoout .writeObject (ps); 【代碼 2】/ 寫入對象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 / 實例化輸入對象 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、比較下面兩段代碼,執行之后有什么區別,說明原因。第一段代碼: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 類找到一個文件File f = new File (nd: n + File ? sepaEatoir + "test .txtn) ; / /聲明 File對象/ 第 2 步:通過子類實例化父類對象FileWriter out = new FileWriter (f) ;/ 進行實例化/ 第 3 步:進行寫操作String str = "Hello World! ! !n
19、; / 準備一個字符串out .write (str) ;/ 將內容寫入文件/out. f lush () ;/ 強制性清空緩沖區中的內容/ 第 4 步:關閉輸出流/ out?close ();/ 此時沒有關閉第二段代碼:import java.io.public class Output St reamDemo 0 5 public static void main(String args) throws Exception / 第 1 步:使用 File 類找到一個文件File f = new File ("test. 七乂七”);/ 聲明 File 對象/ 第 2 步:通過子類實例化父類對象F i leOutput Stream out =new FileOutput St ream (f) ;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 湖南省長沙市周南教育集團重點中學2025屆初三第一次適應性考試(一模)生物試題含解析
- 江蘇省射陽實驗初中2024-2025學年初三練習題四(全國I卷)英語試題含答案
- 髖關節后入路術后護理
- 銷售合規培訓
- 公休座談會骨科護理
- 2025聘請合同范本咨詢服務合同書范本參考
- 2025租賃合同中的押金
- 護理闌尾炎查房
- 2025常規產品出口合同范本
- 2025年高考歷史總復習人教版必修1政治史默寫清單
- 灼口綜合征護理
- 實驗室氣體泄漏應急預案
- 【碳足跡報告】山東金拓熱能科技有限公司產品碳足跡報告
- 小孩進入廠區安全免責協議書(2篇)
- 動火作業安全指導手冊
- 讀書分享讀書交流會《基督山伯爵》課件
- 延安精神概論智慧樹知到答案2024年延安大學
- JT∕T 779-2010 港口設施保安評估導則
- 2024年四川省成都市中考地理+生物試卷真題(含答案解析)
- (高清版)AQ 1043-2007 礦用產品安全標志標識
- 高考數學復習-經典選擇題150道+幾何習題+數學復習練習測試題(有答案)
評論
0/150
提交評論