題目:java
假設有文件夾SRC存有編譯以前的源文件, 文件夾DIST存有編譯以後的二進制文件.express
編譯前和編譯後的文件名相同, 但擴展名不一樣.apache
若是編譯失敗, 在DIST中就不會生成對應的二進制文件.編程
好比, AAA.fmb 編譯以後是 AAA.fmx,maven
又如, BBB.rdf 編譯以後是 BBB.rep,code
又如, CCC.plx 編譯以後是 CCC.pll.regexp
如今的問題是, 在經過 ANT 腳本進行編譯以後, DIST中的文件數 少於 SRC中的文件數.xml
請經過編程的方式對比SRC和DIST文件夾, 找出編譯失敗的文件.rem
解答 by Groovy:get
def dirA = new File("G:\\CODE\\TEST\\A").listFiles(); def dirB = new File("G:\\CODE\\TEST\\B").listFiles(); def listA = new ArrayList() def listB = new ArrayList() dirA.each{it-> listA.add(it.name.replaceFirst(~/\.[^\.]+$/, '')) } dirB.each{it-> listB.add(it.name.replaceFirst(~/\.[^\.]+$/, '')) } //println listA //println listB listA.each{it-> if(!listB.contains(it)){ println(it) } }
如何用JAVA取得不帶擴展名的文件名?
//The easiest way is to use a regular expression. fileNameWithOutExt = "test.xml".replaceFirst("[.][^.]+$", ""); //The above expression will remove the last dot followed by one or more characters. //Here's a basic unit test. public void testRegex() { assertEquals("test", "test.xml".replaceFirst("[.][^.]+$", "")); assertEquals("test.2", "test.2.xml".replaceFirst("[.][^.]+$", "")); }
如何用GROOVY取得不帶擴展名的文件名?
//I believe the grooviest way would be: file.name.lastIndexOf('.').with {it != -1 ? file.name[0..<it] : file.name} //or with a simple regexp: file.name.replaceFirst(~/\.[^\.]+$/, '') //also there's an apache commons-io java lib for that kinda purposes, //which you could easily depend on if you use maven: org.apache.commons.io.FilenameUtils.getBaseName(file.name)