====== Groovy File ====== * [[http://groovy.codehaus.org/Recipes+For+File|Groovy - Recipes For File]] * [[http://groovy.codehaus.org/groovy-jdk/java/io/File.html|File GDK]] * [[http://docs.oracle.com/javase/6/docs/api/java/io/File.html|File Java 6]] ===== 파일 이름 ===== * [[http://stackoverflow.com/questions/1569547/does-groovy-have-an-easy-way-to-get-a-filename-without-the-extension|파일 이름에서 확장자 빼고 이름만]] file.name.replaceFirst(~/\.[^\.]+$/, '') ===== 텍스트 파일 ===== def file = new File('filename.txt') def text = file.text // 전체 텍스트 // 각 줄을 읽어서 처리 file.eachLine { line -> println "# ${line}" } ===== 디렉토리 ===== * ''new File('.').directorySize()'' : 디렉토리 크기 구하기. Groovy 2.1 ==== 디렉토리의 파일 목록 필터링 ==== * [[http://docs.oracle.com/javase/6/docs/api/java/io/FileFilter.html|FileFilter]] 혹은 [[http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html|FilenameFilter]]를 사용한다. * 특정 확장자의 파일 목록을 구하는 예 def filesByExt(ext) { File dir = new File('.') dir.listFiles(new FilenameFilter() { public boolean accept(File f, String filename) { return filename.endsWith('.' + ext) } }) } List movies = filesbyExt('avi') * Groovy 스럽게 바꾸면 [[http://www.groovycode.com/file/filename_filter|groovycode.com : file : filter files with the filenamefilter]] def filesByExt(ext) { new File('.').listFiles({file, filename -> filename.endsWith('.' + ext)} as FilenameFilter) } ==== 현재 디렉토리의 동영상 파일을 E?? 에 따라 이름 줄이기 ==== 현재 디렉토리에 ''긴 제목의 동영상.E01.mp4'' 형태의 파일들이 있을 때 파일명을 ''E01.mp4'' 형태로 줄이기 new File('.').eachFile { it -> it.renameTo(it.name.find(/E[0-9]{2}/) + ".mp4") } ===== 참조 ===== * [[https://www.baeldung.com/groovy-io|Guide to I/O in Groovy]]