首页 > 其他 > 详细

groovy常用模块

时间:2021-05-15 12:31:50      阅读:16      评论:0      收藏:0      [点我收藏+]
  1. http请求

@Grab(group=‘org.codehaus.groovy.modules.http-builder‘, module=‘http-builder‘, version=‘0.5.0-RC2‘ )
import groovyx.net.http.HTTPBuilder

def urls = [
  "http://www.baidu.com",
  "http://www.163.com/" 
]

def up = urls.collect { url ->
  try {
    new HTTPBuilder( url ).get( path:‘‘ ) { response ->
      response.statusLine.statusCode == 200
    }
  }
  catch( e ) { false }
}

println up

  1. 文件读写
https://www.w3cschool.cn/groovy/groovy_file_io.html

import java.io.File 
class Example { 
   static void main(String[] args) { 
      new File(‘E:/‘,‘Example.txt‘).withWriter(‘utf-8‘) { 
         writer -> writer.writeLine ‘Hello World‘ 
      }  
   } 
}

  1. 检查主机是否ip
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class PingUtil {
    
    public static void main(String[] args) {
        String host1 = "14.215.178.37";
        String host2 = "www.baidu.com";
        ping(host1);
        ping(host2);
    }

    public static void ping(String host) {
        try {
            InetAddress inetAddress = InetAddress.getByName(host);
            boolean reachable = inetAddress.isReachable(5*1000);
            if(reachable) {
                System.out.println("ping success. Host name: " + inetAddress.getHostName() + ", IP addr: " + inetAddress.getHostAddress());
            }else {
                System.out.println("ping failed.");
            }
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
    }
}

groovy常用模块

原文:https://www.cnblogs.com/amize/p/14770755.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!