程式扎記: [ GroovyGN ] Working with Files

標籤

2015年8月13日 星期四

[ GroovyGN ] Working with Files

Source From Here 
Preface 
When we write code in Java to work with files we must write a lot of boilerplate code to make sure all streams are opened and closed correctly and provide exception handling. The Commons IO package already helps, but Groovy makes working with files so easy. Groovy adds a lot of useful methods to thejava.io.File class. We can use simple properties to write and read text, methods to traverse the file system and methods to filter contents. 

Sample Code 
Here is a Groovy script with different samples of working with files: 
  1. // Normal way of creating file objects.  
  2. def file1 = new File('groovy1.txt')  
  3. def file2 = new File('groovy2.txt')  
  4. def file3 = new File('groovy3.txt')  
  5.   
  6. // Writing to the files with the write method:  
  7. file1.write 'Working with files the Groovy way is easy.\n'  
  8.   
  9. // Using the leftShift operator:  
  10. file1 << 'See how easy it is to add text to a file.\n'  
  11.   
  12. // Using the text property:  
  13. file2.text = '''We can even use the text property of  
  14. a file to set a complete block of text at once.'''  
  15.   
  16. // Or a writer object:  
  17. file3.withWriter('UTF-8') { writer ->  
  18.     writer.write('We can also use writers to add contents.')  
  19. }  
  20.   
  21. // Reading contents of files to an array:  
  22. def lines = file1.readLines()  
  23. assert 2 == lines.size()  
  24. assert 'Working with files the Groovy way is easy.' == lines[0]  
  25.   
  26. // Or we read with the text property:  
  27. assert 'We can also use writers to add contents.' == file3.text  
  28.   
  29. // Or with a reader:  
  30. count = 0  
  31. file2.withReader { reader ->   
  32.     while (line = reader.readLine()) {  
  33.         switch (count) {  
  34.             case 0:   
  35.                 assert 'We can even use the text property of' == line  
  36.                 break  
  37.             case 1:  
  38.                 assert 'a file to set a complete block of text at once.' == line  
  39.                 break  
  40.         }  
  41.         count++  
  42.     }  
  43. }  
  44.   
  45. // We can also read contents with a filter:  
  46. sw = new StringWriter()  
  47. file1.filterLine(sw) { it =~ /Groovy/ }  
  48. assert 'Working with files the Groovy way is easy.\r\n' == sw.toString()  
  49.   
  50. // We can look for files in the directory with different methods.  
  51. // See for a complete list the File GDK documentation.  
  52. files = []  
  53. new File('.').eachFileMatch(~/^groovy.*\.txt$/) { files << it.name }  
  54. assert ['groovy1.txt''groovy2.txt', groovy3.txt'] == files  
  55.   
  56. // Delete all files:  
  57. files.each { new File(it).delete() }  
Supplement 
Working with the GDK - Working with files and I/O 
The Simple Groovy datatypes - Working with regular expressions

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!