import javax.sound.midi.*; import java.io.*; class MidiPlayer implements Runnable...{ protected Sequence m_Midi; protected Sequencer m_player; public boolean m_bOk; private Thread m_thread; MidiPlayer(String FileName)...{ File file=new File(FileName); m_bOk=true; m_thread=null; try...{m_Midi=MidiSystem.getSequence(file);} catch(InvalidMidiDataException ie)...{m_bOk=false;} catch(IOException ie)...{m_bOk=false;} try...{m_player=MidiSystem.getSequencer();} catch(MidiUnavailableException ie)...{m_bOk=false;} } public void run()...{ while(m_thread!=null)...{ try...{m_player.open();} catch(MidiUnavailableException ie)...{} try...{m_player.setSequence(m_Midi);} catch(InvalidMidiDataException ie)...{} m_player.start(); while(m_player.isRunning()&&m_thread!=null)...{ try...{Thread.sleep(1000);} catch(InterruptedException ie)...{} } m_player.close(); } } public void play()...{ if(m_player.isRunning()==false&&m_thread==null)...{ try...{m_player.open();} catch(MidiUnavailableException ie)...{} try...{m_player.setSequence(m_Midi);} catch(InvalidMidiDataException ie)...{} m_player.start(); } } public void loop()...{ if(m_player.isRunning()==false&&m_thread==null)...{ m_thread=new Thread(this); m_thread.start(); } } public void stop()...{ if(m_thread!=null&&m_thread.isAlive())m_thread.yield(); m_thread=null; if(m_player.isRunning())m_player.close(); } } //test.java import java.awt.*; import java.awt.event.*; class fr extends Frame implements ActionListener...{ MidiPlayer p; Button b1,b2,b3; public fr()...{ super("123"); setLayout(new FlowLayout()); p=new MidiPlayer("sound.mid"); if(p.m_bOk==false)System.exit(0); b1=new Button("循环"); b2=new Button("结束"); b3=new Button("播放"); add(b1); add(b2); add(b3); b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this); addWindowListener(new WindowAdapter()...{ public void windowClosing(WindowEvent e)...{ setVisible(false); System.exit(0); } }); setSize(200,200); setVisible(true); } public void actionPerformed(ActionEvent e)...{ if(e.getSource()==b1)...{ p.loop(); } else if(e.getSource()==b2)...{ p.stop(); } else if(e.getSource()==b3)p.play(); } } public class test...{ public static void main(String args[])...{ fr f=new fr(); } } 用Java实现支持多线程的socket通信 //Server.java /**//* * Created on 2004-8-11 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package socket_communicate; import java.io.*; import java.net.*; //用java实现支持多线程的socket通信 /**//** * @author Administrator * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class Server ...{ public Server(int port) ...{ ServerSocket serversocket=null; try ...{ serversocket=new ServerSocket(port); } catch(IOException e) ...{ System.out.println("Error occurs when listening to port:"+port); System.exit(0); } System.out.println("Server is listening to port "+port); Socket clientsocket=null; try ...{ clientsocket=serversocket.accept(); } catch(IOException e) ...{ System.out.println("Error occurs:"+e.getMessage()); System.exit(0); } try ...{ PrintWriter out=new PrintWriter(clientsocket.getOutputStream(),true);//用来向客户端输出信息 BufferedReader in=new BufferedReader(new InputStreamReader(clientsocket.getInputStream())); String line; while((line=in.readLine())!=null) ...{ out.println("The message send form Client to Server is:"+line+".");//这条语句将向客户端输出信息 System.out.println("Message from Client is:"+line);//向标准输出设备输出信息 } in.close(); out.close(); clientsocket.close(); serversocket.close(); } catch(IOException e) ...{ System.err.println("Error:"+e.getMessage()); } } public static void main(String[] args) ...{ Server server=new Server(85); } } //Client.java /**//* * Created on 2004-8-11 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package socket_communicate; import java.io.*; import java.net.*; /**//** * @author Administrator * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class Client ...{ public Client(String hostname,int port) ...{ try ...{ Socket clientsocket=new Socket(hostname,port); PrintWriter out=new PrintWriter(clientsocket.getOutputStream(),true); BufferedReader in=new BufferedReader(new InputStreamReader(clientsocket.getInputStream())); BufferedReader user_in=new BufferedReader(new InputStreamReader(System.in)); String line_user; String line_server; while((line_user=user_in.readLine())!=null) ...{ if(line_user.equals("88"))//断开连接的输入 break; out.println(line_user); line_server=in.readLine(); System.out.println("Client receive:"+line_server); } out.close(); in.close(); clientsocket.close(); } catch (UnknownHostException e) ...{ System.out.println("Unknown Host Name!"); System.exit(0); } catch (IOException e) ...{ System.out.println("Error:"+e.getMessage()); } } public static void main(String[] args) ...{ Client client=new Client("168.168.168.32",85); } } 用Java程序解决服务器JSP子页面不能及时更新的问题 import java.io.*; public class FileChange ...{ public static void main(String[] args) throws IOException ...{ File Path=new File(args[0]); //获取应用程序文件目录 FileChange FC=new FileChange(); FC.listPath(Path); //调用方法,改变目录下的文件 } void listPath(File Path) throws IOException ...{ File[] files; files=Path.listFiles(); //获取目录下文件的列表 for(int i=0;i<files.length;i++)...{ try ...{ RandomAccessFile f=new RandomAccessFile(files[i],"rw"); //以读写的方式打开一个文件 f.seek(f.length()); //将文件指针放在文件尾 f.writeBytes(" "); //模拟键入回车件动作改变文件,但不改变文件实质内容 f.close(); }catch(FileNotFoundException e) ...{ System.out.println(e.getMessage()); } } } } 基于JAVA快速排序的新应用 class dulist...{ public int key; public dulist next,pre; //定义双向指针的结点 public dulist (int key)...{ //构造一个双向指针均为空的结点 this.key=key; next=null; pre=null; } public dulist creatdulist(int a[],int n)...{ //创建一个带头结点的双向循环链表 dulist p,q,la=new dulist(n); //n为链表长度 int i; la.next=la; // la为头结点 la.pre=la; q=la; //q为跟随指针 for(i=0;i<n;i++) ...{p=new dulist(a[i]); q.next=p; p.pre=q; q=p; } q.next=la; la.pre=q; return la; } public dulist qkpass (dulist la,dulist p,dulist q)...{ int t,pivotkey; //一趟快速排序,返回枢轴位置 t=pivotkey=p.key; //用表的第一个结点作枢轴 while (p!=q) //从子表的两端交替地向中间扫描 ...{while(p!=q&&q.key>=pivotkey) q=q.pre; p.key=q.key; //将比枢轴小的结点移到低端 while(p!=q&&p.key<=pivotkey) p=p.next; q.key=p.key; //将比枢轴大的移到高端 } p.key=t; //枢轴结点到位 return p; } public void sort(dulist la,dulist low,dulist high)...{ dulist pivotloc; // low和high为头和尾的子表快排 if(low!=high) ...{pivotloc=qkpass(la,low,high); //将la一分为二 if(low.key<pivotloc.key) //枢轴在low之后 sort(la,low,pivotloc.pre);//对子表递归快排 if(high.key>pivotloc.key) //枢轴在high之前 sort(la,pivotloc.next,high); } //对高子表递归快排 } public void printdulist(dulist la)...{ dulist p=la.next; //打印链表中的数据域 while(p!=la) ...{System.out.println(p.key+""); p=p.next; } System.out.println(); } } public class qsort...{ public static void main(String[] args)...{ dulist la; int[] a=new int[]...{3,2,1,6,5,10,9,8,7,4}; la=dulist.creatdulist(a,10); //将数组元素存于链表 la.printdulist(la); la.sort(la,la.next,la.pre); la.printdulist(la); } } //BorderLayoutTest.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; public class test1 { public static void main(String[] args) { cloneFrame frame = new cloneFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class cloneFrame extends JFrame { public cloneFrame() { setTitle("机试程序"); setSize(600, 400); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(new Color(56,43,85)); Button1 = new JButton("点击复制反转字符串"); Button1.addActionListener(new turnListener()); buttonPanel.add(Button1); JPanel textPanel = new JPanel(); textPanel.setBackground(new Color(100,100,100)); field1 = new JTextField("姓名:老孙;学号:2004132028", 20); field2 = new JTextField("反转", 20); field2.setEditable(false); textPanel.add(field1); textPanel.add(field2); JPanel tuPanel = new JPanel(); JLabel label = new JLabel(new ImageIcon("006.gif")); tuPanel.add(label); tuPanel.setBackground(new Color(100,100,100)); Container contentPane = getContentPane(); contentPane.add(buttonPanel, BorderLayout.SOUTH); contentPane.add(textPanel, BorderLayout.NORTH); contentPane.add(tuPanel, BorderLayout.CENTER); contentPane.setBackground(new Color(100,100,100)); } private class turnListener implements ActionListener { public void actionPerformed(ActionEvent event) { String gets = field1.getText(); int i = gets.length(); StringBuffer buffer = new StringBuffer(i); for(int j=i-1;j>=0;j--) { buffer.append(gets.charAt(j)); } String gets2 = buffer.toString(); field2.setText(gets2); } } private JButton Button1; private JTextField field1; private JTextField field2; } 如何在指定目录下查找java类文件 import java.io.*; import java.util.jar.*; import java.util.Enumeration; /** * <p>Title: SearchClass</p> * * <p>Descriptionc:查找指定目录下的class文件 </p> * * <p>Copyright: Copyright (c) 2005</p> * * <p>Company:OpenSystem&MiddleWare Group of UESTC</p> * * @author: <a href="mailto:huangmx@yeah">an_ant</a> * @version 1.0 */ public class SearchClass { static String className = ""; static int result = 0; static boolean flag = false; /** * visit * * @param f File * @throws Exception * @todo 访问文件,并比较是否为要查找的class文件 */ public static void visit(File f) throws Exception { if (! (f.isDirectory())) { String fileName = f.getName(); //f为jar文件 if (fileName.lastIndexOf(".jar") > 0) { try { JarFile jarFile = new JarFile(f); JarEntry jarEntry = jarFile.getJarEntry(className); if (jarEntry != null) { result++; System.out.println(result + ":在jar文件" + f + "中找到"); } Enumeration e = jarFile.entries(); while (e.hasMoreElements()) { String inname = e.nextElement().toString(); int j = inname.lastIndexOf("/"); if (j > 0) { String suffix = inname.substring(j + 1, inname.length()); if (suffix.equals(className)) flag = true; } if (flag) { result++; System.out.println(result + ":在jar文件:" + f + "中找到"); System.out.println(inname); flag = false; break; } } } catch (Exception e) { e.printStackTrace(); } } //f为class文件 else if (fileName.lastIndexOf(".class") > 0) { if (fileName.equals(className)) { result++; System.out.println(result + ":在class文件" + f + "中找到"); } } } else { throw new Exception("发生意外!"); } } /** * walk * * @param f File * @todo 递归目录 */ public static void walk(File f) { //如果当前File是个文件,调用visit(); if (f.isFile()) try { visit(f); } catch (Exception e) { e.printStackTrace(); } //如果当前File是个目录,存储其子目录,并递归调用walk(); else if (f.isDirectory()) { String list[] = f.list(); for (int i = 0; i < list.length; i++) walk(new File(f, list[i])); } } /** * main * * @param args String[] * @todo 主函数 */ public static void main(String args[]) { if (args.length > 1) { className = args[1] + ".class"; try { File srcDirectory = new File(args[0]); File firstList[] = srcDirectory.listFiles(); for (int i = 0; i < firstList.length; i++) { if (firstList[i].exists()) { walk(firstList[i]); } else System.err.println("不能读取目录: " + srcDirectory); } System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.out.println("搜索结果:共出现" + result + "处"); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("输入格式有误!"); System.out.println("正确输入格式:java SearchClass 搜索目录 类名称"); } } } Java图像处理技术 //以下是创建图过程的程序代码: import java.applet.Applet; import java.awt.BorderLayout; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.universe.*; import javax.media.j3d.*; import javax.vecmath.*; public class simplebox extends Applet...{ //创建BranchGroup节点 public BranchGroup createSceneGraph()...{ BranchGroup objRoot=new BranchGroup(); //生成坐标系 TransformGroup objTrans=new TransformGroup(); objRoot.addChild(objTrans); //定义外观 Appearance app=new Appearance(); //定义材质,设置材质的发射光为红色 Material material=new Material(); material.setEmissiveColor(new Color3f(1.0f,0.0f,0.0f)); app.setMaterial(material); //定义几何体 Box b=new Box(.4f,.4f,.4f,app); objTrans.addChild(b); objRoot.compile(); return objRoot; } //定义simplebox构造器,在构造器中定义布局管理,画布,场景,视图 public simplebox()...{ setLayout(new BorderLayout()); Canvas3D c=new Canvas3D(null); add("Center",c); BranchGroup scene=createSceneGraph(); SimpleUniverse u=new SimpleUniverse(c); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public static void main(String[] args)...{ new MainFrame(new simplebox(),256,256); } }