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