隨筆 - 20  文章 - 2  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           

          引用

          蔣委員長如何將class打包exe文件
          class文件打包exe文件要經(jīng)過兩個步驟:
          第一步:將class文件打包成jar文件,請參見我的上一篇文章:http://maidini.blog.163.com/blog/static/37762704200848910573/
          第二步:將jar文件打包成exe文件。這是今天的重點

          將jar打包exe 有許多工具,我以exe4j為例詳解打包過程:
          第一步:下載exe4j,最新版本為4.1 如下圖 exe4j的官方網(wǎng)站 
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          第二步:運行exe4j.exe 我使用的是免安裝版(zip) 如下圖
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          第三步:運行軟件exe4j,然后點擊Next
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          第四步:選擇第二項"JAR in EXE mode",然后點擊Next
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          第五步:在General中填入名稱,在Directories填入生成exe的目錄位置。如下圖:我將生成exe目錄設(shè)置為C:\ok ,然后點Next
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          第六步:Executable name處填入生成exe文件的名稱,Icon File處填寫圖標(biāo)文件位置,再點Next
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          第七步:首先設(shè)置Class Path的位置,即jar文件的位置。然后點OK
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          第八步:接著設(shè)置Main-Class 的位置,即main方法所在的位置。然后點OK 如下圖
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          設(shè)置完成后,如下圖: 一定要先設(shè)置Class Path,再設(shè)置Main class
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          第九步:填寫版本號,最好不要填寫第二項。否則可能造成錯誤(見后面),最后點擊Finish即可
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          錯誤內(nèi)容:No JVM could be found on your system. Please define EXE4J_JAVA_HOME to point to an installed JDK or JRE or download a JRE from www.java.com
          當(dāng)時出現(xiàn)這個錯誤,查看了JDK和JRE都沒有問題,exe4j也是最新版。后來才發(fā)現(xiàn)是設(shè)置"Maximum version"。如果不填寫"Maximum version",就不報錯。也不知道是不是exe4j的BUG
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          第十步:完成所有設(shè)置,點擊“Click Here to Start the Application”可以運行測試生成的EXE文件
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人

          生成的最終的EXE程序,雙擊直接運行?,F(xiàn)在可以把自己寫的小程序發(fā)給朋友們玩玩啦,呵呵
          引用 如何將class打包exe文件 - 紫依韻 - 見人還是人



          文章來源:http://wxq594808632.blog.163.com/blog/static/10907975520093842753860
          posted @ 2009-04-08 16:28 武志強(qiáng) 閱讀(1966) | 評論 (0)編輯 收藏

           

          引用

          蔣委員長如何將Class打包Jar文件--Eclipse
          開發(fā)環(huán)境:Eclipse + Win XP SP2
          使用插件:Fatjar、Jigloo

          閑話少說,直接切入正題。首先打開Eclipse,創(chuàng)建一個Java工程。如下圖:我機(jī)子上的是JDK1.6
          引用 如何將Class打包Jar文件--Eclipse - 紫依韻 - 見人還是人

          第二步:用Jigloo插件創(chuàng)建一個窗體,為了更明顯演示我將窗體名字修改為“這是一個測試程序”。如下圖:
          引用 如何將Class打包Jar文件--Eclipse - 紫依韻 - 見人還是人

          第三步:現(xiàn)在就該將運行正常的java程序打包成jar文件。我使用Fatjar插件打包相當(dāng)方便、傻瓜。直接右鍵選擇要打包的工程,彈出的菜單中選擇“Build Fat Jar”。如下圖:
          引用 如何將Class打包Jar文件--Eclipse - 紫依韻 - 見人還是人

          第四步:彈出對話框,需要將引導(dǎo)路徑修改,其他選項使用默認(rèn)設(shè)置,然后點擊Finish。“Main-Class”這項修改為Main方法所在的文件。此步不可缺少,否則打包的Jar文件雙擊不可運行。
          引用 如何將Class打包Jar文件--Eclipse - 紫依韻 - 見人還是人

          稍等一下,就打包成功如下圖:出現(xiàn)一個testexe_fat.jar文件。
          引用 如何將Class打包Jar文件--Eclipse - 紫依韻 - 見人還是人

          進(jìn)入工程所在的目錄也可以看到一個testexe_fat.jar文件,雙擊它,就運行了。如果無誤應(yīng)該彈出像圖2的界面。如下圖:
          引用 如何將Class打包Jar文件--Eclipse - 紫依韻 - 見人還是人

          選擇jar文件右鍵,點擊open。相當(dāng)于雙擊jar文件如下圖:
          引用 如何將Class打包Jar文件--Eclipse - 紫依韻 - 見人還是人

          總結(jié):打包Jar文件是將分散Class文件統(tǒng)一封裝的好辦法。雙擊運行速度與沒打包前運行速度沒有區(qū)別。打包一定要設(shè)置好Main-Class的路徑,否則JVM找不到入口程序。

          如果不知道如何在Eclipse中安裝插件,請參見我的另一篇文章:http://maidini.blog.163.com/blog/static/37762704200832671334860/



          文章來源:http://wxq594808632.blog.163.com/blog/static/1090797552009384273607
          posted @ 2009-04-08 16:27 武志強(qiáng) 閱讀(3184) | 評論 (2)編輯 收藏

           

          引用

          蔣委員長Eclipse的插件安裝

          眾所周知,Eclipse無法方便快速的開發(fā)Swing程序。因為Eclipse沒有自帶圖形化編碼窗口,如果要開發(fā)圖形化程序,只有手動敲代碼非常不方便?,F(xiàn)在有一個插件Jigloo提供圖形化程序開發(fā)窗口,可以即拖即顯。如何將它安裝到Eclipse當(dāng)中呢?

          安裝插件一般使用Link方式:Link意味鏈接,Eclipse加載時通過鏈接找到安裝的插件
          引用 Eclipse的插件安裝 - 紫依韻 - 見人還是人
          如上圖所示:我的Eclipse路徑C:\Program Files\Eclipse 然后將Jigloo解壓到Eclipse安裝目錄中。這里需要注意Jigloo目錄下必須要有個叫Eclipse的目錄,Eclipse的目錄下才是放入features和plugins目錄。以上圖為例:C:\Program Files\Eclipse\Jigloo\Eclipse 路徑里放入features和plugins目錄,即features目錄路徑為:C:\Program Files\Eclipse\Jigloo\Eclipse\features,plugins目錄路徑為:C:\Program Files\Eclipse\Jigloo\Eclipse\plugins  這里必須要強(qiáng)調(diào)目錄路徑,因為我使用的是Link方式安裝插件。

          插件安裝位置正確后,就該鏈接了(Link)。在Eclipse路徑C:\Program Files\Eclipse下建立一個links目錄(必須這個名字),并在links目錄下建立一個Jigloo.link的文件(文件名必須同插件目錄名相同,后綴名必須是link)。最后用記事本打開Jigloo.link文件,填入path = Jigloo(path = 插件目錄名)。保存后,重新啟動Eclipse。如下圖
          引用 Eclipse的插件安裝 - 紫依韻 - 見人還是人
          在新建項目中出現(xiàn)了一個叫GUI Forms的項目,說明插件安裝成功,這就是Jigloo插件提供的圖形化開發(fā)工具。

          寫在最后:
          向大家推薦一個插件Aptana,這個插件可以調(diào)試Javascript代碼和HTML代碼。此插件還是第一款支持ajax調(diào)試的程序。詳見http://www.aptana.com/  插件下載地址http://update.aptana.com/install/studio/3.2/
          引用 Eclipse的插件安裝 - 紫依韻 - 見人還是人
          因為新版本要收費(只有一個月免費試用),我使用的老版本,安裝到Eclipse中有3個按鈕(如上圖,黃色驚嘆號和紅色叉)


          文章來源:http://wxq594808632.blog.163.com/blog/static/1090797552009384255251
          posted @ 2009-04-08 16:26 武志強(qiáng) 閱讀(2297) | 評論 (0)編輯 收藏

          如題,廢話不多說。

          JSP頁面

          hello.jsp

          <%@ page contentType="text/html; charset=GBK" %>
          <html>
          <head>
          <title>AJAX提交頁面</title>
          <meta http-equiv="Content-Type" content="text/html; charset=gbk">
          <script type="text/javascript" src="ajax.js"></script>
             <script type="text/javascript">
             function doso(){
               var url = "actAction.do";
               var s="武曉強(qiáng)斯蒂芬34-*(&_(*!$^_*!&#!^$+!*#&&";
               s=encodeURIComponent(s);      //轉(zhuǎn)換特殊字符
               var parameter ="name="+s+"&email=abc@abc.com&www=http://wxq594808632.blog.163.com/";
               var method = "POST";
               function callBack(text){           //回調(diào)函數(shù)
                alert("調(diào)用成功!\n名字為:"+text);
               }
             new Ajax().ajaxRequest(url,parameter,method,callBack);     //調(diào)用方法發(fā)送Request
             }
             </script>
          </head>
          <body>
          <input type="button" value="提交" onclick="doso()"/>
             </body>
          </html>

           

          java類

          ActAction.java

          package ajaxpost;

          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.action.ActionForm;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.Action;
          import java.io.UnsupportedEncodingException;
          import java.io.IOException;
          import java.io.PrintWriter;

          public class ActAction extends Action {

              public ActionForward execute(ActionMapping mapping, ActionForm form,
                                           HttpServletRequest request,
                                           HttpServletResponse response) throws
                      UnsupportedEncodingException {
                  //設(shè)置字符編碼返回的編碼
                  response.setContentType("text/html;charset=UTF-8");
                  //接收字符的編碼
                  try {
                      request.setCharacterEncoding("UTF-8");
                  } catch (UnsupportedEncodingException e) {
                      e.printStackTrace();
                  }
                  PrintWriter out = null;
                  try {
                      out = response.getWriter();
                  } catch (IOException ex) {
                      ex.printStackTrace();
                  }
                  String name = request.getParameter("name");
                  String email = request.getParameter("email");
                  String www = request.getParameter("www");
                  System.out.println(email);
                  System.out.println(name);
                  System.out.println(www);
               //   out.print(name+"\n"+email+"\n"+www);
                  out.print(name);         //返回值
                  return null;
              }
          }

          最重要滴

          ajax.js

          我用別人的。感謝這位大哥。。

          /*
          author zhangshuling
          email 
          zhangshuling1214@126.com
          */
          function  Ajax(){
              var _xmlHttp = null;
           this.createXMLHttpRequest = function(){
            try{
             if (window.ActiveXObject) {                                                     
              _xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");                                     
             }                                                                               
             else if (window.XMLHttpRequest) {                                                   
              _xmlHttp = new XMLHttpRequest();                                               
             }
            }catch(e){
               alert(e.name +" : " + e.message);
            }
           }
           
           this.backFunction = function(_backFunction){
            if(_xmlHttp.readyState == 4) {
             if(_xmlHttp.status == 200) {
              _backFunction(_xmlHttp.responseText);//這里可以設(shè)置返回類型
             }
            }
             _xmlHttp.onreadystatechange = null;
           }

           this.doPost = function(_url,_parameter,_backFunction){
               try{
                _xmlHttp.open("POST",_url, false);
             _xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
             _xmlHttp.send(_parameter);
             }catch(e){
              alert(e.name +" : " + e.message);
               }
           }
           
           this.doGet = function(_url,_parameter,_backFunction){
              try{
                  var _random = Math.round(Math.random()*10000);
                  _xmlHttp.open("GET", (_url+"?random=" +_random +"&" + _parameter), false);
               _xmlHttp.send(null);
             }catch(e){
                alert(e.name +" : " + e.message);
             }
           }
           
              this.ajaxRequest = function(_url,_parameter,_method,_backFunction){
                    try{
                      this.createXMLHttpRequest();
                   if(_method.toLowerCase() == "post"){
                      this.doPost(_url,_parameter,_backFunction);
                   }else{
                      this.doGet(_url,_parameter,_backFunction); 
                   }
                   try{
                     _xmlHttp.onreadystatechange = this.backFunction(_backFunction);
                   }catch(err){
                      //??????IE?????????????????
                   }
                }catch(e){
                alert(e.name +" : " + e.message);
             }
               }

          }

          /*
           var url = "ajax.do";
           var parameter = "parameter=parameter";
           var method = "post"
           
           function callBack(text){
            ....
           }
           
           new Ajax().ajaxRequest(url,parameter,method,callBack);

          */

           

          經(jīng)過本人FF,IE6,IE7,測試。完全好用


          文章來源:http://wxq594808632.blog.163.com/blog/static/109079755200923121054225
          posted @ 2009-03-31 14:11 武志強(qiáng) 閱讀(844) | 評論 (0)編輯 收藏

          import java.io.IOException;
          import java.awt.image.BufferedImage;
          import java.net.URL;
          import java.io.BufferedInputStream;
          import java.io.OutputStream;
          import java.io.FileOutputStream;
          import java.io.File;
          import javax.imageio.ImageIO;

          public class Img2 {
          //讀取遠(yuǎn)程url圖片,得到寬高
              public int[] returnImgWH(String imgurl) {
                  boolean b=false;
                  try {
                      //實例化url
                      URL url = new URL(imgurl);
                      //載入圖片到輸入流
                      java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
                      //實例化存儲字節(jié)數(shù)組
                      byte[] bytes = new byte[100];
                      //設(shè)置寫入路徑以及圖片名稱
                      OutputStream bos = new FileOutputStream(new File( "C:\\thetempimg.gif"));
                      int len;
                      while ((len = bis.read(bytes)) > 0) {
                          bos.write(bytes, 0, len);
                      }
                      bis.close();
                      bos.flush();
                      bos.close();
                      //關(guān)閉輸出流
                      b=true;
                  } catch (Exception e) {
                      //如果圖片未找到
                      b=false;
                  }
                  int[] a = new int[2];
                  if(b){    //圖片存在
                      //得到文件
                      java.io.File file = new java.io.File("C:\\thetempimg.gif");
                      BufferedImage bi = null;
                      try {
                          //讀取圖片
                          bi = javax.imageio.ImageIO.read(file);
                      } catch (IOException ex) {
                          ex.printStackTrace();
                      }
                      a[0] = bi.getWidth(); //獲得 寬度
                      a[1] = bi.getHeight(); //獲得 高度
                      //刪除文件
                      file.delete();
                  }else{     //圖片不存在
                      a=null;
                  }
                 return a;

              }

              public static void main(String[] args) {
                  Img2 i = new Img2();
                  int[] a=i.returnImgWH("        if(a==null){
                      System.out.println("圖片未找到!");
                  }else{
                      System.out.println("寬為" + a[0]);
                      System.out.println("高為" + a[1]);
                  }
              }
          }


          文章來源:
          http://wxq594808632.blog.163.com/blog/static/10907975520092274458465
          posted @ 2009-03-27 16:05 武志強(qiáng) 閱讀(2372) | 評論 (0)編輯 收藏

          如題

          默認(rèn)排序方式為asc,可省略...

          select   *   from   table   order   by   t1   desc,   t2   desc  ,   t3 desc;


          文章來源:http://wxq594808632.blog.163.com/blog/static/109079755200922444356457
          posted @ 2009-03-24 16:44 武志強(qiáng) 閱讀(176) | 評論 (0)編輯 收藏

           function jsTrim(value)
                 {
                        var reg=/^\S$/;
                        if (!value) return "";
                        if(value.length==0)
                               return value;
                        else
                        {
                               var lp,rp;
                               lp=0;rp=value.length-1;
                               while ((value.charAt(lp)==" "||(value.charCodeAt(lp)<128?(reg.test(value.charAt(lp))==false?true:false):false)||value.charAt(rp)==" "||(value.charCodeAt(rp)<128?(reg.test(value.charAt(rp))==false?true:false):false))&&rp>=lp)
                               {
                                      if (value.charAt(lp)==" "||value.charCodeAt(lp)<128?(reg.test(value.charAt(lp))==false?true:false):false)
                                             lp++;
                                      if (value.charAt(rp)==" "||value.charCodeAt(rp)<128?(reg.test(value.charAt(rp))==false?true:false):false)
                                             rp--;
                               }
                               if(lp>rp)
                                      return "";
                               else
                                      return (value.substring(lp,rp+1));

                        }
                 }


          文章來源:http://wxq594808632.blog.163.com/blog/static/10907975520092205931780
          posted @ 2009-03-20 17:10 武志強(qiáng) 閱讀(991) | 評論 (0)編輯 收藏

           

          引用

          邵波的空間Java實現(xiàn)給圖片添加水印
          import java.awt.*;
          import java.awt.image.*;
          import java.io.*;
          import javax.swing.*;
          import com.sun.image.codec.jpeg.*;

          public class WaterMark ...{
          /** *//**
          * 給圖片添加水印
          * @param filePath 需要添加水印的圖片的路徑
          * @param markContent 水印的文字
          * @param markContentColor 水印文字的顏色
          * @param qualNum 圖片質(zhì)量
          * @return
          */
          public boolean createMark(String filePath,String markContent,Color markContentColor,float qualNum,String watermark)
          ...{
          ImageIcon imgIcon=new ImageIcon(filePath);
          Image theImg =imgIcon.getImage();
          int width=theImg.getWidth(null);
          int height= theImg.getHeight(null);
          //ImageIcon waterIcon=new ImageIcon(watermark);
          //Image waterImg =waterIcon.getImage();
          BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
          Graphics2D g=bimage.createGraphics();
          g.setColor(markContentColor);
          g.setBackground(Color.white);
          g.drawImage(theImg, 0, 0, null );
          //g.drawImage(waterImg, width*2, height, null );
          g.drawString(markContent,width-100,height-30); //添加水印的文字和設(shè)置水印文字出現(xiàn)的內(nèi)容
          g.dispose();
          try...{
          FileOutputStream out=new FileOutputStream(filePath);
          JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
          JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
          param.setQuality(qualNum, true);
          encoder.encode(bimage, param);
          out.close();
          }catch(Exception e)
          ...{ return false; }
          return true;
          }
          public static void main(String arg[])
          ...{
          WaterMark wk=new WaterMark();
          if(wk.createMark("D:\1.jpg","Welcome!",Color.WHITE,70f,""))...{
          System.out.println("制作成功");
          }else...{
          System.out.println("失敗了!");
          }
          }
          }


          文章來源:http://wxq594808632.blog.163.com/blog/static/109079755200921911832219
          posted @ 2009-03-19 13:19 武志強(qiáng) 閱讀(244) | 評論 (0)編輯 收藏

           

          引用

          邵波的空間如何防止同一個賬戶被多個人同時登陸?

          1.用session超時,session為null就表示下線了      
               
            2.也可以采用數(shù)據(jù)庫中設(shè)置     臨時表     來處理      
            一個用戶登陸時向表中插進(jìn)一條記錄,用戶離開時候刪除該記錄      
            如想統(tǒng)計在線人數(shù),簡單地執(zhí)行      
            select     count(*)     from     table...     即可      
               
            3.application對象中可以記住現(xiàn)在的人數(shù),application的生命周期和服務(wù)器的生命周期一樣長。      
               
            4.還有一種方法要用到一個文件global.jsa     ,方法是(在JSP中)是sessionDestroy(),其中它是以session對象為參數(shù)的。還有要把global.jsa文件必須房子和JSP程序相同的文件目錄內(nèi)才行。      
               
            5.網(wǎng)頁自動刷新的代碼是:      
            在文件頭部加上      
            <meta     http-equiv="refresh"     content="15">      
            刷新間隔時間是15秒      
               
               
            6.在session中加入監(jiān)聽類,類的示例代碼如下:      

          onLineUser.java-------------------------------------------------------------------------------------------

          package com.shaobo.struts.action;

          import javax.servlet.http.*;
          import javax.servlet.*;
          import java.util.*;

          public class onLineUser implements HttpSessionBindingListener {
           public onLineUser() {
           }

           private Vector users = new Vector();

           public int getCount() {
            users.trimToSize();
            return users.capacity();
           }

           public boolean existUser(String userName) {
            users.trimToSize();
            boolean existUser = false;
            for (int i = 0; i < users.capacity(); i++) {
             if (userName.equals((String) users.get(i))) {
              existUser = true;
              break;
             }
            }
            return existUser;
           }

           public boolean deleteUser(String userName) {
            users.trimToSize();
            if (existUser(userName)) {
             int currUserIndex = -1;
             for (int i = 0; i < users.capacity(); i++) {
              if (userName.equals((String) users.get(i))) {
               currUserIndex = i;
               break;
              }
             }
             if (currUserIndex != -1) {
              users.remove(currUserIndex);
              users.trimToSize();
              return true;
             }
            }
            return false;
           }

           public Vector getOnLineUser() {
            return users;
           }

           public void valueBound(HttpSessionBindingEvent e) {
            users.trimToSize();
            if (!existUser(e.getName())) {
             users.add(e.getName());
             System.out.print(e.getName() + "\t登入到系統(tǒng)\t"
               + (new Date()));
             System.out.println("在線用戶數(shù)為:" + getCount());
            } else
             System.out.println(e.getName() + "已經(jīng)存在");
           }

           public void valueUnbound(HttpSessionBindingEvent e) {
            users.trimToSize();
            String userName = e.getName();
            deleteUser(userName);
            System.out.print(userName + "\t退出系統(tǒng)\t" + (new Date()));
            System.out.println("在線用戶數(shù)為:" + getCount());
           }
          }

          login.jsp--------------------------------------------------------------------------------------------

          <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
          <%
          String path = request.getContextPath();
          String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
          %>

          <jsp:useBean id="onlineuser" class="com.shaobo.struts.action.onLineUser"
           scope="application" />
          <html>
           <head>
           </head>
           <body onUnload="postMessage()">
            <center>
             <p>
             <h1>
              登陸成功,歡迎訪問
             </h1>
             </p>
            </center>
            <%
            session = request.getSession(false);
            %>
            <%
             String username = request.getParameter("username");
             if (onlineuser.existUser(username)) {
              out.println("用戶<font color=red>" + username
              + "</font>已經(jīng)登陸!");
             } else {
              session.setMaxInactiveInterval(50); //Sesion有效時長,以秒為單位      
              session.setAttribute(username, onlineuser);
              out.println("歡迎新用戶:<font color=red>" + username
              + "</font>登陸到系統(tǒng)!");
             }
             out.println("<br>當(dāng)前在線用戶人數(shù):<font color=red>"
               + onlineuser.getCount() + "</font><br>");
             String ip = request.getRemoteAddr();
             out.println("<br>IP:<font color=red>" + ip + "</font><br>");
             Vector vt = onlineuser.getOnLineUser();
             Enumeration e = vt.elements();
             out.println("在線用戶列表");
             out.println("<table border=1>");
             out.println("<tr><td>用戶名</td></tr>");
             while (e.hasMoreElements()) {
              out.println("<tr><td>");
              out.println((String) e.nextElement() + "<br>");
              out.println("</td></tr>");
             }
             out.println("</table>");
            %>
            <center>
             <p>
             </p>
             [
             <a href="javascript:window.close()">關(guān)閉窗口</a>]
             <%
              out.println("<p><a href='index.jsp'>退出系統(tǒng)</a></p>");
             %>
            </center>
            <Script>      
                        function postMessage(){                                                                  
                           <%onlineuser.deleteUser(request.getParameter("username"));%>      
                      }      
            </Script>
           </body>
          </html>

          歡迎光臨邵波的空間` http://blog.sina.com.cn/shaobolive

          QQ:343269876


          文章來源:http://wxq594808632.blog.163.com/blog/static/10907975520092191192874
          posted @ 2009-03-19 13:19 武志強(qiáng) 閱讀(804) | 評論 (0)編輯 收藏
          一.獲得控制臺用戶輸入的信息

          /** *//**獲得控制臺用戶輸入的信息
               * @return
               * @throws IOException
               */
              public String getInputMessage() throws IOException...{
                  System.out.println("請輸入您的命令∶");
                  byte buffer[]=new byte[1024];
                  int count=System.in.read(buffer);
                  char[] ch=new char[count-2];//最后兩位為結(jié)束符,刪去不要
                  for(int i=0;i<count-2;i++)
                      ch[i]=(char)buffer[i];
                  String str=new String(ch);
                  return str;
              }
          可以返回用戶輸入的信息,不足之處在于不支持中文輸入,有待進(jìn)一步改進(jìn)。

          二.復(fù)制文件

          1.以文件流的方式復(fù)制文件

          /** *//**以文件流的方式復(fù)制文件
               * @param src 文件源目錄
               * @param dest 文件目的目錄
               * @throws IOException 
               */
              public void copyFile(String src,String dest) throws IOException...{
                  FileInputStream in=new FileInputStream(src);
                  File file=new File(dest);
                  if(!file.exists())
                      file.createNewFile();
                  FileOutputStream out=new FileOutputStream(file);
                  int c;
                  byte buffer[]=new byte[1024];
                  while((c=in.read(buffer))!=-1)...{
                      for(int i=0;i<c;i++)
                          out.write(buffer[i]);       
                  }
                  in.close();
                  out.close();
              }
          該方法經(jīng)過測試,支持中文處理,并且可以復(fù)制多種類型,比如txt,xml,jpg,doc等多種格式

          三.寫文件

          1.利用PrintStream寫文件

          /** *//**
               * 文件輸出示例
               */
              public void PrintStreamDemo()...{
                  try ...{
                      FileOutputStream out=new FileOutputStream("D:/test.txt");
                      PrintStream p=new PrintStream(out);
                      for(int i=0;i<10;i++)
                          p.println("This is "+i+" line");
                  } catch (FileNotFoundException e) ...{
                      e.printStackTrace();
                  }
              }
          2.利用StringBuffer寫文件

          public void StringBufferDemo() throws IOException......{
                  File file=new File("/root/sms.log");
                  if(!file.exists())
                      file.createNewFile();
                  FileOutputStream out=new FileOutputStream(file,true);       
                  for(int i=0;i<10000;i++)......{
                      StringBuffer sb=new StringBuffer();
                      sb.append("這是第"+i+"行:前面介紹的各種方法都不關(guān)用,為什么總是奇怪的問題 ");
                      out.write(sb.toString().getBytes("utf-8"));
                  }       
                  out.close();
              }
          該方法可以設(shè)定使用何種編碼,有效解決中文問題。

          四.文件重命名


              /** *//**文件重命名
               * @param path 文件目錄
               * @param oldname  原來的文件名
               * @param newname 新文件名
               */
              public void renameFile(String path,String oldname,String newname)...{
                  if(!oldname.equals(newname))...{//新的文件名和以前文件名不同時,才有必要進(jìn)行重命名
                      File oldfile=new File(path+"/"+oldname);
                      File newfile=new File(path+"/"+newname);
                      if(newfile.exists())//若在該目錄下已經(jīng)有一個文件和新文件名相同,則不允許重命名
                          System.out.println(newname+"已經(jīng)存在!");
                      else...{
                          oldfile.renameTo(newfile);
                      }
                  }        
              }五.轉(zhuǎn)移文件目錄


          轉(zhuǎn)移文件目錄不等同于復(fù)制文件,復(fù)制文件是復(fù)制后兩個目錄都存在該文件,而轉(zhuǎn)移文件目錄則是轉(zhuǎn)移后,只有新目錄中存在該文件。

              /** *//**轉(zhuǎn)移文件目錄
               * @param filename 文件名
               * @param oldpath 舊目錄
               * @param newpath 新目錄
               * @param cover 若新目錄下存在和轉(zhuǎn)移文件具有相同文件名的文件時,是否覆蓋新目錄下文件,cover=true將會覆蓋原文件,否則不操作
               */
              public void changeDirectory(String filename,String oldpath,String newpath,boolean cover)...{
                  if(!oldpath.equals(newpath))...{
                      File oldfile=new File(oldpath+"/"+filename);
                      File newfile=new File(newpath+"/"+filename);
                      if(newfile.exists())...{//若在待轉(zhuǎn)移目錄下,已經(jīng)存在待轉(zhuǎn)移文件
                          if(cover)//覆蓋
                              oldfile.renameTo(newfile);
                          else
                              System.out.println("在新目錄下已經(jīng)存在:"+filename);
                      }
                      else...{
                          oldfile.renameTo(newfile);
                      }
                  }      
              }



          六.讀文件

          1.利用FileInputStream讀取文件


              /** *//**讀文件
               * @param path
               * @return
               * @throws IOException
               */
              public String FileInputStreamDemo(String path) throws IOException...{
                  File file=new File(path);
                  if(!file.exists()||file.isDirectory())
                      throw new FileNotFoundException();
                  FileInputStream fis=new FileInputStream(file);
                  byte[] buf = new byte[1024];
                  StringBuffer sb=new StringBuffer();
                  while((fis.read(buf))!=-1)...{
                      sb.append(new String(buf));   
                      buf=new byte[1024];//重新生成,避免和上次讀取的數(shù)據(jù)重復(fù)
                  }
                  return sb.toString();
              }


          2.利用BufferedReader讀取

          在IO操作,利用BufferedReader和BufferedWriter效率會更高一點

              /** *//**讀文件
               * @param path
               * @return
               * @throws IOException
               */
              public String BufferedReaderDemo(String path) throws IOException...{
                  File file=new File(path);
                  if(!file.exists()||file.isDirectory())
                      throw new FileNotFoundException();
                  BufferedReader br=new BufferedReader(new FileReader(file));
                  String temp=null;
                  StringBuffer sb=new StringBuffer();
                  temp=br.readLine();
                  while(temp!=null)...{
                      sb.append(temp+" ");
                      temp=br.readLine();
                  }
                  return sb.toString();
              }


          3.利用dom4j讀取xml文件

              /** *//**從目錄中讀取xml文件
               * @param path 文件目錄
               * @return
               * @throws DocumentException
               * @throws IOException
               */
              public Document readXml(String path) throws DocumentException, IOException...{
                  File file=new File(path);
                  BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
                  SAXReader saxreader = new SAXReader();
                  Document document = (Document)saxreader.read(bufferedreader);
                  bufferedreader.close();
                  return document;
              }


          七.創(chuàng)建文件(文件夾)

          1.創(chuàng)建文件夾  /** *//**創(chuàng)建文件夾
               * @param path  目錄
               */
              public void createDir(String path)...{
                  File dir=new File(path);
                  if(!dir.exists())
                      dir.mkdir();
              }
          2.創(chuàng)建新文件 /** *//**創(chuàng)建新文件
               * @param path 目錄
               * @param filename 文件名
               * @throws IOException
               */
              public void createFile(String path,String filename) throws IOException...{
                  File file=new File(path+"/"+filename);
                  if(!file.exists())
                      file.createNewFile();
              }
          八.刪除文件(目錄)

          1.刪除文件     /** *//**刪除文件
               * @param path 目錄
               * @param filename 文件名
               */
              public void delFile(String path,String filename)...{
                  File file=new File(path+"/"+filename);
                  if(file.exists()&&file.isFile())
                      file.delete();
              }
          2.刪除目錄
          要利用File類的delete()方法刪除目錄時,必須保證該目錄下沒有文件或者子目錄,否則刪除失敗,因此在實際應(yīng)用中,我們要刪除目錄,必須利用遞歸刪除該目錄下的所有子目錄和文件,然后再刪除該目錄。  /** *//**遞歸刪除文件夾
               * @param path
               */
              public void delDir(String path)...{
                  File dir=new File(path);
                  if(dir.exists())...{
                      File[] tmp=dir.listFiles();
                      for(int i=0;i<tmp.length;i++)...{
                          if(tmp[i].isDirectory())...{
                              delDir(path+"/"+tmp[i].getName());
                          }
                          else...{
                              tmp[i].delete();
                          }
                      }
                      dir.delete();
                  }
              }

          文章來源:http://wxq594808632.blog.163.com/blog/static/109079755200921911749952
          posted @ 2009-03-19 13:18 武志強(qiáng) 閱讀(233) | 評論 (0)編輯 收藏
          僅列出標(biāo)題  下一頁
          主站蜘蛛池模板: 托克托县| 新昌县| 保亭| 镇赉县| 波密县| 荆门市| 湖南省| 定远县| 台东县| 洮南市| 铜梁县| 班玛县| 农安县| 淄博市| 武隆县| 防城港市| 姚安县| 无极县| 汝阳县| 富源县| 古蔺县| 曲靖市| 巨野县| 南安市| 古交市| 喀什市| 盐津县| 东海县| 佛山市| 二连浩特市| 龙陵县| 定州市| 尉犁县| 宜兴市| 思茅市| 墨竹工卡县| 璧山县| 涪陵区| 乌兰浩特市| 民县| 仁怀市|