001 /* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016 package org.opengion.fukurou.util; 017 018 import java.io.BufferedInputStream; 019 import java.io.BufferedOutputStream; 020 import java.io.BufferedReader; 021 import java.io.BufferedWriter; 022 import java.io.File; 023 import java.io.InputStream; 024 import java.io.FileInputStream; 025 import java.io.FileNotFoundException; 026 import java.io.FileOutputStream; 027 import java.io.IOException; 028 import java.io.InputStreamReader; 029 import java.io.OutputStream; 030 import java.io.OutputStreamWriter; 031 import java.io.PrintWriter; 032 import java.io.UnsupportedEncodingException; 033 import java.io.Writer; 034 import java.util.Collections; 035 import java.util.List; 036 037 // import java.nio.ByteBuffer; 038 import java.nio.channels.FileChannel; 039 040 /** 041 * FileUtil.java ã¯ã€å?通的ã«ä½¿ç”¨ã•れã‚?File関連メソãƒ?ƒ‰ã‚’集ç´?—ãŸã?クラスã§ã™ã? 042 * 043 * 全変数ã¯ã€public static final 宣è¨?•れã¦ãŠã‚Šã€å?メソãƒ?ƒ‰ã¯ã€public static synchronized 宣è¨?•れã¦ã?¾ã™ã? 044 * 045 * @og.group ユーãƒ?‚£ãƒªãƒ?‚£ 046 * 047 * @version 4.0 048 * @author Kazuhiko Hasegawa 049 * @since JDK5.0, 050 */ 051 public final class FileUtil { 052 private static final NonClosePrintWriter outWriter = new NonClosePrintWriter( System.out ); 053 private static final NonClosePrintWriter errWriter = new NonClosePrintWriter( System.err ); 054 055 /** 056 * ã™ã¹ã¦ã?staticメソãƒ?ƒ‰ãªã®ã§ã€ã‚³ãƒ³ã‚¹ãƒˆãƒ©ã‚¯ã‚¿ã‚’呼ã³å‡ºã•ãªãã—ã¦ãŠãã¾ã™ã? 057 * 058 */ 059 private FileUtil() {} 060 061 /** シスãƒ?ƒ ä¾å˜ã?改行記å·ã‚’ã‚»ãƒ?ƒˆã—ã¾ã™ã? */ 062 private static final String CR = System.getProperty("line.separator"); 063 064 /** 5.6.1.2 (2013/02/22) UNIXç³»ã®ãƒ•ァイルåを表ã™ã‚»ãƒ‘レータæ–??*/ 065 private static final char UNIX_SEPARATOR = '/'; 066 067 /** 5.6.1.2 (2013/02/22) Windwosç³»ã®ãƒ•ァイルåを表ã™ã‚»ãƒ‘レータæ–??*/ 068 private static final char WINDOWS_SEPARATOR = '\\'; 069 070 /** 5.6.1.2 (2013/02/22) ãƒ•ã‚¡ã‚¤ãƒ«ã®æ‹¡å¼µåã?区åˆ?‚Šã‚’è¡¨ã™æ–‡å?*/ 071 public static final char EXTENSION_SEPARATOR = '.'; 072 073 /** 074 * Fileオブジェクトã¨ã‚¨ãƒ³ã‚³ãƒ¼ãƒ‰ã‚ˆã‚?PrintWriterオブジェクトを作æ?ã—ã¾ã™ã? 075 * 076 * @param file 出力ã™ã‚‹ãƒ•ァイルオブジェクãƒ? 077 * @param encode ファイルã®ã‚¨ãƒ³ã‚³ãƒ¼ãƒ? 078 * 079 * @return PrintWriterオブジェクãƒ? 080 * @throws RuntimeException 何らã‹ã?エラーãŒç™ºç”Ÿã—ãŸå?å? 081 */ 082 public static PrintWriter getPrintWriter( final File file,final String encode ) { 083 return getPrintWriter( file,encode,false ); 084 } 085 086 /** 087 * Fileオブジェクトã¨ã‚¨ãƒ³ã‚³ãƒ¼ãƒ‰ã‚ˆã‚?PrintWriterオブジェクトを作æ?ã—ã¾ã™ã? 088 * 089 * @param file 出力ã™ã‚‹ãƒ•ァイルオブジェクãƒ? 090 * @param encode ファイルã®ã‚¨ãƒ³ã‚³ãƒ¼ãƒ? 091 * @param append ファイルを追åŠ?ƒ¢ãƒ¼ãƒ?true)ã«ã™ã‚‹ã‹ã©ã?‹ 092 * 093 * @return PrintWriterオブジェクãƒ? 094 * @throws RuntimeException 何らã‹ã?エラーãŒç™ºç”Ÿã—ãŸå?å? 095 */ 096 public static PrintWriter getPrintWriter( final File file,final String encode,final boolean append ) { 097 final PrintWriter writer ; 098 099 try { 100 writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter( 101 new FileOutputStream(file,append) ,encode ))); 102 } 103 catch( UnsupportedEncodingException ex ) { 104 String errMsg = "æŒ?®šã•れãŸã‚¨ãƒ³ã‚³ãƒ¼ãƒ?‚£ãƒ³ã‚°ãŒã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€? + CR 105 + ex.getMessage() + CR 106 + "File=[" + file + " , encode=[" + encode + "]" ; 107 throw new RuntimeException( errMsg,ex ); 108 } 109 catch( FileNotFoundException ex ) { // 3.6.1.0 (2005/01/05) 110 String errMsg = "ファイルåãŒã‚ªãƒ¼ãƒ—ン出æ¥ã¾ã›ã‚“ã§ã—ãŸã€? + CR 111 + ex.getMessage() + CR 112 + "File=[" + file + " , encode=[" + encode + "]" ; 113 throw new RuntimeException( errMsg,ex ); 114 } 115 116 return writer ; 117 } 118 119 /** 120 * ファイルåよりã?PrintWriterオブジェクトを作æ?ã™ã‚‹ç°¡æ˜“メソãƒ?ƒ‰ã§ã™ã? 121 * 122 * ã“れã¯ã€ãƒ•ァイルåã?ã€ãƒ•ルパスã§ã€è¿½åŠ?ƒ¢ãƒ¼ãƒ‰ã§ã€UTF-8 エンコードã? 123 * ãƒã‚°ãƒ•ァイルをå?力ã™ã‚‹å?åˆã«ä½¿ç”¨ã—ã¾ã™ã? 124 * ã¾ãŸã?ファイルåã«ã€?System.out" ã¨ã€?System.err" を指定ã§ãã¾ã™ã? 125 * ãã?å ´åˆã?ã€æ¨™æº–å?力ã?ã¾ãŸã?ã€æ¨™æº–エラー出力ã«å‡ºåŠ›ã•れã¾ã™ã? 126 * "System.out" ã¨ã€?System.err" を指定ã—ãŸå?åˆã?ã€NonClosePrintWriter 127 * オブジェクトãŒè¿”ã•れã¾ã™ã?ã“れã¯ã€close() 処ç?Œå‘¼ã°ã‚Œã¦ã‚‚ã?何もã—ãªã? 128 * クラスã§ã™ã?ã¾ãŸã?常ã«å†?ƒ¨ã‚ャãƒ?‚·ãƒ¥ã®åŒã˜ã‚ªãƒ–ジェクトãŒè¿”ã•れã¾ã™ã? 129 * 130 * @param file 出力ã™ã‚‹ãƒ•ァイルå? 131 * 132 * @return PrintWriterオブジェクãƒ? 133 * @throws RuntimeException 何らã‹ã?エラーãŒç™ºç”Ÿã—ãŸå?å? 134 * @throws IllegalArgumentException ファイルå㌠null ã®å ´å? 135 */ 136 public static PrintWriter getLogWriter( final String file ) { 137 if( file == null ) { 138 String errMsg = "ファイルåã«ã€null ã¯æŒ?®šã§ãã¾ã›ã‚“ã€?; 139 throw new IllegalArgumentException( errMsg ); 140 } 141 142 final PrintWriter writer ; 143 if( "System.out".equalsIgnoreCase( file ) ) { 144 writer = outWriter ; 145 } 146 else if( "System.err".equalsIgnoreCase( file ) ) { 147 writer = errWriter ; 148 } 149 else { 150 writer = getPrintWriter( new File( file ),"UTF-8",true ); 151 } 152 153 return writer ; 154 } 155 156 /** 157 * OutputStreamã¨ã‚¨ãƒ³ã‚³ãƒ¼ãƒ‰ã‚ˆã‚?PrintWriterオブジェクトを作æ?ã—ã¾ã™ã? 158 * 159 * @og.rev 5.5.2.0 (2012/05/01) æ–°è¦è¿½åŠ? 160 * 161 * @param os 利用ã™ã‚‹OutputStream 162 * @param encode ファイルã®ã‚¨ãƒ³ã‚³ãƒ¼ãƒ? 163 * 164 * @return PrintWriterオブジェクãƒ? 165 * @throws RuntimeException 何らã‹ã?エラーãŒç™ºç”Ÿã—ãŸå?å? 166 */ 167 public static PrintWriter getPrintWriter( final OutputStream os,final String encode ) { 168 final PrintWriter writer ; 169 170 try { 171 writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter( 172 os ,encode ))); 173 } 174 catch( UnsupportedEncodingException ex ) { 175 String errMsg = "æŒ?®šã•れãŸã‚¨ãƒ³ã‚³ãƒ¼ãƒ?‚£ãƒ³ã‚°ãŒã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€? + CR 176 + ex.getMessage() + CR 177 + "encode=[" + encode + "]" ; 178 throw new RuntimeException( errMsg,ex ); 179 } 180 return writer ; 181 } 182 183 /** 184 * PrintWriter を継承ã—ãŸã€JspWriterãªã©ã® Writer 用ã®ã‚¯ãƒ©ã‚¹ã‚’定義ã—ã¾ã™ã? 185 * 186 * 例ãˆã°ã€JspWriterãªã©ã® JSP/Servletç‰ã?フレーãƒ?ƒ¯ãƒ¼ã‚¯ã§ä½¿ç”¨ã•れã‚? 187 * Writer ã§ã¯ã€flush ã‚?close 処ç??ã€ãƒ•レーãƒ?ƒ¯ãƒ¼ã‚¯å†?§è¡Œã‚れã¾ã™ã? 188 * ãã?å ´åˆã?通常ã®ãƒ•ァイルã¨åŒã˜ç”¨ã«ã€flush ã‚?close をアプリケーションå´ã§ 189 * 行ã†ã¨ã€å?部処ç?š„ã«ä¸æ•´åˆãŒç™ºç”Ÿã—ãŸã‚Šã€æœ€æ‚ªã®å ´åˆã‚¨ãƒ©ãƒ¼ã«ãªã‚Šã¾ã™ã? 190 * ã“ã?クラスã¯ã€NonFlushPrintWriter クラスã®ã‚ªãƒ–ジェクトを返ã—ã¾ã™ã? 191 * ã“れã¯ã€??常ã®ã€new PrintWriter( Writer ) ã§ã€æ±‚ã‚ã‚‹ã?ã¨ã€ã»ã¨ã‚“ã©åŒæ§˜ã? 192 * 処ç?‚’行ã„ã¾ã™ãŒã€close() 㨠flush() メソãƒ?ƒ‰ãŒå‘¼ã°ã‚Œã¦ã‚‚ã?何もã—ã¾ã›ã‚“ã€? 193 * 194 * @param writer 出力ã™ã‚‹Writeオブジェクãƒ?NonFlushPrintWriterクラス) 195 * 196 * @return PrintWriterオブジェクãƒ? 197 */ 198 public static PrintWriter getNonFlushPrintWriter( final Writer writer ) { 199 return new NonFlushPrintWriter( writer ); 200 } 201 202 /** 203 * Fileオブジェクトã¨ã‚¨ãƒ³ã‚³ãƒ¼ãƒ‰ã‚ˆã‚?BufferedReaderオブジェクトを作æ?ã—ã¾ã™ã? 204 * 205 * @param file 入力ã™ã‚‹ãƒ•ァイルオブジェクãƒ? 206 * @param encode ファイルã®ã‚¨ãƒ³ã‚³ãƒ¼ãƒ? 207 * 208 * @return BufferedReaderオブジェクãƒ? 209 * @throws RuntimeException 何らã‹ã?エラーãŒç™ºç”Ÿã—ãŸå?å? 210 */ 211 public static BufferedReader getBufferedReader( final File file,final String encode ) { 212 final BufferedReader reader ; 213 214 try { 215 reader = new BufferedReader(new InputStreamReader( 216 new FileInputStream( file ) ,encode )); 217 } 218 catch( UnsupportedEncodingException ex ) { 219 String errMsg = "æŒ?®šã•れãŸã‚¨ãƒ³ã‚³ãƒ¼ãƒ?‚£ãƒ³ã‚°ãŒã‚µãƒã?トã•れã¦ã?¾ã›ã‚“ã€? + CR 220 + ex.getMessage() + CR 221 + "FIle=[" + file + " , encode=[" + encode + "]" ; 222 throw new RuntimeException( errMsg,ex ); 223 } 224 catch( FileNotFoundException ex ) { 225 String errMsg = "ファイルåãŒã‚ªãƒ¼ãƒ—ン出æ¥ã¾ã›ã‚“ã§ã—ãŸã€? + CR 226 + ex.getMessage() + CR 227 + "FIle=[" + file + " , encode=[" + encode + "]" ; 228 throw new RuntimeException( errMsg,ex ); 229 } 230 231 return reader ; 232 } 233 234 /** 235 * æŒ?®šã?ファイルåãŒã€å®Ÿéš›ã«å˜åœ¨ã—ã¦ã?‚‹ã‹ã©ã?‹ã‚’ãƒã‚§ãƒ?‚¯ã—ã¾ã™ã? 236 * å˜åœ¨ã—ãªã??åˆã?ã€?¼’秒毎ã«ã€?¼“回確èªã—ã¾ã™ã? 237 * ãれã§ã‚‚å˜åœ¨ã—ãªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼ã‚’è¿”ã—ã¾ã™ã? 238 * return ã•れるFileオブジェクトã?ã€æ£è¦ã?å½¢å¼?CanonicalFile)ã§ã™ã? 239 * 240 * @param dir フォルãƒ? 241 * @param filename ファイルå? 242 * 243 * @return å˜åœ¨ãƒã‚§ãƒ?‚¯(ãªã‘れ㰠null/ã‚れã°ã€CanonicalFile) 244 */ 245 public static File checkFile( final String dir, final String filename ) { 246 return checkFile( dir,filename,3 ); 247 } 248 249 /** 250 * æŒ?®šã?ファイルåãŒã€å®Ÿéš›ã«å˜åœ¨ã—ã¦ã?‚‹ã‹ã©ã?‹ã‚’ãƒã‚§ãƒ?‚¯ã—ã¾ã™ã? 251 * å˜åœ¨ã—ãªã??åˆã?ã€?¼’秒毎ã«ã€æŒ‡å®šã?回数åˆ?¢ºèªã—ã¾ã™ã? 252 * ãれã§ã‚‚å˜åœ¨ã—ãªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼ã‚’è¿”ã—ã¾ã™ã? 253 * return ã•れるFileオブジェクトã?ã€æ£è¦ã?å½¢å¼?CanonicalFile)ã§ã™ã? 254 * 255 * @param dir フォルãƒ? 256 * @param filename ファイルå? 257 * @param count 回数æŒ?®? 258 * 259 * @return å˜åœ¨ãƒã‚§ãƒ?‚¯(ãªã‘れ㰠null/ã‚れã°ã€CanonicalFile) 260 */ 261 public static File checkFile( final String dir, final String filename,final int count ) { 262 File file = null; 263 264 int cnt = count; 265 while( cnt > 0 ) { 266 file = new File( dir,filename ); 267 if( file.exists() ) { break; } 268 else { 269 if( cnt == 1 ) { return null; } // 残り?‘回ã®å ´åˆã?ã€?¼’ç§’å¾?©Ÿã›ãšã«å³æŠœã‘ã‚‹ã? 270 try { Thread.sleep( 2000 ); } // ?’ç§’å¾?©? 271 catch ( InterruptedException ex ) { 272 System.out.println( "InterruptedException" ); 273 } 274 System.out.println(); 275 System.out.print( "CHECK File Error! CNT=" + cnt ); 276 System.out.print( " File=" + file.getAbsolutePath() ); 277 } 278 cnt--; 279 } 280 281 // ãƒ•ã‚¡ã‚¤ãƒ«ã®æ£å¼ãƒ‘スåã?å–å¾? 282 try { 283 return file.getCanonicalFile() ; 284 } 285 catch( IOException ex ) { 286 String errMsg = "ãƒ•ã‚¡ã‚¤ãƒ«ã®æ£å¼ãƒ‘スåãŒå–å¾—ã§ãã¾ã›ã‚“ã€?" + file.getAbsolutePath() + "]"; 287 throw new RuntimeException( errMsg,ex ); 288 } 289 } 290 291 /** 292 * ファイルã®ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?を行ã„ã¾ã™ã? 293 * 294 * copy( File,File,false ) を呼ã³å‡ºã—ã¾ã™ã? 295 * 296 * @og.rev 5.1.6.0 (2010/05/01) 戻りå?ã«ã€true/false æŒ?®šã—ã¾ã™ã? 297 * 298 * @param fromFile コピã?å…?ƒ•ァイルå? 299 * @param toFile コピã?先ファイルå? 300 * 301 * @return ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?ãŒæ£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 302 * @see #copy( File,File,boolean ) 303 */ 304 public static boolean copy( final String fromFile,final String toFile ) { 305 return copy( new File( fromFile ), new File( toFile ), false ); 306 } 307 308 /** 309 * ファイルã®ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?を行ã„ã¾ã™ã? 310 * 311 * copy( File,File,boolean ) を呼ã³å‡ºã—ã¾ã™ã? 312 * 第?“引数ã®ã€keepTimeStamp=true ã§ã€ã‚³ãƒ”ã?å…??ファイルã®ã‚¿ã‚¤ãƒ?‚¹ã‚¿ãƒ³ãƒ—ã‚’ã€? 313 * コピã?å…ˆã«ã‚‚ã‚»ãƒ?ƒˆã—ã¾ã™ã? 314 * 315 * @og.rev 5.1.6.0 (2010/05/01) 戻りå?ã«ã€true/false æŒ?®šã—ã¾ã™ã? 316 * 317 * @param fromFile コピã?å…?ƒ•ァイルå? 318 * @param toFile コピã?先ファイルå? 319 * @param keepTimeStamp タイãƒ?‚¹ã‚¿ãƒ³ãƒ—ç¶æŒ[true/false] 320 * 321 * @return ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?ãŒæ£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 322 * @see #copy( File,File,boolean ) 323 */ 324 // public static boolean copy( final String fromFile,final String toFile,final boolean changeCrLf ) { 325 public static boolean copy( final String fromFile,final String toFile,final boolean keepTimeStamp ) { 326 return copy( new File( fromFile ), new File( toFile ), keepTimeStamp ); 327 } 328 329 /** 330 * ファイルã®ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?を行ã„ã¾ã™ã? 331 * 332 * copy( File,File,false ) を呼ã³å‡ºã—ã¾ã™ã? 333 * 334 * @og.rev 5.1.6.0 (2010/05/01) 戻りå?ã«ã€true/false æŒ?®šã—ã¾ã™ã? 335 * 336 * @param fromFile コピã?å…?ƒ•ァイル 337 * @param toFile コピã?先ファイル 338 * 339 * @return ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?ãŒæ£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 340 * @see #copy( File,File,boolean ) 341 */ 342 public static boolean copy( final File fromFile,final File toFile ) { 343 return copy( fromFile, toFile, false ); 344 } 345 346 /** 347 * ファイルã®ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?を行ã„ã¾ã™ã? 348 * 349 * 第?“引数ã®ã€keepTimeStamp=true ã§ã€ã‚³ãƒ”ã?å…??ファイルã®ã‚¿ã‚¤ãƒ?‚¹ã‚¿ãƒ³ãƒ—ã‚’ã€? 350 * コピã?å…ˆã«ã‚‚ã‚»ãƒ?ƒˆã—ã¾ã™ã? 351 * toFile ãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€fromFile ã®ãƒ•ァイルåã‚’ãã?ã¾ã¾ã‚³ãƒ”ã?ã—ã¾ã™ã? 352 * fromFile ãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ã‚¨ãƒ©ãƒ¼ã«ã—ã¾ã™ã? 353 * copyDirectry( File,Fileboolean )を使用ã—ã¦ãã ã•ã„ã€?自動å?ç??ã—ã¦ã?¾ã›ã‚“) 354 * 355 * @og.rev 5.1.6.0 (2010/05/01) æ–°è¦è¿½åŠ? 356 * @og.rev 5.6.5.2 (2013/06/21) ByteBufferを利用ã—ãŸæ–¹å¼ã‹ã‚‰ã?transferTo を使用ã™ã‚‹æ–¹å¼ã«å¤‰æ›´ 357 * @og.rev 5.7.1.2 (2013/12/20) copyå…?toFile)ã®ãƒ•ォルãƒ?Œå˜åœ¨ã—ãªã‘れã°ã€ä½œæ?ã—ã¾ã™ã? 358 * 359 * @param fromFile コピã?å…?ƒ•ァイル 360 * @param toFile コピã?先ファイル 361 * @param keepTimeStamp タイãƒ?‚¹ã‚¿ãƒ³ãƒ—ç¶æŒ[true/false] 362 * 363 * @return ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?ãŒæ£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 364 * @see #copyDirectry( File,File,boolean ) 365 */ 366 public static boolean copy( final File fromFile,final File toFile,final boolean keepTimeStamp ) { 367 FileInputStream inFile = null; 368 FileOutputStream outFile = null; 369 FileChannel fin = null; 370 FileChannel fout = null; 371 372 File tempToFile = toFile ; 373 try { 374 // fromFileãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ã‚¨ãƒ©ãƒ¼ 375 if( fromFile.isDirectory() ) { 376 System.err.println( fromFile + " ãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã®ãŸã‚ã€å?ç?§ãã¾ã›ã‚“ã€? ); 377 return false; 378 } 379 // toFileãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ãã®ãƒ‘スã§ãƒ•ァイルåã‚’fromFileã‹ã‚‰å–り出ã™ã? 380 if( toFile.isDirectory() ) { 381 tempToFile = new File( toFile,fromFile.getName() ); 382 } 383 384 // 5.7.1.2 (2013/12/20) copyå…?toFile)ã®ãƒ•ォルãƒ?Œå˜åœ¨ã—ãªã‘れã°ã€ä½œæ?ã—ã¾ã™ã? 385 File parent = tempToFile.getParentFile(); 386 if( !parent.exists() && !parent.mkdirs() ) { 387 // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹ 388 System.err.println( parent + " ã® ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªä½œæ?ã«å¤±æ•—ã—ã¾ã—ãŸã€? ); 389 return false; 390 } 391 392 inFile = new FileInputStream( fromFile ); 393 outFile = new FileOutputStream( tempToFile ); 394 395 fin = inFile.getChannel(); 396 fout = outFile.getChannel(); 397 398 // 5.6.5.2 (2013/06/21) ByteBufferを利用ã—ãŸæ–¹å¼ã‹ã‚‰ã?transferTo を使用ã™ã‚‹æ–¹å¼ã«å¤‰æ›´ 399 // ByteBuffer buffer = ByteBuffer.allocateDirect( BUFSIZE ); 400 // while ( (fin.read(buffer) != -1) || buffer.position() > 0) { 401 // buffer.flip(); 402 // fout.write( buffer ); 403 // buffer.compact(); 404 // } 405 406 fin.transferTo(0, fin.size(), fout ); 407 408 } 409 catch ( IOException ex ) { 410 System.out.println(ex.getMessage()); 411 return false; 412 } 413 finally { 414 Closer.ioClose( inFile ) ; 415 Closer.ioClose( outFile ); 416 Closer.ioClose( fin ) ; 417 Closer.ioClose( fout ); 418 } 419 420 if( keepTimeStamp ) { 421 return tempToFile.setLastModified( fromFile.lastModified() ); 422 } 423 424 return true; 425 } 426 // public static boolean copy( final File fromFile,final File toFile,final boolean keepTimeStamp ) { 427 // BufferedInputStream fromStream = null; 428 // BufferedOutputStream toStream = null; 429 // File tempToFile = toFile ; 430 // try { 431 // // fromFileãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ã‚¨ãƒ©ãƒ¼ 432 // if( fromFile.isDirectory() ) { 433 // System.err.println( fromFile + " ãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã®ãŸã‚ã€å?ç?§ãã¾ã›ã‚“ã€? ); 434 // return false; 435 // } 436 // // toFileãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ãã®ãƒ‘スã§ãƒ•ァイルåã‚’fromFileã‹ã‚‰å–り出ã™ã? 437 // if( toFile.isDirectory() ) { 438 // tempToFile = new File( toFile,fromFile.getName() ); 439 // } 440 // 441 // fromStream = new BufferedInputStream( new FileInputStream( fromFile ) ); 442 // toStream = new BufferedOutputStream( new FileOutputStream( tempToFile ) ); 443 // 444 // boolean isOK = copy( fromStream,toStream ); 445 // if( !isOK ) { return false; } 446 // 447 // } 448 // catch ( IOException ex ) { 449 // System.out.println(ex.getMessage()); 450 // return false; 451 // } 452 // finally { 453 // Closer.ioClose( fromStream ) ; 454 // Closer.ioClose( toStream ) ; 455 // } 456 // 457 // if( keepTimeStamp ) { 458 // tempToFile.setLastModified( fromFile.lastModified() ); 459 // } 460 // 461 // return true; 462 // } 463 464 private static final byte B_CR = (byte)0x0d ; // '\r' 465 private static final byte B_LF = (byte)0x0a ; // '\n' 466 private static final int BUFSIZE = 8192 ; // 5.1.6.0 (2010/05/01) 467 468 /** 469 * ファイルã®ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?を行ã„ã¾ã™ã? 470 * 471 * ã“ã?ファイルコピã?ã¯ã€ãƒã‚¤ãƒŠãƒªãƒ•ァイル㮠改行コードを 472 * CR+LF ã«çµ±ä¸?—ã¾ã™ã?ã¾ãŸã?UTF-8 ã® BOM(0xef,0xbb,0xbf) ãŒã‚れã?ã€? 473 * å–り除ãã¾ã™ã? 474 * 475 * @og.rev 5.1.6.0 (2010/05/01) æ–°è¦è¿½åŠ? 476 * 477 * @param fromFile コピã?å…?ƒ•ァイル 478 * @param toFile コピã?先ファイル 479 * 480 * @return ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?ãŒæ£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 481 */ 482 public static boolean changeCrLfcopy( final File fromFile,final File toFile ) { 483 BufferedInputStream fromStream = null; 484 BufferedOutputStream toStream = null; 485 File tempToFile = toFile ; 486 try { 487 // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ãã®ãƒ‘スã§ãƒ•ァイルåã‚’fromFileã‹ã‚‰å–り出ã™ã? 488 if( toFile.isDirectory() ) { 489 tempToFile = new File( toFile,fromFile.getName() ); 490 } 491 fromStream = new BufferedInputStream( new FileInputStream( fromFile ) ); 492 toStream = new BufferedOutputStream( new FileOutputStream( tempToFile ) ); 493 494 // int BUFSIZE = 8192 ; // 5.1.6.0 (2010/05/01) static final定義 495 byte[] buf = new byte[BUFSIZE]; 496 int len ; 497 // 4.2.3.0 (2008/05/26) changeCrLf 属æ?対å¿? 498 499 boolean bomCheck = true; // æœ??ã®ä¸?›žã?‘ã€?¼¢?¯?ãƒã‚§ãƒ?‚¯ã‚’行ã†ã€? 500 byte bt = (byte)0x00; // ãƒãƒƒãƒ•ã‚¡ã®æœ?¾Œã¨æœ??ã®æ¯”è¼?™‚ã«ä½¿ç”¨ 501 while( (len = fromStream.read(buf,0,BUFSIZE)) != -1 ) { 502 int st = 0; 503 if( bomCheck && len >= 3 && 504 buf[0] == (byte)0xef && 505 buf[1] == (byte)0xbb && 506 buf[2] == (byte)0xbf ) { 507 st = 3; 508 } 509 else { 510 // ãƒãƒƒãƒ•ã‚¡ã®æœ?¾ŒãŒ CR ã§ã€å?é ã?LF ã®å ´åˆã?LF をパスã—ã¾ã™ã? 511 if( bt == B_CR && buf[0] == B_LF ) { 512 st = 1 ; 513 } 514 } 515 bomCheck = false; 516 517 for( int i=st;i<len;i++ ) { 518 bt = buf[i] ; 519 if( bt == B_CR || bt == B_LF ) { 520 toStream.write( (int)B_CR ); // CR 521 toStream.write( (int)B_LF ); // LF 522 // CR+LF ã®å ´å? 523 if( bt == B_CR && i+1 < len && buf[i+1] == B_LF ) { 524 i++; 525 bt = buf[i] ; 526 } 527 } 528 else { 529 toStream.write( (int)bt ); 530 } 531 } 532 } 533 // æœ?¾ŒãŒæ”¹è¡Œã‚³ãƒ¼ãƒ‰ã§ãªã‘れã°ã€æ”¹è¡Œã‚³ãƒ¼ãƒ‰ã‚’追åŠ?—ã¾ã™ã? 534 // ãƒ?‚ストコピã?ã¨ã®äº’æ›æ€§ã®ãŸã‚ 535 if( bt != B_CR && bt != B_LF ) { 536 toStream.write( (int)B_CR ); // CR 537 toStream.write( (int)B_LF ); // LF 538 } 539 } 540 catch ( IOException ex ) { 541 System.out.println(ex.getMessage()); 542 return false; 543 } 544 finally { 545 Closer.ioClose( fromStream ) ; 546 Closer.ioClose( toStream ) ; 547 } 548 549 return true; 550 } 551 552 /** 553 * 入出力ストリーãƒ?–“ã§ãƒ??ã‚¿ã®è»¢é€ã‚’行ã„ã¾ã™ã? 554 * 555 * ã“ã“ã§ã¯ã€ã™ã§ã«ä½œæ?ã•れãŸã‚¹ãƒˆãƒªãƒ¼ãƒ?«åŸºã¥ãã?ãƒ??ã‚¿ã®å…¥å‡ºåŠ›ã‚’è¡Œã„ã¾ã™ã? 556 * よã£ã¦ã€å?ã«ãƒ•ォルãƒ?½œæ?ã‚??å˜åœ¨ãƒã‚§ãƒ?‚¯ã€ãƒ•ァイルã®å‰Šé™¤ãªã©ã®å¿?¦ãªå‡¦ç?? 557 * 済ã¾ã—ã¦ç½®ã?¦ãã ã•ã„ã€? 558 * ã¾ãŸã?ã“ã?メソãƒ?ƒ‰å†?§ã€ã‚¹ãƒˆãƒªãƒ¼ãƒ??クãƒãƒ¼ã‚¹å‡¦ç??行ã£ã¦ã?¾ã›ã‚“ã€? 559 * 560 * @og.rev 5.1.6.0 (2010/05/01) æ–°è¦è¿½åŠ? 561 * 562 * @param input 入力ストリーãƒ? 563 * @param output 出力ストリーãƒ? 564 * 565 * @return ãƒ??タ転é€ãŒæ£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 566 */ 567 public static boolean copy( final InputStream input,final OutputStream output ) { 568 if( input == null ) { 569 System.err.println( "入力ストリーãƒ?Œ 作æ?ã•れã¦ã?¾ã›ã‚“ã€? ); 570 return false; 571 } 572 573 if( output == null ) { 574 System.err.println( "出力ストリーãƒ?Œ 作æ?ã•れã¦ã?¾ã›ã‚“ã€? ); 575 return false; 576 } 577 578 try { 579 byte[] buf = new byte[BUFSIZE]; 580 int len; 581 while((len = input.read(buf)) != -1) { 582 output.write(buf, 0, len); 583 } 584 } 585 catch ( IOException ex ) { 586 System.out.println( ex.getMessage() ); 587 return false; 588 } 589 // finally { 590 // Closer.ioClose( input ); 591 // Closer.ioClose( output ); 592 // } 593 return true ; 594 } 595 596 /** 597 * å†å¸°å‡¦ç?§ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®ã‚³ãƒ”ã?を行ã„ã¾ã™ã? 598 * 599 * æŒ?®šã•れãŸã‚³ãƒ”ã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã‹ã£ãŸã‚Šå˜åœ¨ã—ãªã?¨ãã? falseã‚’è¿”ã—ã¾ã™ã? 600 * 601 * @og.rev 4.3.0.0 (2008/07/24) 追åŠ? 602 * @og.rev 5.1.6.0 (2010/05/01) 戻りå?ã«ã€true/false æŒ?®šã—ã¾ã™ã? 603 * 604 * @param fromDir コピã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªå? 605 * @param toDir コピã?先ディレクトリå? 606 * 607 * @return ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®ã‚³ãƒ”ã?ãŒæ£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 608 */ 609 public static boolean copyDirectry( final String fromDir, final String toDir ) { 610 return copyDirectry( new File( fromDir ), new File( toDir ),false ); 611 } 612 613 /** 614 * å†å¸°å‡¦ç?§ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’コピã?ã—ã¾ã™ã? 615 * 616 * æŒ?®šã•れãŸã‚³ãƒ”ã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã‹ã£ãŸã‚Šå˜åœ¨ã—ãªã?¨ãã? falseã‚’è¿”ã—ã¾ã™ã? 617 * 618 * @og.rev 4.3.0.0 (2008/07/24) 追åŠ? 619 * @og.rev 5.1.6.0 (2010/05/01) å†?ƒ¨å‡¦ç?‚’若干変更ã—ã¾ã™ã? 620 * 621 * @param fromDir コピã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒª 622 * @param toDir コピã?先ディレクトリ 623 * 624 * @return ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®ã‚³ãƒ”ã?ãŒæ£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 625 */ 626 public static boolean copyDirectry( final File fromDir, final File toDir ) { 627 return copyDirectry( fromDir, toDir, false ); 628 } 629 630 /** 631 * å†å¸°å‡¦ç?§ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’コピã?ã—ã¾ã™ã? 632 * 633 * æŒ?®šã•れãŸã‚³ãƒ”ã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã‹ã£ãŸã‚Šå˜åœ¨ã—ãªã?¨ãã? falseã‚’è¿”ã—ã¾ã™ã? 634 * 635 * @og.rev 4.3.0.0 (2008/07/24) 追åŠ? 636 * @og.rev 5.1.6.0 (2010/05/01) å†?ƒ¨å‡¦ç?‚’若干変更ã—ã¾ã™ã? 637 * @og.rev 5.3.7.0 (2011/07/01) フォルãƒ?«ã‚¢ã‚¯ã‚»ã‚¹ã§ããªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼ã‚’è¿”ã—ã¾ã™ã? 638 * 639 * @param fromDir コピã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒª 640 * @param toDir コピã?先ディレクトリ 641 * @param keepTimeStamp タイãƒ?‚¹ã‚¿ãƒ³ãƒ—ç¶æŒ[true/false] 642 * 643 * @return ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®ã‚³ãƒ”ã?ãŒæ£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 644 */ 645 public static boolean copyDirectry( final File fromDir, final File toDir, final boolean keepTimeStamp ) { 646 // コピã?å…?Œãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã??åˆã?falseを返㙠647 // 4.3.4.4 (2009/01/01) 648 if( !fromDir.exists() || !fromDir.isDirectory() ) { 649 System.err.println( fromDir + " ã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã?‹ã€å˜åœ¨ã—ã¾ã›ã‚“ã€? ); 650 return false; 651 } 652 653 // 4.3.4.4 (2009/01/01) 654 if( !toDir.exists() ) { 655 // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹ 656 if( !toDir.mkdirs() ) { 657 System.err.println( toDir + " ã® ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªä½œæ?ã«å¤±æ•—ã—ã¾ã—ãŸã€? ); 658 return false; 659 } 660 } 661 662 // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå†??ファイルをã™ã¹ã¦å–å¾—ã™ã‚? 663 File[] files = fromDir.listFiles(); 664 665 // 5.3.7.0 (2011/07/01) フォルãƒ?«ã‚¢ã‚¯ã‚»ã‚¹ã§ããªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼ 666 if( files == null ) { 667 System.err.println( fromDir + " ã¯ã‚¢ã‚¯ã‚»ã‚¹ã§ãã¾ã›ã‚“ã€? ); 668 return false; 669 } 670 671 // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå†??ファイルã«å¯¾ã—コピã?処ç?‚’行ㆠ672 boolean flag = true; 673 for( int i = 0; files.length>i; i++ ){ 674 if( files[i].isDirectory() ){ // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã?£ãŸå?åˆã?å†å¸°å‘¼ã³å‡ºã—を行ㆠ675 flag = copyDirectry( files[i], new File( toDir, files[i].getName()),keepTimeStamp ); 676 } 677 else{ // ファイルã?£ãŸå?åˆã?ファイルコピã?処ç?‚’行ㆠ678 flag = copy( files[i], new File( toDir, files[i].getName()),keepTimeStamp ); 679 } 680 if( !flag ) { return false; } 681 } 682 return true; 683 } 684 // public static boolean copyDirectry( final File fromDirectry, final File toDirectry ) { 685 // // コピã?å…?Œãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã??åˆã?falseを返㙠686 // // 4.3.4.4 (2009/01/01) 687 // if( !fromDirectry.exists() || !fromDirectry.isDirectory() ) { return false; } 688 // 689 // // 4.3.4.4 (2009/01/01) 690 // boolean flag = true; 691 // if( !toDirectry.exists() ) { 692 // // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹ 693 // flag = toDirectry.mkdirs(); 694 // if( ! flag ) { System.err.println( toDirectry.getName() + " ã® ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªä½œæ?ã«å¤±æ•—ã—ã¾ã—ãŸã€? ); } 695 // } 696 // 697 // // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå†??ファイルをã™ã¹ã¦å–å¾—ã™ã‚? 698 // File[] files = fromDirectry.listFiles(); 699 // 700 // // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå†??ファイルã«å¯¾ã—コピã?処ç?‚’行ㆠ701 // for( int i = 0; files.length>i; i++ ){ 702 // if( files[i].isDirectory() ){ // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã?£ãŸå?åˆã?å†å¸°å‘¼ã³å‡ºã—を行ㆠ703 // copyDirectry( 704 // new File( fromDirectry.toString(), files[i].getName() ), 705 // new File( toDirectry.toString(), files[i].getName())); 706 // } 707 // else{ // ファイルã?£ãŸå?åˆã?ファイルコピã?処ç?‚’行ㆠ708 // copy( 709 // new File( fromDirectry.toString(), files[i].getName() ), 710 // new File( toDirectry.toString(), files[i].getName()) ); 711 // } 712 // } 713 // return true; 714 // } 715 716 /** 717 * æŒ?®šã•れãŸãƒ•ァイルåŠã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆã‚’削除ã—ã¾ã™ã? 718 * ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?サブフォルãƒ?Šã³ãƒ•ァイルも削除ã—ã¾ã™ã? 719 * ?‘ã¤ã§ã‚‚ファイルã®å‰Šé™¤ã«å¤±æ•—ã—ãŸå?åˆã?ãã?時点ã§å‡¦ç?‚’䏿–ã—falseã‚’è¿”ã—ã¾ã™ã? 720 * 721 * @og.rev 5.3.7.0 (2011/07/01) フォルãƒ?«ã‚¢ã‚¯ã‚»ã‚¹ã§ããªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼ã‚’è¿”ã—ã¾ã™ã? 722 * 723 * @param file 削除ファイル/ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒª 724 * 725 * @return ファイル/ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å‰Šé™¤ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗] 726 */ 727 public static boolean deleteFiles( final File file ) { 728 if( file.exists() ) { 729 if( file.isDirectory() ) { 730 File[] list = file.listFiles(); 731 732 // 5.3.7.0 (2011/07/01) フォルãƒ?«ã‚¢ã‚¯ã‚»ã‚¹ã§ããªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼ 733 if( list == null ) { 734 System.err.println( file + " ã¯ã‚¢ã‚¯ã‚»ã‚¹ã§ãã¾ã›ã‚“ã€? ); 735 return false; 736 } 737 738 for( int i=0; i<list.length; i++ ) { 739 deleteFiles( list[i] ); 740 } 741 } 742 if( !file.delete() ) { return false; } 743 } 744 return true; 745 } 746 747 /** 748 * æŒ?®šã•れãŸãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’基点ã¨ã—ãŸãƒ•ァイルå?パスをå«ã‚?ã®ä¸?¦§ã‚’è¿”ã—ã¾ã™ã? 749 * 750 * @og.rev 4.3.6.6 (2009/05/15) æ–°è¦ä½œæ? 751 * @og.rev 5.4.3.2 (2012/01/06) 引数isCopy追åŠ? 752 * 753 * @param dir 基点ã¨ãªã‚‹ãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒª 754 * @param sort ファイルåã§ã‚½ãƒ¼ãƒˆã™ã‚‹ã‹ 755 * @param list ファイルåä¸?¦§ã‚’æ?ç´ã™ã‚‹List 756 * @param isCopy コピã?ä¸ãƒ•ァイルを除外ã™ã‚‹ã‹ [true:å«ã‚?false:除外] 757 */ 758 public static void getFileList( final File dir, final boolean sort, final List<String> list, boolean isCopy ) { 759 if( list == null ) { return; } 760 if( dir.isFile() ) { 761 // コピã?ä¸åˆ¤å®šã?renameã§è¡Œã† 762 if( !isCopy && !dir.renameTo( dir ) ){ 763 return; 764 } 765 else{ 766 list.add( dir.getAbsolutePath() ); 767 } 768 } 769 else if( dir.isDirectory() ) { 770 File[] files = dir.listFiles(); 771 for( int i=0; i<files.length; i++ ) { 772 getFileList( files[i], sort, list, isCopy ); 773 } 774 } 775 if( sort ) { 776 Collections.sort( list ); 777 } 778 } 779 780 /** 781 * æŒ?®šã•れãŸãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’基点ã¨ã—ãŸãƒ•ァイルå?パスをå«ã‚?ã®ä¸?¦§ã‚’è¿”ã—ã¾ã™ã? 782 * äº’æ›æ€§ã®ãŸã‚ã€ã‚³ãƒ”ã?ä¸ãƒ•ァイルもå«ã¿ã¾ã™ã? 783 * 784 * @og.rev 5.4.3.2 (2012/01/06) コピã?ä¸å¯¾å¿œã?ãŸã‚引数?”ã¤ã‚’作æ?ã™ã‚‹ 785 * 786 * @param dir 基点ã¨ãªã‚‹ãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒª 787 * @param sort ファイルåã§ã‚½ãƒ¼ãƒˆã™ã‚‹ã‹ 788 * @param list ファイルåä¸?¦§ã‚’æ?ç´ã™ã‚‹List 789 */ 790 public static void getFileList( final File dir, final boolean sort, final List<String> list ) { 791 getFileList( dir, sort, list, true ); 792 } 793 794 /** 795 * æŒ?®šã•れãŸãƒ•ァイルå?パスをå«ã‚?ã‹ã‚‰ã€ãƒ‘スも拡張åã‚‚ãªã?ƒ•ァイルåã‚’è¿”ã—ã¾ã™ã? 796 * 797 * @og.rev 5.6.1.2 (2013/02/22) æ–°è¦ä½œæ? 798 * 799 * @param filename ファイルå?パスをå«ã‚? 800 * @return パスもã?æ‹¡å¼µåã‚‚ãªã?ƒ•ァイルå? 801 */ 802 public static String getBaseName( final String filename ) { 803 804 if (filename == null) { 805 return null; 806 } 807 808 // セパレータã®ä½ç½®ã‚’å–å¾—ã? 809 int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR); 810 int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR); 811 int lastSepPos = Math.max( lastUnixPos , lastWindowsPos ); 812 813 // æ‹¡å¼µåã?ä½ç½®ã‚’å–å¾—ã? 814 int extPos = filename.lastIndexOf(EXTENSION_SEPARATOR); 815 if( lastSepPos > extPos ) { extPos = -1; } // 念ã®ãŸã‚ã€æœ€å¾Œã?セパレータよりå‰ã«ã‚ã‚‹æ‹¡å¼µåã?区åˆ?‚Šæ–?—ã?無効ã€? 816 817 if( extPos < 0 ) { 818 // SEPARATOR ãŒãªã‘れã°ã€lastSepPos + 1 = 0 ã¨ãªã‚Šã?å…ˆé?ã‹ã‚‰å–å¾—ã§ãã‚‹ã€? 819 return filename.substring( lastSepPos + 1 ); 820 } else { 821 return filename.substring( lastSepPos + 1 , extPos ); 822 } 823 } 824 825 /** 826 * ファイルをリãƒã?ãƒ?‚’行ã„ã¾ã™ã? 827 * 引数ã®useBackup属æ?ã‚?true ã«ã™ã‚‹ã¨ã€toFile ãŒå˜åœ¨ã—ãŸå ´åˆã?toFile ã®ç›´ä¸‹ã« "_backup" フォルãƒ?‚’ 828 * 作æ?ã—ã¦ã€toFile ??"_" ??(ç¾åœ¨æ™‚刻ã®LONG値) ??"." ??(toFileã®æ‹¡å¼µå? ã«åå‰å¤‰æ›´ã—ã¾ã™ã? 829 * useBackup属æ?ã‚?false ã«ã™ã‚‹ã¨ã€toFile ãŒå˜åœ¨ã—ãŸå ´åˆã?toFile を削除ã—ã¾ã™ã? 830 * 831 * @og.rev 5.7.1.2 (2013/12/20) æ–°è¦è¿½åŠ? 832 * 833 * @param fromFile åå‰å¤‰æ›´ã™ã‚‹å…??ファイル 834 * @param toFile åå‰å¤‰æ›´å¾Œã?ファイル 835 * @param useBackup ãƒãƒƒã‚¯ã‚¢ãƒ??を作æ?ã™ã‚‹ã‹ã©ã?‹(true:作æ?ã™ã‚‹/false:作æ?ã—ãªã? 836 * @return true:æ£å¸¸å‡¦ç?false:異常処ç? 837 */ 838 public static boolean renameTo( final File fromFile , final File toFile , final boolean useBackup ) { 839 if( fromFile == null || toFile == null ) { 840 String errMsg = "入力ファイルã?null ã§ã™ã?" ; 841 System.err.println( errMsg ); 842 return false; 843 } 844 845 // 変更先ã?ファイルãŒå˜åœ¨ã—ãŸå ´åˆã?処ç?? 846 if( toFile.exists() ) { 847 // ãƒãƒƒã‚¯ã‚¢ãƒ??作æ?ã™ã‚‹å ´å? 848 if( useBackup ) { 849 File parent = toFile.getParentFile(); // ãƒãƒƒã‚¯ã‚¢ãƒ??ã™ã¹ãファイルã®ãƒ•ォルãƒ? 850 File backup = new File( parent , "_backup" ); // ãã?直下ã«ã€?_backup" フォルãƒ?‚’作æ? 851 if( !backup.exists() && !backup.mkdirs() ) { 852 String errMsg = "ãƒãƒƒã‚¯ã‚¢ãƒ??処ç?§backupフォルãƒ??作æ?ã«å¤±æ•—ã—ã¾ã—ãŸã€?" + backup + "]"; 853 System.err.println( errMsg ); 854 return false; 855 } 856 // ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルåã?ã€å?ã®ãƒ•ァイルå?æ‹¡å¼µåå«ã‚? ??"_" + ç¾åœ¨æ™‚刻ã®long値 + "." + å…??ãƒ•ã‚¡ã‚¤ãƒ«ã®æ‹¡å¼µå? 857 String bkupName = toFile.getName(); 858 File toFile2 = new File( parent,bkupName ); // オリジナル㮠toFile ã‚’rename ã™ã‚‹ã¨ã¾ãšã„ã®ã§ã€åŒåã?Fileオブジェクトを作æ? 859 860 bkupName = bkupName + "_" + System.currentTimeMillis() + "." + getExtension( bkupName ) ; 861 File bkupFile = new File( backup,bkupName ); 862 863 if( !toFile2.renameTo( bkupFile ) ) { 864 String errMsg = "ãƒãƒƒã‚¯ã‚¢ãƒ??処ç?§ãƒãƒƒã‚¯ã‚¢ãƒ??ファイルをリãƒã?ãƒ?§ãã¾ã›ã‚“ã§ã—ãŸã€? +CR 865 + " [" + toFile + "] â‡?[" + bkupFile + "]" ; 866 System.err.println( errMsg ); 867 return false; 868 } 869 } 870 // ãƒãƒƒã‚¯ã‚¢ãƒ??作æ?ã—ãªã??åˆã?ã€å‰Šé™¤ã—ã¾ã™ã? 871 else if( !toFile.delete() ) { 872 String errMsg = "æ—¢å˜ã?ファイル[" + toFile + "]ãŒå‰Šé™¤ã§ãã¾ã›ã‚“ã§ã—ãŸã€?; 873 System.err.println( errMsg ); 874 return false; 875 } 876 } 877 878 if( !fromFile.renameTo( toFile ) ) { 879 String errMsg = "æ‰?®šã?ファイルをリãƒã?ãƒ?§ãã¾ã›ã‚“ã§ã—ãŸã€? + CR 880 + " [" + fromFile + "] â‡?[" + toFile + "]" ; 881 System.err.println( errMsg ); 882 return false; 883 } 884 return true; 885 } 886 887 /** 888 * ファイルåã‹ã‚?æ‹¡å¼µåã‚’å–å¾—ã—ã¾ã™ã? 889 * 890 * ä¸?•ªæœ?¾Œã«è¦‹ã¤ã‹ã£ãŸãƒ”リオドã‹ã‚‰å¾Œã‚ã‚’å?りå–ã£ã¦è¿”ã—ã¾ã™ã? 891 * æ‹¡å¼µåã?区åˆ?‚Šæ–??".")ãŒãªã‘れã°ã€ç©ºæ–?—å?ã‚’è¿”ã—ã¾ã™ã? 892 * 893 * @og.rev 5.7.1.2 (2013/12/20) UploadedFileã‹ã‚‰ã«ç§»å‹•ã?若干ã®ãƒã‚¸ãƒ?‚¯å¤‰æ›´ 894 * 895 * @param fileName ファイルå? 896 * @return æ‹¡å¼µå? 897 */ 898 public static String getExtension( final String fileName ) { 899 int extPos = fileName.lastIndexOf( EXTENSION_SEPARATOR ); 900 if( extPos >= 0 ) { 901 return fileName.substring( extPos + 1 ); 902 } 903 return ""; 904 } 905 906 /** 907 * PrintWriter を継承ã—ãŸã€System.out/System.err 用ã®ã‚¯ãƒ©ã‚¹ã‚’定義ã—ã¾ã™ã? 908 * 909 * 通常ã®ã€new PrintWriter( OutputStream ) ã§ã€æ±‚ã‚ã‚‹ã?ã¨ã€ã»ã¨ã‚“ã©åŒæ§˜ã? 910 * 処ç?‚’行ã„ã¾ã™ã? 911 * ãŸã ã€close() メソãƒ?ƒ‰ãŒå‘¼ã°ã‚Œã¦ã‚‚ã?何もã—ã¾ã›ã‚“ã€? 912 * 913 */ 914 private static final class NonClosePrintWriter extends PrintWriter { 915 /** 916 * コンストラクター 917 * 918 * new PrintWriter( OutputStream ) を行ã„ã¾ã™ã? 919 * 920 * @param out OutputStream 921 */ 922 public NonClosePrintWriter( final OutputStream out ) { 923 super( out ); 924 } 925 926 /** 927 * close() メソãƒ?ƒ‰ã‚’オーãƒã?ライドã—ã¾ã™ã? 928 * 929 * 何もã—ã¾ã›ã‚“ã€? 930 * 931 */ 932 public void close() { 933 // ã“ã“ã§ã¯å‡¦ç?‚’行ã„ã¾ã›ã‚“ã€? 934 } 935 } 936 937 /** 938 * PrintWriter を継承ã—ãŸã€JspWriterãªã©ã® Writer 用ã®ã‚¯ãƒ©ã‚¹ã‚’定義ã—ã¾ã™ã? 939 * 940 * 例ãˆã°ã€JspWriterãªã©ã® JSP/Servletç‰ã?フレーãƒ?ƒ¯ãƒ¼ã‚¯ã§ä½¿ç”¨ã•れã‚? 941 * Writer ã§ã¯ã€flush ã‚?close 処ç??ã€ãƒ•レーãƒ?ƒ¯ãƒ¼ã‚¯å†?§è¡Œã‚れã¾ã™ã? 942 * ãã?å ´åˆã?通常ã®ãƒ•ァイルã¨åŒã˜ç”¨ã«ã€flush ã‚?close をアプリケーションå´ã§ 943 * 行ã†ã¨ã€å?部処ç?š„ã«ä¸æ•´åˆãŒç™ºç”Ÿã—ãŸã‚Šã€æœ€æ‚ªã®å ´åˆã‚¨ãƒ©ãƒ¼ã«ãªã‚Šã¾ã™ã? 944 * ã“ã?クラスã¯ã€å˜ã«ã€??常ã®ã€new PrintWriter( Writer ) ã§ã€æ±‚ã‚ã‚‹ã?ã¨ã€? 945 * ã»ã¨ã‚“ã©åŒæ§˜ã?処ç?‚’行ã„ã¾ã™ã? 946 * ãŸã ã€close() 㨠flush() メソãƒ?ƒ‰ãŒå‘¼ã°ã‚Œã¦ã‚‚ã?何もã—ã¾ã›ã‚“ã€? 947 * 948 */ 949 private static final class NonFlushPrintWriter extends PrintWriter { 950 /** 951 * コンストラクター 952 * 953 * new PrintWriter( Writer ) を行ã„ã¾ã™ã? 954 * 955 * @param writer Writer 956 */ 957 public NonFlushPrintWriter( final Writer writer ) { 958 super( writer ); 959 } 960 961 /** 962 * close() メソãƒ?ƒ‰ã‚’オーãƒã?ライドã—ã¾ã™ã? 963 * 964 * 何もã—ã¾ã›ã‚“ã€? 965 * 966 */ 967 public void close() { 968 // ã“ã“ã§ã¯å‡¦ç?‚’行ã„ã¾ã›ã‚“ã€? 969 } 970 971 /** 972 * flush() メソãƒ?ƒ‰ã‚’オーãƒã?ライドã—ã¾ã™ã? 973 * 974 * 何もã—ã¾ã›ã‚“ã€? 975 * 976 */ 977 public void flush() { 978 // ã“ã“ã§ã¯å‡¦ç?‚’行ã„ã¾ã›ã‚“ã€? 979 } 980 } 981 982 /** 983 * ファイルをコピã?ã—ã¾ã™ã? 984 * 985 * 引数㫠<file1> <file2> [<encode1> <encode2>] を指定ã—ã¾ã™ã? 986 * file1 ã‚’èªã¿è¾¼ã¿ã€file2 ã«ã‚³ãƒ”ã?ã—ã¾ã™ã?コピã?å‰ã«ã€file2 ã¯ã€file2_backup ã«ã‚³ãƒ”ã?ã—ã¾ã™ã? 987 * file1 ãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã”ã¨ã‚³ãƒ”ã?ã—ã¾ã™ã? 988 * encode1ã€encode2 を指定ã™ã‚‹ã¨ã€ã‚¨ãƒ³ã‚³ãƒ¼ãƒ‰å¤‰æ›ã—ãªãŒã‚‰ã‚³ãƒ”ã?ã«ãªã‚Šã¾ã™ã? 989 * ã“ã?å ´åˆã?ã€ãƒ•ァイルåŒå£«ã®ã‚³ãƒ”ã?ã®ã¿ã«ãªã‚Šã¾ã™ã? 990 * 991 * @og.rev 4.0.0.0 (2007/11/28) メソãƒ?ƒ‰ã®æˆ»ã‚Šå?ã‚’ãƒã‚§ãƒ?‚¯ã—ã¾ã™ã? 992 * @og.rev 5.1.6.0 (2010/05/01) 引数ã®ä¸¦ã³é ??処ç?‚’変更ã—ã¾ã™ã? 993 * 994 * @param args 引数é…å? file1 file2 [encode1 encode2] 995 * @throws Throwable ãªã‚“らã‹ã?エラーãŒç™ºç”Ÿã—ãŸå?åˆã? 996 */ 997 public static void main( final String[] args ) throws Throwable { 998 if( args.length != 2 && args.length != 4 ) { 999 LogWriter.log("Usage: java FileUtil <file1> <file2> [<encode1> <encode2>]" ); 1000 return ; 1001 } 1002 1003 File file1 = new File( args[0] ); 1004 File file2 = new File( args[1] ); 1005 1006 File tempFile = new File( args[1] + "_backup" ); 1007 1008 if( args.length < 3 ) { 1009 if( file1.isDirectory() ) { 1010 FileUtil.copyDirectry( file1, file2, true ); 1011 } 1012 else { 1013 FileUtil.copy( file2,tempFile ); 1014 FileUtil.copy( file1,file2, true ); 1015 } 1016 } 1017 else { 1018 String encode1 = args[2]; 1019 String encode2 = args[3]; 1020 1021 FileUtil.copy( file2,tempFile ); 1022 1023 BufferedReader reader = FileUtil.getBufferedReader( file1 ,encode1 ); 1024 PrintWriter writer = FileUtil.getPrintWriter( file2 ,encode2 ); 1025 1026 try { 1027 String line1; 1028 while((line1 = reader.readLine()) != null) { 1029 writer.println( line1 ); 1030 } 1031 } 1032 finally { 1033 Closer.ioClose( reader ) ; 1034 Closer.ioClose( writer ) ; 1035 } 1036 } 1037 } 1038 }