java实验4.docx

上传人:b****4 文档编号:24137389 上传时间:2023-05-24 格式:DOCX 页数:14 大小:67.12KB
下载 相关 举报
java实验4.docx_第1页
第1页 / 共14页
java实验4.docx_第2页
第2页 / 共14页
java实验4.docx_第3页
第3页 / 共14页
java实验4.docx_第4页
第4页 / 共14页
java实验4.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

java实验4.docx

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

java实验4.docx

java实验4

实验项目名称:

Java高级编程实验学时:

6

同组学生姓名:

————实验地点:

1514/A203

实验日期:

2016.5.13/5.25/5.27实验成绩:

批改教师:

王倩倩批改时间:

 

一、实验目的和要求

(1)了解文件的概念和文件对象的创建方法;

(2)掌握使用文件输入输出流读写文件的方法;

(3)了解线程的基本概念和多线程程序设计的基本方法;

(4)掌握数据库连接的方法;

(5)创建SQL查询并更新数据库中的信息;

(6)调试程序要记录调试过程中出现的问题及解决办法;

(7)编写程序要规范、正确,上机调试过程和结果要有记录,不断积累编程及调试经验;

(8)做完实验后给出本实验的实验报告。

二、实验仪器和设备

奔腾以上计算机,Windows操作系统,装有JDK1.7和Eclipse软件,Access数据库。

三、实验过程

(1)使用文件字节输入/输出流,合并两个指定文件;当文件中的数据已排序时,合并后的数据也要求是已排序的。

(2)将Java的关键字保存在一个文本文件中,判断一个字符串是否为Java的关键字。

(3)编写在构造方法中产生一个1-5之间的随机数的继承Thread类的线程类DelayPrintThread,使得线程体每休眠此随机数时间就打印输出线程号和休眠时间;另外编写应用DelayPrintThread类的Java应用程序TwoThread.java,在main()方法中创建两个线程,并应用sleep()控制主应用程序延迟一段时间。

(4)编写继承Runnable接口的Applet多线程小程序类MultiThreadApplet,编写继承该类的Applet小程序类Clock,在Clock中重新构造父类的run()方法,实现数字时钟的功能,要求不断刷新显示时、分、秒。

要求:

(1)注意选用适当的文件流进行文件读写;

(2)学会两种创建线程的方法,并比较使用场合;

(3)养成良好的编程习惯,严格按照命名规则为包、类及类成员命名,将每个程序打包,包的命名方式如four.num1表示实验四的第一题;

(4)学会查阅JavaAPI文档,如查找常用工具类。

程序清单:

