사용자 도구

사이트 도구


groovy:file

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
groovy:file [2013/01/27 14:53]
kwon37xi
groovy:file [2019/03/07 11:15] (현재)
kwon37xi
줄 2: 줄 2:
   * [[http://groovy.codehaus.org/Recipes+For+File|Groovy - Recipes For 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://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|파일 이름에서 확장자 빼고 이름만]]<code groovy>
 +file.name.replaceFirst(~/\.[^\.]+$/, '')
 +</code>
 ===== 텍스트 파일 ===== ===== 텍스트 파일 =====
 <code groovy> <code groovy>
줄 18: 줄 24:
   * ''new File('.').directorySize()'' : 디렉토리 크기 구하기. Groovy 2.1   * ''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]]를 사용한다.
 +  * 특정 확장자의 파일 목록을 구하는 예<code groovy>
 +
 +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')
 +</code>
 +  * Groovy 스럽게 바꾸면 [[http://www.groovycode.com/file/filename_filter|groovycode.com : file : filter files with the filenamefilter]] <code groovy>
 +def filesByExt(ext) {
 +    new File('.').listFiles({file, filename -> filename.endsWith('.' + ext)} as FilenameFilter)
 +}
 +</code>
 +==== 현재 디렉토리의 동영상 파일을 E?? 에 따라 이름 줄이기 ====
 +현재 디렉토리에 ''긴 제목의 동영상.E01.mp4'' 형태의 파일들이 있을 때 파일명을 ''E01.mp4'' 형태로 줄이기
 +<code groovy>
 +new File('.').eachFile  { it -> it.renameTo(it.name.find(/E[0-9]{2}/) + ".mp4") }
 +</code>
  
 +===== 참조 =====
 +  * [[https://www.baeldung.com/groovy-io|Guide to I/O in Groovy]]
groovy/file.1359266008.txt.gz · 마지막으로 수정됨: 2013/01/27 14:53 저자 kwon37xi