java代码中运行系统命令

//运行系统的命令
        Runtime rt = Runtime.getRuntime();
        Process pro = rt.exec("cmd /c native2ascii.exe -reverse C:\\temp\\to.txt > C:\\temp\\from.txt");

        pro.waitFor();


/** 
     * unicode转中文 
     * @param  str 要转的Unicode字符串
     * @return String 转化后的字符串
     * @throws IOException 
     * @throws InterruptedException 
     */  
    private String unicodeToChinese(String strs) throws IOException, InterruptedException
    {
        File file = new File("C:\\temp");
        if (!file.exists())
        {
            file.mkdir();
        }
        File fileFrom = new File(file,"from.txt");
        File toFrom = new File(file,"to.txt");
        if (!fileFrom.exists())
        {
            fileFrom.createNewFile();
        }
        if (!toFrom.exists())
        {
            toFrom.createNewFile();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(toFrom));
        bw.write(strs);
        
        bw.close();
        
        //运行系统的命令
        Runtime rt = Runtime.getRuntime();
        Process pro = rt.exec("cmd /c native2ascii.exe -reverse C:\\temp\\to.txt > C:\\temp\\from.txt");
        pro.waitFor();
        
        //toFrom.delete();
        
        BufferedReader br = new BufferedReader(new FileReader(fileFrom));
        StringBuffer sb = new StringBuffer();
        
        String readStr = br.readLine();
        while (null != readStr)
        {
            sb.append(readStr);
            sb.append("\n");
            
            readStr = br.readLine();
        }
        br.close();
        //System.out.println(sb.toString());
        
        //fileFrom.delete();
        
        return sb.toString();  
     }  


/** 
     * 中文转unicode 
     * @param str 
     * @return String 反回unicode编码 
     */  
    private String  chinaToUnicode(String str)
    {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < str.length(); i++)
        {  
            int chr1 = (char) str.charAt(i);
            //汉字范围 \u4e00-\u9fa5 (中文)
            if(chr1 >= 19968 && chr1 <= 171941)
            {
                result.append("\\u" + Integer.toHexString(chr1));
            }
            else
            {  
                result.append(str.charAt(i)); 
            }  
        }
        return result.toString();
    }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值