继往开来 吐故纳新
日历
网志分类
· 所有网志 (990)
· 个人作品 (62)
· 软件设计 (33)
· 面向对象编程 (22)
· JavaAPI (39)
· Java开源工具 (31)
· Swing (34)
· Java语法细节 (39)
· 样式表CSS (12)
· XML (10)
· J2EE(JavaEE) (23)
· 算法数据结构 (64)
· 正则表达式 (4)
· 软件知识 (6)
· Java线程 (9)
· Web开发.Jsp/Servlet/Struts (20)
· 程序随想录 (7)
· Spring (5)
· Hibernate (7)
· J2SE 高级 (2)
· J2SE 高级 (0)
· Web开发.Ajax (16)
· Web开发.JavaScript (43)
· DB4O (2)
· Web开发.CSS/Html (22)
· C# (20)
· ERP (4)
· JDBC (1)
· 编程资源 (16)
· 编程感悟 (29)
· DB/Sql (13)
· VB (29)
· VC (2)
· 桌面脚本 (3)
· 新兴软件 (3)
· 英语学习 (21)
· 网文转载 (159)
· 职场风云 (39)
· 诗词歌赋 (32)
· 生活感言 (77)
· 奇文共赏 (13)
· 财经纵横 (6)
· 未分类 (11)
站内搜索
友情链接
· 歪酷博客
· 我的歪酷 非非共享界
· 偶要雷锋
· 豆瓣
· nczonline
· 当当网
· easyjf中文站
· Donews
· 天极Java文章列表
· W3CSchool
· taiten的BLOG
· Dojo中国
· Dojo
· Extjs.com
· Lifehack中文网志
· JaveEye的一个AS专题
· Banq's JDon
· Java 中文网址大全
· 梦想Java
· 360Doc个人图书馆
· java开源大全
· 我在硅谷动力的软件下载站
· 站长中国
· 随意贴
· CSS教学素材站
· java 参考中文站
· 面向构件与SOA社区
· 彩字生成
· 派派小说论坛
· 如坐春风
· 英语学习网
· BBC CHina
· www.dlbang.com
· 古文竖排格式在线转化工具
· 免费家谱
· 图片上传基地
· 风景壁纸
· 和风细雨
· MyC#BlogInCsdn

订阅 RSS

0207443

歪酷博客

开此博一为经验积累,二为资料收集,三为同道交流,四为资源共享.
« 上一篇: 【转贴】利用Token防止重复提交(Struts框架) 下一篇: 【翻译】java.sql包一些特殊用法 »
Junglesong @ 2007-03-01 01:57

确定何时程序将退出

当应用程序被正常关闭时,程序将首先开启任何注册了的关闭线程,等它们结束后才真正退出.
   // 注册一个关闭线程
   Runtime.getRuntime().addShutdownHook(new Thread() {
        // This method is called during shutdown
        public void run() {
            // Do shutdown work ...
        }
    });

计算耗费的时间
   // 取得当前时间 Get current time
    long start = System.currentTimeMillis();
   
    // 执行事务Do something ...
   
    // 得到毫秒单位的耗时 Get elapsed time in milliseconds
    long elapsedTimeMillis = System.currentTimeMillis()-start;
   
    // 得到秒单位的耗时 Get elapsed time in seconds
    float elapsedTimeSec = elapsedTimeMillis/1000F;
   
    // 得到分钟单位的耗时 Get elapsed time in minutes
    float elapsedTimeMin = elapsedTimeMillis/(60*1000F);
   
    // 得到小时单位的耗时 Get elapsed time in hours
    float elapsedTimeHour = elapsedTimeMillis/(60*60*1000F);
   
    // 得到日子单位的耗时 Get elapsed time in days
    float elapsedTimeDay = elapsedTimeMillis/(24*60*60*1000F);