(建议程序中适当添加注释信息,增强可读性;较长程序可分栏书写,保证报告排版整洁美观。

第1题实验代码:

packageshiyan4_1;

importjava.io.*;

importjavax.swing.JOptionPane;

publicclassmergefile//extendsFilterInputStreanmimplementsDateIput

{

publicintct;

publicmergefile()

{

}

publicvoidmerge(Stringfilename1,Stringfilename2,Stringfilename3)

{

try

{

FileInputStreamfin1=newFileInputStream(filename1);

FileInputStreamfin2=newFileInputStream(filename2);

FileOutputStreamfout3=newFileOutputStream(filename3,true);

bytebuffer[]=newbyte[512];

intcount=0;

while((count=fin1.read(buffer))!

=-1)

{

this.ct=count;

fout3.write(buffer,0,count);

}

fin1.close();

while((count=fin2.read(buffer))!

=-1)

{

this.ct+=count;

fout3.write(buffer,0,count);

}

System.out.print("&"+this.ct);

fin2.close();

fout3.close();

}

catch(FileNotFoundExceptionex)

{

JOptionPane.showMessageDialog(null,filename1+"文件不存在,不能合并。

");

}

catch(IOExceptionex)

{

JOptionPane.showMessageDialog(null,"IO异常,合并\""+filename1+filename2+"\"文件未成功");

}

}

publicvoidsort(Stringfilename)

{

try

{

bytetemp[]=newbyte[ct];

intcount=0;

bytet;

FileInputStreamfin=newFileInputStream(filename);

while((count=fin.read(temp))!

=-1)

{

}

fin.close();

FileOutputStreamfout=newFileOutputStream(filename,false);

for(inti=0;i

{

for(intj=0;j

{

if(temp[j]>temp[j+1])

{

t=temp[j];

temp[j]=temp[j+1];

temp[j+1]=t;

}

}

}

fout.write(temp);

fout.close();

}

catch(FileNotFoundExceptionex)

{

JOptionPane.showMessageDialog(null,filename+"文件不存在,不能合并。

");

}

catch(IOExceptionex)

{

JOptionPane.showMessageDialog(null,"IO异常,合并\""+filename+"\"文件未成功");

}

}

publicstaticvoidmain(Stringargs[])

{

mergefilemf=newmergefile();

mf.merge("f:

\\tex1.txt","f:

\\tex2.txt","f:

\\tex3.txt");

mf.sort("f:

\\tex3.txt");

}

}

第2题实验代码:

packageshiyan4_2;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjavax.swing.JOptionPane;

importshiyan4_1.mergefile;

publicclassiskeyword

{

publiciskeyword()

{

}

publicbooleanjudgekeyword(Stringfilename)

{

try

{

FileInputStreamfin=newFileInputStream(filename);

bytebuffer[]=newbyte[512];

intcount=0,ct=0;

while((count=fin.read(buffer))!

=-1)

{

ct=count;

}

fin.close();

FileInputStreamfin2=newFileInputStream(filename);

bytett[]=newbyte[ct];

while((count=fin2.read(tt))!

=-1)

{

ct=count;

}

fin.close();//对于输入流打开的时候再次打开会有影响,而且要知道到底读入多少字节,还需要进行两次读写。

Stringstr="abstract";

System.out.println(str);

Stringtemp=newString(tt);//将字符数组转换成字符串

System.out.println(temp.length());

if(str.equals(temp))

{

returntrue;

}

else

{

returnfalse;

}

}

catch(FileNotFoundExceptionex)

{

JOptionPane.showMessageDialog(null,filename+"文件不存在,不能合并");

}

catch(IOExceptionex)

{

JOptionPane.showMessageDialog(null,"IO异常,文件未成功");

}

}

publicstaticvoidmain(Stringargs[])

{

iskeywordkk=newiskeyword();

if(kk.judgekeyword("f:

\\keyword.txt"))

{

System.out.println("有关键字");

}

else

{

System.out.println("没有关键字");

}

}

}

第3题实验代码:

packageshiyan4_3;

 

publicclassDelayPrintThreadextendsThread

{

intsleepTime;

publicDelayPrintThread()

{

sleepTime=1+(int)(Math.random()*5);

}

publicvoidrun()

{

inti=0;

while(i++<3)

{

try

{

Thread.sleep(sleepTime);

System.out.println("theidofthreadis:

"+Thread.currentThread().getId());

System.out.println("thesleeptimeis:

"+sleepTime);

}

catch(InterruptedExceptionev)

{

break;

}

}

}

}

packageshiyan4_3;

publicclasstwothread

{

publicstaticvoidmain(Stringargs[])throwsInterruptedException

{

System.out.println("currentThread="+Thread.currentThread().getName());

Thread.sleep(1000);

DelayPrintThreadthread1=newDelayPrintThread();

DelayPrintThreadthread2=newDelayPrintThread();

thread1.start();

thread2.start();

}

}

第4题实验代码:

packageshiyan4_4;

importjava.applet.Applet;

classMultiThreadAppletextendsAppletimplementsRunnable

{

publicvoidrun()

{

}

}

packageshiyan4_4;

importjava.awt.*;

importjava.util.*;

publicclassClockextendsMultiThreadApplet

{

Threadclockthread;

Fontfont;

publicClock()

{

}

publicvoidinit()

{

this.setSize(350,150);

font=newFont("TimesRoman",Font.CENTER_BASELINE,45);

}

publicvoidstart()

{

if(clockthread==null)

clockthread=newThread(this,"Clock");

clockthread.start();

}

publicvoidrun()

{

while(true)

{

repaint();

try

{

Thread.sleep(1000);

}

catch(InterruptedExceptione)

{

break;

}

}

}

publicvoidpaint(Graphicsg)

{

g.setFont(font);

Calendarcal=Calendar.getInstance();

g.drawString(cal.get(Calendar.HOUR_OF_DAY)+":

"+cal.get(Calendar.MINUTE)+":

"+cal.get(Calendar.SECOND),80,100);

}

}

四、实验结果与分析

第1题实验结果:

第2题实验结果:

f:

\keyword.txt文本中存放的内容:

abstract

8

有关键字

第3题实验结果:

currentThread=main

theidofthreadis:

8

thesleeptimeis:

3

theidofthreadis:

9

thesleeptimeis:

3

theidofthreadis:

8

thesleeptimeis:

3

theidofthreadis:

9

thesleeptimeis:

3

theidofthreadis:

8

thesleeptimeis:

3

theidofthreadis:

9

thesleeptimeis:

3

第4题实验结果:

 

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

当前位置:首页 > 人文社科 > 法律资料

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

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