groovy向java所提供的File
类,新增了几个方便的方法。分别是:eachLine和.text。
package file
class FileToConsole {
static main(args) { println("====将文件中的内容写入到控制台"); File file = new File("D:/tutorial/15groovy/code/GroovyTutorial/src/file/myfile.txt"); file.eachLine {line -> println line; };
println ("在每一行的前边加上一个行号,然后打印到控制台"); def lineNumber = 0; file = new File("D:/tutorial/15groovy/code/GroovyTutorial/src/file/myfile.txt"); file.eachLine {line -> lineNumber++; println ("$lineNumber:$line"); }; println ("读文件内容并写入到String"); String s = new File("D:/tutorial/15groovy/code/GroovyTutorial/src/file/myfile.txt").text; println s; }
}
|
输出
====将文件中的内容写入到控制台 hello world hello zuoys! 在每一行的前边加上一个行号,然后打印到控制台 1:hello world 2:hello zuoys! 读文件内容并写入到String hello world hello zuoys!
|
File
对象提供了几个方法,如 eachFile
, eachDir
和earchFileRecursively
,闭包使用。
写入文件,并且向文件中追加内容,以及从磁盘中删除文件。
package file
class WriteAndAppendToFile { static def filePath = "D:/tutorial/15groovy/code/GroovyTutorial/src/file/output.txt"; static main(args) { File file = new File(filePath); file.write("Hello\n"); file.append("Testing\n"); file << "More appending...\n"; File result = new File(filePath); println (result.text); //从磁盘中删除该文件 file.delete(); }
}
|
输出
Hello Testing More appending...
|
抓取http远端页面,类似读取一个文本文件。
package file
class ReadHttpPage {
static main(args) { String url = "http://www.cnblogs.com/yaoyuan2/"; //法1 def data = new URL(url).text; //法2:使用groovy JDK方法 //data = url.toURL().text; println (data); }
}
|
输出:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>遥远2 - 博客园</title> <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=umTYGJ1Xbo61obX_Gqsil3krUSBXOilTLax6nesdN401"/> <link id="MainCss" type="text/css" rel="stylesheet" href="/skins/SimpleClear/bundle-SimpleClear.css?v=EVOKy_9d-0OzsDt7Iaw0OHSiRqKJoQVRFQeK_xCGZ6c1"/> <link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/yaoyuan2/rss"/> <link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/yaoyuan2/rsd.xml"/> <link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/yaoyuan2/wlwmanifest.xml"/> <script src="//common.cnblogs.com/script/jquery.js" type="text/javascript"></script> <script type="text/javascript">var currentBlogApp = ‘yaoyuan2‘, cb_enable_mathjax=false;</script> <script src="/bundles/blog-common.js?v=5U27mPu5I0KkEyh-j235bo21EbCO9CoRnrVjj5DiOjI1" type="text/javascript"></script> </head> <body> <a name="top"></a>
<div id="main"> <div id="header"> <h1><a id="Header1_HeaderTitle" href="http://www.cnblogs.com/yaoyuan2/">遥远2</a></h1> <p id="tagline"></p></div>
</div> <div id="rightmenu">
<div id="my_links"> <h3>导航</h3> <ul> <li><a id="MyLinks1_HomeLink" href="http://www.cnblogs.com/">博客园</a></li> <li><a id="MyLinks1_MyHomeLink" href="http://www.cnblogs.com/yaoyuan2/">首页</a></li> <!--<li><a id="MyLinks1_NewPostLink" rel="nofollow" href="http://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>--> <li><a id="MyLinks1_ContactLink" rel="nofollow" href="http://msg.cnblogs.com/send/%E9%81%A5%E8%BF%9C2">联系</a></li> <li><a id="MyLinks1_Syndication" href="http://www.cnblogs.com/yaoyuan2/rss">订阅</a></li><!--<a id="MyLinks1_XMLLink" href="http://www.cnblogs.com/yaoyuan2/rss"><img src="http://www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--> <li><a id="MyLinks1_Admin" rel="nofollow" href="http://i.cnblogs.com/">管理</a></li> </ul> </div> <div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script>
<h3>公告</h3> <div class="newsItem"> <div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script> </div>
<div id="blog_stats"> <h3>统计</h3> <ul> <li>随笔 - 0 <li>文章 - 0 <li>评论 - 0 <!--<li>引用 - 0--> </li> </ul> </div> <div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
<div id="footer"> Powered by: <br />
<a id="Footer1_Hyperlink3" NAME="Hyperlink1" href="http://www.cnblogs.com/"><font face="Verdana">博客园</font></a> <br /> Copyright © 遥远2 </div>
</div>
</body> </html>
|
原文:http://www.cnblogs.com/yaoyuan2/p/5719200.html