完成一个能被排序的类
In order for a class to be used in a sorted collection such as a SortedTree or for it to be sortable by Collections.sort(), the class must implement Comparable
为了让一个类能用于如SortedTree 这样的可[排序集合中,或是让它能被Collections.sort()函数排序,类必须实现Comparable接口.
    public class MyClass implements Comparable {
        public int compareTo(Object o) {
            // If this < o, return a negative value
            // If this = o, return 0
            // If this > o, return a positive value
        }
    }

重定向标准输出及错误(在跟踪调试程序时也许能有妙用)
This example replaces standard output and error with a print stream that copies its output to both the console and to a file. 
本例将标准输出和错误替换为了打印流以让输出同时在控制台和文件中输出.



// All writes to this print stream are copied to two print streams public class TeeStream extends PrintStream { PrintStream out; public TeeStream(PrintStream out1, PrintStream out2) { super(out1); this.out = out2; } public void write(byte buf[], int off, int len) { try { super.write(buf, off, len); out.write(buf, off, len); } catch (Exception e) { } } public void flush() { super.flush(); out.flush(); } } 下面是使用的例子:

Here's an example that uses the class:
    
    try {
        // Tee standard output
        PrintStream out = new PrintStream(new FileOutputStream("out.log"));
        PrintStream tee = new TeeStream(System.out, out);
    
        System.setOut(tee);
    
        // Tee standard error
        PrintStream err = new PrintStream(new FileOutputStream("err.log"));
        tee = new TeeStream(System.err, err);
    
        System.setErr(tee);
    } catch (FileNotFoundException e) {
    }
    
    // Write to standard output and error and the log files
    System.out.println("welcome");
    System.err.println("error");

加载一个不在classpath中的类
A URLClassLoader can be used to load classes in any directory. 
URLClassLoader 可以用于加载位于其它目录的类

    // 创建一个包括类文件的目录的File对象  Create a File object on the root of the directory containing the class file

    File file = new File("c:\myclasses\");
   
    try {
        // 将File转化为URL Convert File to a URL
        URL url = file.toURL();          // file:/c:/myclasses/
        URL[] urls = new URL[]{url};
   
        // 使用目录创建一个新的ClassLoader  Create a new class loader with the directory
        ClassLoader cl = new URLClassLoader(urls);
    
        // 载入目标类
        // Load in the class; MyClass.class should be located in
        // the directory file:/c:/myclasses/com/mycompany
        Class cls = cl.loadClass("com.mycompany.MyClass");
    } catch (MalformedURLException e) {
    } catch (ClassNotFoundException e) {
    }

执行一条命令行命令
    try {
        // 执行无参命令  Execute a command without arguments
        String command = "ls";
        Process child = Runtime.getRuntime().exec(command);
   
        // 执行有参命令  Execute a command with an argument
        command = "ls /tmp";
        child = Runtime.getRuntime().exec(command);
    } catch (IOException e) {
    }

    // 如果参数包括空格,必须用数组来包含参数
    try {
        // Execute a command with an argument that contains a space
        String[] commands = new String[]{"grep", "hello world", "/tmp/f.txt"};
        Process child = Runtime.getRuntime().exec(commands);
    } catch (IOException e) {
    }

读取命令的输出
    try {
        // 执行命令 Execute command
        String command = "ls";
        Process child = Runtime.getRuntime().exec(command);
    
        // 取得命令的输出流并Get the input stream and read from it
        InputStream in = child.getInputStream();
        int c;
        while ((c = in.read()) != -1) {
            process((char)c);
        }
        in.close();
    } catch (IOException e) {
    }
向命令输入
try {
        // 执行命令 Execute command
        String command = "cat";
        Process child = Runtime.getRuntime().exec(command);
   
        // 取得输出流并写入 Get output stream to write from it
        OutputStream out = child.getOutputStream();
   
        out.write("some text".getBytes());
        out.close();
    } catch (IOException e) {
    }









相关文章:

最新评论


chen

2007-03-02 21:27

确定何时程序将退出,一般是什么时候使用啊?


2007-03-02 21:31 网址: http://junglesong.ycool.com/

释放占据的资源,比如文件,管道等.


2007-03-02 21:56 网址: http://junglesong.ycool.com/

启动别的程序也用得到的.



chen

2007-03-03 14:59

标准的输入和输出不是应该放到java.io里面么,为什么java年鉴要放到java.lang呢?这里面有什么说法么?


2007-03-03 15:21 网址: http://junglesong.ycool.com/

年鉴归类的原则是看使用的关键语句在那个包里,上面的输入输出只是用途,关键是Process child = Runtime.getRuntime().exec(command);一句.

你说呢?


评论 / 个人网页 / 扔小纸条
* 昵称

已经注册过? 请登录

新用户请先注册 以便能显示头像及追踪评论回复

Email
网址
* 评论
表情
 


 

分类小组论坛
杂谈 , 娱乐、八卦 , 文学、艺术 , 体育 , 旅游、同城 , 象牙塔 , 情感 , 时尚、生活 , 星座 , 科技

请注意遵守中华人民共和国法律法规, 如威胁到本站生存, 将依法向有关部门报告, 同时本站的相关记录可能成为对您不利的证据.

相关法律法规
全国人大常委会关于维护互联网安全的决定
中华人民共和国计算机信息系统安全保护条例
中华人民共和国计算机信息网络国际联网管理暂行规定
计算机信息网络国际联网安全保护管理办法
计算机信息系统国际联网保密管理规定