<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 41,  comments - 40,  trackbacks - 0

    如何只運行一個實例,網(wǎng)上有三種方法(單模式不算):

    1、占用端口(有效,但是有端口被挪用的缺陷)

    2、檢查文件是否存在(有效,但突然停機可能有問題)

    3、使用JNI(有效,但喪失可移植性)

    我寫的和第二個原理差不多,判斷某個文件是否被占用,并且我把記錄日志、保存日志、判斷是否運行放在一起。

    你可以轉載本Blog,但是不能篡改作者名稱。

    import java.awt.event.WindowEvent;
    import java.awt.event.WindowAdapter;

    import java.io.File;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;

    import javax.swing.JFrame;


    /*******************************************************************************
    ?*
    ?* 只運行一個實例
    ?*
    ?* Just single instance
    ?*
    ?*
    ?* Author: NeedJava
    ?*
    ?* Modified: 2007.08.29
    ?*
    ?*
    ?* 你可以使用此程序于任何地方,但請保留程序作者及注釋的完整。如果你改進了程序,
    ?*
    ?* 請在原作者后添加姓名,如:Author: NeedJava/Jack/Mike,版本及修改時間同理。
    ?*
    ?******************************************************************************/
    public final class SingleInstance
    {
    ? /*****************************************************************************
    ?? *
    ?? * 構造函數(shù)
    ?? *
    ?? ****************************************************************************/
    ? private SingleInstance()
    ? {
    ??? this( "vab", ".vab", "vab" );
    ? }


    ? public SingleInstance( String filePrefixName, String fileSuffixName, String directoryName )
    ? {
    ??? this.init( filePrefixName, fileSuffixName, directoryName );
    ? }


    ? /*****************************************************************************
    ?? *
    ?? * 原理:
    ?? *
    ?? * 用重定向?qū)藴叔e誤輸出流定向到一個日志文件,占用它,使其不能被刪除
    ?? *
    ?? * 以后的實例試圖將存在的此日志文件改名并且另存為某個文件夾下
    ?? *
    ?? * 如果成功,說明沒有程序占用它(不排除你在用它,缺陷?),正好保存日志
    ?? *
    ?? * 如果不成功,退出、renameTo命令有一箭雙雕的作用
    ?? *
    ?? ****************************************************************************/
    ? private final void init( String filePrefixName, String fileSuffixName, String directoryName )
    ? {
    ??? if( filePrefixName==null||fileSuffixName==null||directoryName==null )
    ????? {
    ??????? exitWithWarning( "Error: Invalid file name.", 10 );
    ????? }

    ??? //We need check the fileSuffixName here? From the tail?

    ??? String fileName=filePrefixName+fileSuffixName;

    ??? try{ File error=new File( fileName );

    ???????? if( error.exists() )
    ?????????? {
    ???????????? File directory=new File( directoryName );

    ???????????? String newFileName=directoryName+File.separator+convertMillisecond2DateTime( error.lastModified() )+fileSuffixName;

    ???????????? //System.out.println( newFileName );

    ???????????? if( directory.exists() )
    ?????????????? {
    ???????????????? if( directory.isDirectory() )
    ?????????????????? {
    ???????????????????? //renameTo也是好東東,既可以改名,還可以移文件
    ???????????????????? if( !error.renameTo( new File( newFileName ) ) )
    ?????????????????????? {
    ???????????????????????? exitWithWarning( "Error: The instance already exists, I will exit.", 11 );
    ?????????????????????? }
    ?????????????????? }
    ???????????????? else{ exitWithWarning( "Error: The \'"+directoryName+"\' is not a valid directory.", 21 );
    ???????????????????? }
    ?????????????? }
    ???????????? else{ if( directory.mkdirs() )
    ???????????????????? {
    ?????????????????????? //renameTo也是好東東,既可以改名,還可以移文件
    ?????????????????????? if( !error.renameTo( new File( newFileName ) ) )
    ???????????????????????? {
    ?????????????????????????? exitWithWarning( "Error: The instance already exists, I will exit.", 11 );
    ???????????????????????? }
    ???????????????????? }
    ?????????????????? else{ exitWithWarning( "Error: Failed to create the \'"+directoryName+"\' directory.", 22 );
    ?????????????????????? }
    ???????????????? }
    ?????????? }

    ???????? //重定向標準錯誤輸出流,所有功能全靠它。來,啵一個
    ???????? System.setErr( new PrintStream( new FileOutputStream( fileName ) ) );
    ?????? }
    ??? catch( FileNotFoundException fnfe )
    ???????? {
    ?????????? //fnfe.printStackTrace();

    ?????????? exitWithWarning( fnfe.toString(), 23 );
    ???????? }
    ??? catch( IOException ioe )
    ???????? {
    ?????????? //ioe.printStackTrace();

    ?????????? exitWithWarning( ioe.toString(), 29 );
    ???????? }
    ??? catch( Exception e )
    ???????? {
    ?????????? //e.printStackTrace();

    ?????????? exitWithWarning( e.toString(), 99 );
    ???????? }
    ? }


    ? /*****************************************************************************
    ?? *
    ?? * You can change the milliseconds to the date and time string here
    ?? *
    ?? * I just return the original
    ?? *
    ?? * Don't get the same string :)
    ?? *
    ?? ****************************************************************************/
    ? private final String convertMillisecond2DateTime( long millisecond )
    ? {
    ??? return ""+millisecond;
    ? }


    ? /*****************************************************************************
    ?? *
    ?? * Return the warning and the exit code, then exit the application
    ?? *
    ?? ****************************************************************************/
    ? private final void exitWithWarning( String warning, int code )
    ? {
    ??? System.out.println( warning==null ? "" : warning );

    ??? System.exit( code );
    ? }


    ? /*****************************************************************************
    ?? *
    ?? * 主函數(shù)入口
    ?? *
    ?? ****************************************************************************/
    ? public final static void main( String[] args )
    ? {
    ??? SingleInstance si=new SingleInstance( "error", ".txt", "error" );

    ??? final JFrame frame=new JFrame( "Just single instance. By NeedJava. 2007.08.29" );

    ??? //I stay here, to make some friends happy.
    ??? frame.addWindowListener( new WindowAdapter()
    ??? {
    ????? public void windowClosing( WindowEvent we )
    ????? {
    ??????? frame.dispose();

    ??????? System.exit( 0 );
    ????? }
    ??? } );

    ??? frame.setBounds( 200, 200, 700, 70 );

    ??? frame.setVisible( true );
    ? }
    }

    posted on 2007-08-29 22:11 NeedJava 閱讀(3208) 評論(8)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲国产香蕉人人爽成AV片久久| 国产V亚洲V天堂无码| 丰满少妇作爱视频免费观看| 曰韩亚洲av人人夜夜澡人人爽| 亚洲黄色免费观看| 美女被免费视频网站a| 亚洲AV人人澡人人爽人人夜夜| 西西大胆无码视频免费| 免费看美女午夜大片| 麻豆亚洲AV永久无码精品久久| 午夜视频免费成人| 国产自国产自愉自愉免费24区| 亚洲精品乱码久久久久久V | 久久午夜免费视频| 免费夜色污私人影院网站电影| 亚洲精品线在线观看| 丁香亚洲综合五月天婷婷| 97在线视频免费播放| 一级全免费视频播放| 亚洲色大18成人网站WWW在线播放| 亚洲色婷婷六月亚洲婷婷6月| 大学生美女毛片免费视频| 国产免费AV片在线观看| 麻豆亚洲AV成人无码久久精品 | 久久亚洲精品中文字幕| 亚洲国产成人久久一区WWW| 性xxxxx免费视频播放| a级片在线免费看| 亚洲国产成人精品无码区二本| 亚洲午夜久久影院| 亚洲综合激情另类专区| 日本19禁啪啪无遮挡免费动图| 51精品视频免费国产专区| 色www永久免费| 美女尿口扒开图片免费| 亚洲大尺码专区影院| 中文字幕久久亚洲一区| 国产精品久久久久影院免费| 五月婷婷综合免费| 91精品国产免费入口| 国产偷伦视频免费观看|