ImageVerifierCode 换一换
格式:DOCX , 页数:34 ,大小:445.37KB ,
资源ID:2920337      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/2920337.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(java 实验六 输入输出 实验报告.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

java 实验六 输入输出 实验报告.docx

1、java 实验六 输入输出 实验报告实验六 输入输出实验目标:1. 理解I/O流的概念与机制,掌握其分类;2. 掌握文本文件的读写、二进制文件的读写、处理流类的概念和用法、对象序列化;3. 掌握File类,压缩流类和RandomAccessFile类的使用;4. 遇到I/O方面的问题,能够自行查阅API稳当解决。实验任务:1、课本198页习题2,并使用这个程序来copy自己硬盘的大文件(任何格式都可以,如电影,最好大于500MB),看看需时多少?提示:首先需要看懂课本文本文件例子,再模仿修改,关键不同在下面三点。(1)文本文件复制中是每次读一行,而在二进制文件复制中是自行指定读取多少字节的数据

2、,写法如下。private boolean copyFiles() /这个私有方法用来拷贝文件,如无异常返回true try byte buf = new byte512; int num = source.read(buf); /从源文件读取数据 while (num 0) / 只要能够读取数据,就继续读 dest.write(buf, 0, num); /向目标文件写入 num = source.read(buf); /从源文件读取数据 catch (IOException iox) System.out.println(Problem reading or writing); retur

3、n false; return true; -以上请注意byte buf = new byte512,说明每次只读512字节,请更换每次读取的缓冲字节数,比如byte buf = new byte5120,每次读5mb左右,请试试copy效率回有所提高吗?请截图回答。(2)需时多少可以通过CPU时钟计算,方法见下。Calendar start=Calendar.getInstance(); new BinaryFileCopy().copy(args0, args1); Calendar end=Calendar.getInstance(); long time=end.getTimeInMi

4、llis()-start.getTimeInMillis(); System.out.println(copy时间为+time+毫秒);(3)因为这个程序执行需要输入参数的,所以在eclipse里面不能直接执行的,要到dos界面去执行,这里命令行的写法就有点讲究了。首先,运行的是class文件,你先要找到class文件而不是java源代码文件,一般放在项目的bin目录下,因此必须在bin目录下执行,第二执行class文件的程序是java,课本第5页最后几行有介绍,比如运行当前目录下的HelloWorld.class,语法为:java HelloWorld,注意,必须在bin目录下执行第三,如果

5、你的类是在一个包里,比如我这个类Exe6_2.class在包charp6里面,写法为java charp6.Exe6_2第四,这个copy程序需要输入两个参数,一个源文件,一个目标文件,记得参数文件名要有后缀名,具体见下截图。当byte buf = new byte512时,该程序的代码如下:package zi;import java.io.*; import java.util.Calendar;class BinaryFileCopy String sourceName, destName; FileInputStream source; FileOutputStream dest; S

6、tring line; /打开源文件和目标文件,无异常返回true private boolean openFiles() try source = new FileInputStream(sourceName ); catch ( IOException iox ) System.out.println(Problem opening +sourceName ); return false; try dest = new FileOutputStream(destName); catch ( IOException iox ) System.out.println(Problem openi

7、ng + destName ); return false; return true; /复制文件 private boolean copyFiles() /这个私有方法用来拷贝文件,如无异常返回true try byte buf = new byte512; int num = source.read(buf); /从源文件读取数据 while (num 0) / 只要能够读取数据,就继续读 dest.write(buf, 0, num); /向目标文件写入 num = source.read(buf); /从源文件读取数据 catch (IOException iox) System.ou

8、t.println(Problem reading or writing); return false; return true; /关闭源文件和目标文件 private boolean closeFiles() boolean retVal=true; try source.close(); catch ( IOException iox ) System.out.println(Problem closing + sourceName ); retVal = false; try dest.close(); catch ( IOException iox ) System.out.prin

9、tln(Problem closing + destName ); retVal = false; return retVal; /执行复制 public boolean copy(String src, String dst ) sourceName = src ; destName = dst ; return openFiles() & copyFiles() & closeFiles();public class Ex6_1 public static void main ( String args ) Calendar start=Calendar.getInstance(); St

10、ring s1=e:/develops/six/copy1.VOB; String s2=e:/develops/six/copy2.VOB; if(new BinaryFileCopy().copy(s1, s2) System.out.print(复制成功); else System.out.print(复制失败); new BinaryFileCopy().copy(s1, s2); Calendar end=Calendar.getInstance(); long time=end.getTimeInMillis()-start.getTimeInMillis(); System.ou

11、t.println(copy时间为+time+毫秒); 其运行结果截图如下:其UML图如下图所示:当byte buf = new byte20480时,其部分代码如下: private boolean copyFiles() /这个私有方法用来拷贝文件,如无异常返回true try byte buf = new byte20480; int num = source.read(buf); /从源文件读取数据 while (num 0) / 只要能够读取数据,就继续读 dest.write(buf, 0, num); /向目标文件写入 num = source.read(buf); /从源文件读

12、取数据 catch (IOException iox) System.out.println(Problem reading or writing); return false; return true; 此时运行结果截图如下:其UML图如下:2、课本198页习题4,把分别用时截图。 提示:(1)这题关键是帮助大家理解buffer缓冲的强大与必要性,两种写法分别如下:(2)随机数的产生,请查阅API,代码见下。Random r = new Random(); int number = r.nextInt(100); (3)因为Writer是写入文本文件,所以需要把Integer类型转成Stri

13、ng类型。fw.write(Integer) number).toString();(4)如何计时?与上题做法一样,通过计算开始前与结束后获得cpu当前时钟(毫秒级),两个数值的差就是代码运行时间。第一种写法的代码如下:package zi;import java.io.*; public class fileWriter public static void main(String args) throws IOException long time = System.currentTimeMillis();/当前时间 FileWriter filewriter=new FileWriter

14、(e:/develops/six/Random1.txt); int number; for(int i=1;i=100000;i+) number=(int)(Math.random()*10000); filewriter.write(number+ ); filewriter.close(); time=System.currentTimeMillis()-time;/时间差System.out.println(用时为:+time+微秒.); 其运行结果如图所示:其UML图如下:第二种写法的代码如下:package zi;import java.io.*; public class bufferedWriter public static void main(String args) throws IOException long time = System.currentTimeMil

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1