프로그래밍/Groovy
Scanner 클래스를 이용한 웹 텍스트 문서 읽기 Groovy 예제
Scripter
2008. 3. 31. 18:25
다음은 JDK 1.5 부터 등장한 java.util.Scanner 클래스를 이용하여 웹문서를 통째로 읽어들이는 Groovy 예제 소스 코드이다. 실행 예는
groovy readWebText.groovy http://groovy.codehaus.org
이다.
(침고 자료: http://kr.sun.com/developers/techtips/c2004_1201.html#1 )
/*
* Filename: readWebText.groovy
*/
def readFile(String filename) {
def connection = new URL(filename).openConnection()
def scanner = new Scanner(connection.getInputStream()).useDelimiter("\\Z")
String text = scanner.next()
println text
scanner.close()
}
if (args.length != 1) {
println("Usage: groovy readWebText.groovy [url address]")
System.exit(0)
}
readFile(args[0])