


版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 素质教育学习心得体会模版
- 钢结构课程设计心得体会模版
- 保洁工作个人心得体会模版
- 三晋卓越联盟·2024-2025学年高三5月质量检测卷(25-X-635C)数学(B)
- 浙江省六校(杭州二中 温州中学 金华一中 绍兴一中 舟山中学 衢州二中)联盟2025届高三5月模拟预测卷语文试卷+答案
- 小家电安规基础知识讲义
- 初中学生家长教育孩子心得体会模版
- 凝血功能异常的临床护理
- 心梗护理方案
- 高血压活动总结模版
- EDI工程手册中文
- 高二语文九日齐山登高省公开课金奖全国赛课一等奖微课获奖课件
- 广东省广州大学附中2021-2022年初二12月大联盟物理在线考试题
- 食品工程系畜产品加工技术教案
- 入股合作的协议书(2024版)
- 广东省深圳市南山区2023-2024学年七年级下学期期末英语试题
- 福建省宁德市霞浦县2024届九年级上学期期中阶段性训练数学试卷(含答案)
- 2024年广东清远市“人才引育”工程专项事业编制高层次人才招聘31人历年【重点基础提升】模拟试题(共500题)附带答案详解
- 偏差行为、卓越一生3.0版
- 小学四年级语文知识竞赛(含答案)
- 火针治疗白癜风的效果观察与评估
评论
0/150
提交评论