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

上传人:b****3 文档编号:2920337 上传时间:2022-11-16 格式:DOCX 页数:34 大小:445.37KB
下载 相关 举报
java 实验六 输入输出 实验报告.docx_第1页
第1页 / 共34页
java 实验六 输入输出 实验报告.docx_第2页
第2页 / 共34页
java 实验六 输入输出 实验报告.docx_第3页
第3页 / 共34页
java 实验六 输入输出 实验报告.docx_第4页
第4页 / 共34页
java 实验六 输入输出 实验报告.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

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

《java 实验六 输入输出 实验报告.docx》由会员分享,可在线阅读,更多相关《java 实验六 输入输出 实验报告.docx(34页珍藏版)》请在冰豆网上搜索。

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

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

实验六输入输出

实验目标:

1.理解I/O流的概念与机制,掌握其分类;

2.掌握文本文件的读写、二进制文件的读写、处理流类的概念和用法、对象序列化;

3.掌握File类,压缩流类和RandomAccessFile类的使用;

4.遇到I/O方面的问题,能够自行查阅API稳当解决。

实验任务:

1、课本198页习题2,并使用这个程序来copy自己硬盘的大文件(任何格式都可以,如电影,最好大于500MB),看看需时多少?

提示:

首先需要看懂课本文本文件例子,再模仿修改,关键不同在下面三点。

(1)文本文件复制中是每次读一行,而在二进制文件复制中是自行指定读取多少字节的数据,写法如下。

privatebooleancopyFiles(){//这个私有方法用来拷贝文件,如无异常返回true

try{

byte[]buf=newbyte[512];

intnum=source.read(buf);//从源文件读取数据

while(num>0){//只要能够读取数据,就继续读

dest.write(buf,0,num);//向目标文件写入

num=source.read(buf);//从源文件读取数据

}

}

catch(IOExceptioniox){

System.out.println("Problemreadingorwriting");

returnfalse;

}

returntrue;

}

-------------------

以上请注意byte[]buf=newbyte[512],说明每次只读512字节,请更换每次读取的缓冲字节数,比如byte[]buf=newbyte[5120],每次读5mb左右,请试试copy效率回有所提高吗?

请截图回答。

(2)需时多少可以通过CPU时钟计算,方法见下。

Calendarstart=Calendar.getInstance();

newBinaryFileCopy().copy(args[0],args[1]);

Calendarend=Calendar.getInstance();

longtime=end.getTimeInMillis()-start.getTimeInMillis();

System.out.println("copy时间为"+time+"毫秒");

(3)因为这个程序执行需要输入参数的,所以在eclipse里面不能直接执行的,要到dos界面去执行,这里命令行的写法就有点讲究了。

首先,运行的是class文件,你先要找到class文件而不是java源代码文件,一般放在项目的bin目录下,因此必须在bin目录下执行,第二执行class文件的程序是java,课本第5页最后几行有介绍,比如运行当前目录下的HelloWorld.class,语法为:

javaHelloWorld,注意,必须在bin目录下执行

第三,如果你的类是在一个包里,比如我这个类Exe6_2.class在包charp6里面,写法为javacharp6.Exe6_2

第四,这个copy程序需要输入两个参数,一个源文件,一个目标文件,记得参数文件名要有后缀名,具体见下截图。

当byte[]buf=newbyte[512]时,该程序的代码如下:

packagezi;

importjava.io.*;

importjava.util.Calendar;

classBinaryFileCopy{

StringsourceName,destName;

FileInputStreamsource;

FileOutputStreamdest;

Stringline;

//打开源文件和目标文件,无异常返回true

privatebooleanopenFiles(){

try{

source=newFileInputStream(sourceName);

}

catch(IOExceptioniox){

System.out.println("Problemopening"+sourceName);

returnfalse;

}

try{

dest=newFileOutputStream(destName);

}

catch(IOExceptioniox)

{

System.out.println("Problemopening"+destName);

returnfalse;

}

returntrue;

}

//复制文件

privatebooleancopyFiles(){//这个私有方法用来拷贝文件,如无异常返回true

try{

byte[]buf=newbyte[512];

intnum=source.read(buf);//从源文件读取数据

while(num>0){//只要能够读取数据,就继续读

dest.write(buf,0,num);//向目标文件写入

num=source.read(buf);//从源文件读取数据

}

}

catch(IOExceptioniox){

System.out.println("Problemreadingorwriting");

returnfalse;

}

returntrue;

}

//关闭源文件和目标文件

privatebooleancloseFiles(){

booleanretVal=true;

try{

source.close();

}

catch(IOExceptioniox){

System.out.println("Problemclosing"+sourceName);

retVal=false;

}

try{

dest.close();

}

catch(IOExceptioniox){

System.out.println("Problemclosing"+destName);

retVal=false;

}

returnretVal;

}

//执行复制

publicbooleancopy(Stringsrc,Stringdst){

sourceName=src;

destName=dst;

returnopenFiles()&©Files()&&closeFiles();

}

}

 

publicclassEx6_1{

publicstaticvoidmain(String[]args){

Calendarstart=Calendar.getInstance();

Strings1="e:

/develops/six/copy1.VOB";

Strings2="e:

/develops/six/copy2.VOB";

if(newBinaryFileCopy().copy(s1,s2))

System.out.print("复制成功");

else

System.out.print("复制失败");

newBinaryFileCopy().copy(s1,s2);

Calendarend=Calendar.getInstance();

longtime=end.getTimeInMillis()-start.getTimeInMillis();

System.out.println("copy时间为"+time+"毫秒");

}

}

 

其运行结果截图如下:

其UML图如下图所示:

 

当byte[]buf=newbyte[20480]时,其部分代码如下:

privatebooleancopyFiles(){//这个私有方法用来拷贝文件,如无异常返回true

try{

byte[]buf=newbyte[20480];

intnum=source.read(buf);//从源文件读取数据

while(num>0){//只要能够读取数据,就继续读

dest.write(buf,0,num);//向目标文件写入

num=source.read(buf);//从源文件读取数据

}

}

catch(IOExceptioniox){

System.out.println("Problemreadingorwriting");

returnfalse;

}

returntrue;

}

此时运行结果截图如下:

其UML图如下:

2、课本198页习题4,把分别用时截图。

提示:

(1)这题关键是帮助大家理解buffer缓冲的强大与必要性,两种写法分别如下:

(2)随机数的产生,请查阅API,代码见下。

Randomr=newRandom();

intnumber=r.nextInt(100);

(3)因为Writer是写入文本文件,所以需要把Integer类型转成String类型。

fw.write(((Integer)number).toString());

(4)如何计时?

与上题做法一样,通过计算开始前与结束后获得cpu当前时钟(毫秒级),两个数值的差就是代码运行时间。

第一种写法的代码如下:

packagezi;

importjava.io.*;

publicclassfileWriter{

publicstaticvoidmain(String[]args)

throwsIOException{

longtime=System.

currentTimeMillis();

//当前时间

FileWriterfilewriter=newFileWriter("e:

/develops/six/Random1.txt");

intnumber;

for(inti=1;i<=100000;i++){

number=(int)(Math.random()*10000);

filewriter.write(number+"");

}

filewriter.close();

time=System.

currentTimeMillis

()-time;

//时间差

System.out.println("用时为:

"+time+"微秒.");

}

}

其运行结果如图所示:

其UML图如下:

第二种写法的代码如下:

packagezi;

importjava.io.*;

publicclassbufferedWriter{

publicstaticvoidmain(String[]args)throwsIOException{longtime=System.currentTimeMil

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

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

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