samedi 25 avril 2015

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


I have to do an exercise.I need to copy a file in an other one by erasing blank characters.I 've done the following. Has anyone have a 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 + " into  file:" + fileWithoutBlank.name
        printerWriter.println(lineWithoutBlank)

     }
     printerWriter.close()      
   }
}

Thanks in advance,


Aucun commentaire:

Enregistrer un commentaire