zoukankan      html  css  js  c++  java
  • java小例子和国际化

    /**
     * @author Rollen-Holt JVM虚拟机的内存信息
     */
    
    class hello{
    	public static void main(String[] args){
    		Runtime run = Runtime.getRuntime();
    		System.out.println("JVM的最大内存量为:  " + run.maxMemory());
    		System.out.println("JVM的空闲内存量为:  " + run.freeMemory());
    
    		for(int i = 0; i < 100000000; ++i) {
    			String str = new String("asda");
    		}
    		System.out.println("JVM的空闲内存量为:  " + run.freeMemory());
    
    		run.gc();
    		System.out.println("JVM的空闲内存量为:  " + run.freeMemory());
    	}
    }
    

      【运行结果】:

    在我的电脑上的输出结果为:

    JVM的最大内存量为:  259522560

    JVM的空闲内存量为:  15931440

    JVM的空闲内存量为:  13478600

    JVM的空闲内存量为:  16070800

    /**
     * @author Rollen-Holt 使用Runtime运行本机可运行程序
     */
    
    class hello{
    	public static void main(String[] args){
    		Runtime run = Runtime.getRuntime();
    		try{
    			run.exec("notepad.exe"); //打开记事本程序
    		}catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

      【运行结果】:

    会打开windows自带的记事本程序

    /**
     * @author Rollen-Holt 
     * 使用Runtime运行本机可运行程序
     * 并且使其5秒后自动关闭
     */
    
    class hello{
    	public static void main(String[] args){
    		Runtime run = Runtime.getRuntime();
    		Process pro=null;
    		
    		try{
    			pro=run.exec("notepad.exe"); //打开记事本程序
    		}catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    		try{
    			Thread.sleep(5000);
    		}catch (Exception e) {
    			e.printStackTrace();
    		}
    		pro.destroy();
    	}
    }
    
    /*
     * 此程序说明 exec的返回类型是Process类型
     * 如果要想使得进程消失,可以使用destroy方法
     */
    

      关于国际化: 

     

    首先建立一个名称为:Message.properties的文件

    然后在里面写入

    name=Rollen Holt

     

    之后再同一个目录下建立一个.java文件。代码为:

    import java.util.*;
    class hello{
    	public static void main(String[] args){
    		ResourceBundle res=ResourceBundle.getBundle("Message");
    		System.out.println(res.getString("name"));
    	}
    }
    

      输出结果为:Rollen Holt

     

    上面的例子有一个问题,如果在资源文件输入中文的话,会出现乱码,必须将中文转化为Unicode编码

    方法是:

    比如我的目录是:F:\我的文件\我的资料\我的学习资料\我的程序\java\hello\src

    下面有2个文件:

    我们想将1.properties中的中文内容转化为Unicode编码(目前里面的内容是:name=任文超)

    cmd然后在命令行下:

    首先定位到你的目录,对于笔者来说:

    然后:

    就会发现在目录下多了一个文件:

    里面的内容是:

    name=\u4efb\u6587\u8d85

    然后编写程序代码:

    import java.util.*;
    class hello{
    	public static void main(String[] args){
    		Locale zhLoc=new Locale("zh","CN");
    		ResourceBundle res=ResourceBundle.getBundle("Message",zhLoc);
    		System.out.println(res.getString("name"));
    	}
    }
    

      输出的结果自然是:任文超

     

    国际化之动态文本:

    name=你好,{0} 转化为unicode编码,其余同上

    然后编写代码:

    import java.util.*;
    import java.text.*;
    class hello{
    	public static void main(String[] args){
    		Locale zhLoc=new Locale("zh","CN");
    		ResourceBundle res=ResourceBundle.getBundle("Message",zhLoc);
    		String str=res.getString("name");
    		System.out.println(MessageFormat.format(str, "任文超"));
    	}
    }
    

      输出结果:你好,任文超!

     

    其实也可以使用类来替代资源文件的,但是在实际使用中很少使用类,此处就不说了,使用类是在太麻烦了。呵呵,留给有兴趣的朋友去干把。







  • 相关阅读:
    oracle循环语句
    解决使用Properties读取中文乱码问题
    oracle常用& to_date()怎么转换带am pm的时间格式
    distinct 多列详解
    javascript中遍历EL表达式List集合中的值
    最近一段时间代码汇总
    JAVA基础之对象的初始化
    求解圆圈中最后剩下的数字
    删除有序链表中的重复结点
    构造二叉树,并求解树的高度
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2148736.html
Copyright © 2011-2022 走看看