groovy常用模块

Posted 活到学到

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了groovy常用模块相关的知识,希望对你有一定的参考价值。

  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常用模块的主要内容,如果未能解决你的问题,请参考以下文章

nodejs常用代码片段

在 Groovy 中将字符串 XML 片段转换为文档节点

Python 常用模块学习

Groovy常用编程知识点简明教程

Gradle入门--Groovy常用语法

为什么groovy方法不打印该文件不存在?