java运行命令和String输出到文本

 

    //java中运行“命令”	
    private static String runCommand(String calarmServiceUrl) {
		StringBuffer output = new StringBuffer();
	    try {     
	        //Process process = Runtime.getRuntime().exec("curl -X GET http://calm.default.svc.cluster.local:8182/api/alma/alarms?sourceObject="+ParameterUitls.getSDN()+" | sed 's/@//g'");  
	    	//Process process = Runtime.getRuntime().exec("curl -X GET http://calm.default.svc.cluster.local:8182/api/alma/alarms | sed 's/@//g'");  
	        //Process process = Runtime.getRuntime().exec("curl -X GET "+calarmServiceUrl+"?sourceObject="+ParameterUitls.getSDN()+" | sed 's/@//g'");  
	        Process process = Runtime.getRuntime().exec("curl -X GET "+calarmServiceUrl+"?sourceObject="+ParameterUitls.getSDN());  
	        InputStream input = process.getInputStream();
	        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
	        String szline;
	        while ((szline = reader.readLine())!= null) {
	            output.append(szline + "\n");
	        }
	        
	        reader.close();
	        process.waitFor();
	        process.destroy();
	    } catch (Exception e) {  
	    	LOGGER.warn(e);
	        return null;
	    }
	    return output.toString().replace("@","");
	
	}

	//String输出到alarm.json文件
	public static void outFile(String s) {
		if(s!=null) {
			String path = AlarmManager.class.getResource("/").toString() + "alarm.json";
			if (path.contains(":")) {
				path = path.replace("file:/", "");
			}
			File file = new File(path);
			try (FileOutputStream fop = new FileOutputStream(file)) {
				// if file doesn't exists, then create it
				if (!file.exists()) {
					file.createNewFile();
				}
				// get the content in bytes
				byte[] contentInBytes = s.getBytes();
	
				fop.write(contentInBytes);
				fop.flush();
				fop.close();
				//System.out.println("Done");
			} catch (IOException e) {
				LOGGER.warn(e);
			}
		}else {
	    	LOGGER.warn("fail to output file ");
		}
	}
	

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值