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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

java实验4.docx

1、java实验4实验项目名称: Java高级编程 实验学时: 6 同组学生姓名: 实验地点: 1514/A203 实验日期: 2016.5.13/5.25/5.27 实验成绩: 批改教师: 王倩倩 批改时间: 一、实验目的和要求(1)了解文件的概念和文件对象的创建方法;(2)掌握使用文件输入输出流读写文件的方法;(3)了解线程的基本概念和多线程程序设计的基本方法;(4)掌握数据库连接的方法;(5)创建SQL查询并更新数据库中的信息;(6)调试程序要记录调试过程中出现的问题及解决办法;(7)编写程序要规范、正确,上机调试过程和结果要有记录,不断积累编程及调试经验;(8)做完实验后给出本实验的实验报

2、告。二、实验仪器和设备奔腾以上计算机,Windows 操作系统,装有JDK1.7和Eclipse软件,Access数据库。三、实验过程(1)使用文件字节输入/输出流,合并两个指定文件;当文件中的数据已排序时,合并后的数据也要求是已排序的。(2)将Java的关键字保存在一个文本文件中,判断一个字符串是否为Java的关键字。(3)编写在构造方法中产生一个1-5之间的随机数的继承Thread类的线程类DelayPrintThread,使得线程体每休眠此随机数时间就打印输出线程号和休眠时间;另外编写应用DelayPrintThread类的Java应用程序TwoThread.java,在main()方法

3、中创建两个线程,并应用sleep()控制主应用程序延迟一段时间。(4)编写继承Runnable接口的Applet多线程小程序类MultiThreadApplet,编写继承该类的Applet小程序类Clock,在Clock中重新构造父类的run()方法,实现数字时钟的功能,要求不断刷新显示时、分、秒。要求:(1)注意选用适当的文件流进行文件读写;(2)学会两种创建线程的方法,并比较使用场合;(3)养成良好的编程习惯,严格按照命名规则为包、类及类成员命名,将每个程序打包,包的命名方式如four.num1表示实验四的第一题;(4)学会查阅Java API文档,如查找常用工具类。程序清单:(建议程序中

4、适当添加注释信息,增强可读性;较长程序可分栏书写,保证报告排版整洁美观。)第1题实验代码:package shiyan4_1;import java.io.*;import javax.swing.JOptionPane;public class mergefile /extends FilterInputStreanm implements DateIput public int ct; public mergefile() public void merge(String filename1,String filename2,String filename3) try FileInputS

5、tream fin1=new FileInputStream(filename1); FileInputStream fin2=new FileInputStream(filename2); FileOutputStream fout3=new FileOutputStream(filename3,true); byte buffer=new byte512; int count=0; while(count=fin1.read(buffer)!=-1) this.ct=count; fout3.write(buffer,0,count); fin1.close(); while(count=

6、fin2.read(buffer)!=-1) this.ct+=count; fout3.write(buffer,0,count); System.out.print(&+this.ct); fin2.close(); fout3.close(); catch(FileNotFoundException ex) JOptionPane.showMessageDialog(null, filename1+文件不存在,不能合并。); catch(IOException ex) JOptionPane.showMessageDialog(null, IO异常,合并+filename1+filena

7、me2+文件未成功); public void sort(String filename) try byte temp=new bytect; int count=0; byte t; FileInputStream fin=new FileInputStream(filename); while(count=fin.read(temp)!=-1) fin.close(); FileOutputStream fout=new FileOutputStream(filename,false); for(int i=0;itemp.length;+i) for(int j=0;jtempj+1)

8、t=tempj; tempj=tempj+1; tempj+1=t; fout.write(temp); fout.close(); catch(FileNotFoundException ex) JOptionPane.showMessageDialog(null, filename+文件不存在,不能合并。); catch(IOException ex) JOptionPane.showMessageDialog(null, IO异常,合并+filename+文件未成功); public static void main(String args) mergefile mf=new merge

9、file(); mf.merge(f:tex1.txt, f:tex2.txt,f:tex3.txt); mf.sort(f:tex3.txt); 第2题实验代码:package shiyan4_2;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.swing.JOptionPane;import shiyan4_1.mergefile;public class is

10、keyword public iskeyword() public boolean judgekeyword(String filename) try FileInputStream fin=new FileInputStream(filename); byte buffer=new byte512; int count=0,ct=0; while(count=fin.read(buffer)!=-1) ct=count; fin.close(); FileInputStream fin2=new FileInputStream(filename); byte tt=new bytect; w

11、hile(count=fin2.read(tt)!=-1) ct=count; fin.close();/对于输入流打开的时候再次打开会有影响,而且要知道到底读入多少字节,还需要进行两次读写。 String str=abstract; System.out.println(str); String temp= new String(tt);/将字符数组转换成字符串 System.out.println(temp.length(); if(str.equals(temp) return true; else return false; catch(FileNotFoundException ex

12、) JOptionPane.showMessageDialog(null, filename+文件不存在,不能合并); catch(IOException ex) JOptionPane.showMessageDialog(null, IO异常,文件未成功); public static void main(String args) iskeyword kk=new iskeyword(); if(kk.judgekeyword(f:keyword.txt) System.out.println(有关键字); else System.out.println(没有关键字); 第3题实验代码:pa

13、ckage shiyan4_3;public class DelayPrintThread extends Thread int sleepTime; public DelayPrintThread() sleepTime = 1 + (int) (Math.random() * 5); public void run() int i=0; while(i+3) try Thread.sleep(sleepTime); System.out.println(the id of thread is:+Thread.currentThread().getId(); System.out.print

14、ln(the sleep time is:+sleepTime); catch (InterruptedException ev) break; package shiyan4_3;public class twothread public static void main(String args) throws InterruptedException System.out.println(currentThread=+Thread.currentThread().getName(); Thread.sleep(1000); DelayPrintThread thread1 = new De

15、layPrintThread(); DelayPrintThread thread2 = new DelayPrintThread(); thread1.start(); thread2.start(); 第4题实验代码:package shiyan4_4;import java.applet.Applet;class MultiThreadApplet extends Applet implements Runnable public void run() package shiyan4_4;import java.awt.*;import java.util.*;public class

16、Clock extends MultiThreadApplet Thread clockthread ; Font font; public Clock() public void init() this.setSize(350, 150); font = new Font(TimesRoman,Font.CENTER_BASELINE,45); public void start() if(clockthread = null) clockthread = new Thread(this,Clock); clockthread.start(); public void run() while

17、(true) repaint(); try Thread.sleep(1000); catch(InterruptedException e) break; public void paint(Graphics g) g.setFont(font); Calendar cal=Calendar.getInstance(); g.drawString(cal.get(Calendar.HOUR_OF_DAY) + : + cal.get(Calendar.MINUTE) + : + cal.get(Calendar.SECOND), 80, 100); 四、实验结果与分析第1题实验结果:第2题实

18、验结果:f:keyword.txt文本中存放的内容:abstract8有关键字第3题实验结果:currentThread=mainthe id of thread is:8the sleep time is:3the id of thread is:9the sleep time is:3the id of thread is:8the sleep time is:3the id of thread is:9the sleep time is:3the id of thread is:8the sleep time is:3the id of thread is:9the sleep time is:3第4题实验结果:

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

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