regex - What is the best way to copy file without blank characters in groovy -


i have exercise.i need copy file in other 1 erasing blank characters.i 've done following. has have better solution ?

class exercise4 {    static void main(string... args) {       exercise4 ex = new exercise4()       ex.copywithoutblank("c:\\users\\drieu\\tmp\\test.txt")    }     def copywithoutblank(string filepath) {        file file = new file(filepath)       file filewithoutblank = new file("c:\\users\\drieu\\tmp\\test2.txt")       printwriter printerwriter = new printwriter(filewithoutblank)        file.eachline { line ->         println "line:" + line         string linewithoutblank =  line.replaceall(" ", "")         println "copy line without blank :" + linewithoutblank + "  file:" + filewithoutblank.name         printerwriter.println(linewithoutblank)       }      printerwriter.close()          } } 

thanks in advance,

you can utilise withwriter method on file open writer, , close when done:

class exercise4 {     static main(args) {         exercise4 ex = new exercise4()         ex.copywithoutblank('/tmp/test.txt')     }      void copywithoutblank(string filepath) {         new file('/tmp/test2.txt').withwriter { w ->             new file(filepath).eachline { line ->                 w.writeline line.replaceall(' ', '')             }         }     } } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -