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             *
358             * @param       fromFile        コピã?å…?ƒ•ァイル
359             * @param       toFile          コピã?先ファイル
360             * @param       keepTimeStamp タイãƒ?‚¹ã‚¿ãƒ³ãƒ—ç¶­æŒ[true/false]
361             *
362             * @return      ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?ãŒæ­£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗]
363             * @see         #copyDirectry( File,File,boolean )
364             */
365            public static boolean copy( final File fromFile,final File toFile,final boolean keepTimeStamp ) {
366                    FileInputStream  inFile  = null;
367                    FileOutputStream outFile = null;
368                    FileChannel  fin  = null;
369                    FileChannel  fout = null;
370    
371                    File tempToFile = toFile ;
372                    try {
373                            // fromFileãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ã‚¨ãƒ©ãƒ¼
374                            if( fromFile.isDirectory() ) {
375                                    System.err.println( fromFile + " ãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã®ãŸã‚ã€å?ç?§ãã¾ã›ã‚“ã€? );
376                                    return false;
377                            }
378                            // toFileãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ãã®ãƒ‘スã§ãƒ•ァイルåã‚’fromFileã‹ã‚‰å–り出ã™ã?
379                            if( toFile.isDirectory() ) {
380                                    tempToFile = new File( toFile,fromFile.getName() );
381                            }
382                            inFile  = new FileInputStream( fromFile );
383                            outFile = new FileOutputStream( tempToFile );
384    
385                            fin  = inFile.getChannel();
386                            fout = outFile.getChannel();
387    
388                            // 5.6.5.2 (2013/06/21) ByteBufferを利用ã—ãŸæ–¹å¼ã‹ã‚‰ã?transferTo を使用ã™ã‚‹æ–¹å¼ã«å¤‰æ›´
389    //                      ByteBuffer buffer = ByteBuffer.allocateDirect( BUFSIZE );
390    //                      while ( (fin.read(buffer) != -1) || buffer.position() > 0) {
391    //                              buffer.flip();
392    //                              fout.write( buffer );
393    //                              buffer.compact();
394    //                      }
395    
396                            fin.transferTo(0, fin.size(), fout );
397    
398                    }
399                    catch ( IOException ex ) {
400                            System.out.println(ex.getMessage());
401                            return false;
402                    }
403                    finally {
404                            Closer.ioClose( inFile  ) ;
405                            Closer.ioClose( outFile );
406                            Closer.ioClose( fin  ) ;
407                            Closer.ioClose( fout );
408                    }
409    
410                    if( keepTimeStamp ) {
411                            return tempToFile.setLastModified( fromFile.lastModified() );
412                    }
413    
414                    return true;
415            }
416    //      public static boolean copy( final File fromFile,final File toFile,final boolean keepTimeStamp ) {
417    //              BufferedInputStream     fromStream = null;
418    //              BufferedOutputStream    toStream   = null;
419    //              File tempToFile = toFile ;
420    //              try {
421    //                      // fromFileãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ã‚¨ãƒ©ãƒ¼
422    //                      if( fromFile.isDirectory() ) {
423    //                              System.err.println( fromFile + " ãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã®ãŸã‚ã€å?ç?§ãã¾ã›ã‚“ã€? );
424    //                              return false;
425    //                      }
426    //                      // toFileãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ãã®ãƒ‘スã§ãƒ•ァイルåã‚’fromFileã‹ã‚‰å–り出ã™ã?
427    //                      if( toFile.isDirectory() ) {
428    //                              tempToFile = new File( toFile,fromFile.getName() );
429    //                      }
430    //
431    //                      fromStream = new BufferedInputStream( new FileInputStream( fromFile ) );
432    //                      toStream   = new BufferedOutputStream( new FileOutputStream( tempToFile ) );
433    //
434    //                      boolean isOK = copy( fromStream,toStream );
435    //                      if( !isOK ) { return false; }
436    //
437    //              }
438    //              catch ( IOException ex ) {
439    //                      System.out.println(ex.getMessage());
440    //                      return false;
441    //              }
442    //              finally {
443    //                      Closer.ioClose( fromStream ) ;
444    //                      Closer.ioClose( toStream ) ;
445    //              }
446    //
447    //              if( keepTimeStamp ) {
448    //                      tempToFile.setLastModified( fromFile.lastModified() );
449    //              }
450    //
451    //              return true;
452    //      }
453    
454            private static final byte B_CR = (byte)0x0d ;   // '\r'
455            private static final byte B_LF = (byte)0x0a ;   // '\n'
456            private static final int  BUFSIZE = 8192 ;              // 5.1.6.0 (2010/05/01)
457    
458            /**
459             * ファイルã®ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?を行ã„ã¾ã™ã?
460             *
461             * ã“ã?ファイルコピã?ã¯ã€ãƒã‚¤ãƒŠãƒªãƒ•ァイル㮠改行コードを
462             * CR+LF ã«çµ±ä¸?—ã¾ã™ã?ã¾ãŸã?UTF-8 ã® BOM(0xef,0xbb,0xbf) ãŒã‚れã?ã€?
463             * å–り除ãã¾ã™ã?
464             *
465             * @og.rev 5.1.6.0 (2010/05/01) æ–°è¦è¿½åŠ?
466             *
467             * @param       fromFile        コピã?å…?ƒ•ァイル
468             * @param       toFile          コピã?先ファイル
469             *
470             * @return      ãƒã‚¤ãƒŠãƒªã‚³ãƒ”ã?ãŒæ­£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗]
471             */
472            public static boolean changeCrLfcopy( final File fromFile,final File toFile ) {
473                    BufferedInputStream     fromStream = null;
474                    BufferedOutputStream    toStream   = null;
475                    File tempToFile = toFile ;
476                    try {
477                            // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ãã®ãƒ‘スã§ãƒ•ァイルåã‚’fromFileã‹ã‚‰å–り出ã™ã?
478                            if( toFile.isDirectory() ) {
479                                    tempToFile = new File( toFile,fromFile.getName() );
480                            }
481                            fromStream = new BufferedInputStream( new FileInputStream( fromFile ) );
482                            toStream   = new BufferedOutputStream( new FileOutputStream( tempToFile ) );
483    
484    //                      int BUFSIZE = 8192 ;            // 5.1.6.0 (2010/05/01) static final定義
485                            byte[] buf = new byte[BUFSIZE];
486                            int len ;
487                            // 4.2.3.0 (2008/05/26) changeCrLf 属æ?対å¿?
488    
489                            boolean bomCheck = true;        // æœ??ã®ä¸?›žã?‘ã€?¼¢?¯?­ãƒã‚§ãƒ?‚¯ã‚’行ã†ã€?
490                            byte    bt = (byte)0x00;        // ãƒãƒƒãƒ•ã‚¡ã®æœ?¾Œã¨æœ??ã®æ¯”è¼?™‚ã«ä½¿ç”¨
491                            while( (len = fromStream.read(buf,0,BUFSIZE)) != -1 ) {
492                                    int st = 0;
493                                    if( bomCheck && len >= 3 &&
494                                            buf[0] == (byte)0xef &&
495                                            buf[1] == (byte)0xbb &&
496                                            buf[2] == (byte)0xbf  ) {
497                                                    st = 3;
498                                    }
499                                    else {
500                                            // ãƒãƒƒãƒ•ã‚¡ã®æœ?¾ŒãŒ CR ã§ã€å?é ­ã?LF ã®å ´åˆã?LF をパスã—ã¾ã™ã?
501                                            if( bt == B_CR && buf[0] == B_LF ) {
502                                                    st = 1 ;
503                                            }
504                                    }
505                                    bomCheck = false;
506    
507                                    for( int i=st;i<len;i++ ) {
508                                            bt = buf[i] ;
509                                            if( bt == B_CR || bt == B_LF ) {
510                                                    toStream.write( (int)B_CR );            // CR
511                                                    toStream.write( (int)B_LF );            // LF
512                                                    // CR+LF ã®å ´å?
513                                                    if( bt == B_CR && i+1 < len && buf[i+1] == B_LF ) {
514                                                            i++;
515                                                            bt = buf[i] ;
516                                                    }
517                                            }
518                                            else {
519                                                    toStream.write( (int)bt );
520                                            }
521                                    }
522                            }
523                            // æœ?¾ŒãŒæ”¹è¡Œã‚³ãƒ¼ãƒ‰ã§ãªã‘れã°ã€æ”¹è¡Œã‚³ãƒ¼ãƒ‰ã‚’追åŠ?—ã¾ã™ã?
524                            // ãƒ?‚­ã‚¹ãƒˆã‚³ãƒ”ã?ã¨ã®äº’æ›æ€§ã®ãŸã‚
525                            if( bt != B_CR && bt != B_LF ) {
526                                    toStream.write( (int)B_CR );            // CR
527                                    toStream.write( (int)B_LF );            // LF
528                            }
529                    }
530                    catch ( IOException ex ) {
531                            System.out.println(ex.getMessage());
532                            return false;
533                    }
534                    finally {
535                            Closer.ioClose( fromStream ) ;
536                            Closer.ioClose( toStream ) ;
537                    }
538    
539                    return true;
540            }
541    
542            /**
543             * 入出力ストリーãƒ?–“ã§ãƒ??ã‚¿ã®è»¢é€ã‚’行ã„ã¾ã™ã?
544             *
545             * ã“ã“ã§ã¯ã€ã™ã§ã«ä½œæ?ã•れãŸã‚¹ãƒˆãƒªãƒ¼ãƒ?«åŸºã¥ãã?ãƒ??ã‚¿ã®å…¥å‡ºåŠ›ã‚’è¡Œã„ã¾ã™ã?
546             * よã£ã¦ã€å?ã«ãƒ•ォルãƒ?½œæ?ã‚??存在ãƒã‚§ãƒ?‚¯ã€ãƒ•ァイルã®å‰Šé™¤ãªã©ã®å¿?¦ãªå‡¦ç??
547             * 済ã¾ã—ã¦ç½®ã?¦ãã ã•ã„ã€?
548             * ã¾ãŸã?ã“ã?メソãƒ?ƒ‰å†?§ã€ã‚¹ãƒˆãƒªãƒ¼ãƒ??クロース処ç??行ã£ã¦ã?¾ã›ã‚“ã€?
549             *
550             * @og.rev 5.1.6.0 (2010/05/01) æ–°è¦è¿½åŠ?
551             *
552             * @param       input   入力ストリー�
553             * @param       output  出力ストリー�
554             *
555             * @return      ãƒ??タ転é€ãŒæ­£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗]
556             */
557            public static boolean copy( final InputStream input,final OutputStream output ) {
558                    if( input == null ) {
559                            System.err.println( "入力ストリーãƒ?Œ 作æ?ã•れã¦ã?¾ã›ã‚“ã€? );
560                            return false;
561                    }
562    
563                    if( output == null ) {
564                            System.err.println( "出力ストリーãƒ?Œ 作æ?ã•れã¦ã?¾ã›ã‚“ã€? );
565                            return false;
566                    }
567    
568                    try {
569                            byte[] buf = new byte[BUFSIZE];
570                            int len;
571                            while((len = input.read(buf)) != -1) {
572                                    output.write(buf, 0, len);
573                            }
574                    }
575                    catch ( IOException ex ) {
576                            System.out.println( ex.getMessage() );
577                            return false;
578                    }
579            //      finally {
580            //              Closer.ioClose( input );
581            //              Closer.ioClose( output );
582            //      }
583                    return true ;
584            }
585    
586            /**
587             * å†å¸°å‡¦ç?§ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®ã‚³ãƒ”ã?を行ã„ã¾ã™ã?
588             *
589             * æŒ?®šã•れãŸã‚³ãƒ”ã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã‹ã£ãŸã‚Šå­˜åœ¨ã—ãªã?¨ãã?  falseã‚’è¿”ã—ã¾ã™ã?
590             *
591             * @og.rev 4.3.0.0 (2008/07/24) 追�
592             * @og.rev 5.1.6.0 (2010/05/01) 戻りå?ã«ã€true/false æŒ?®šã—ã¾ã™ã?
593             *
594             * @param       fromDir コピã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªå?
595             * @param       toDir   コピã?先ディレクトリå?
596             *
597             * @return      ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®ã‚³ãƒ”ã?ãŒæ­£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗]
598             */
599            public static boolean copyDirectry( final String fromDir, final String toDir ) {
600                    return copyDirectry( new File( fromDir ), new File( toDir ),false );
601            }
602    
603            /**
604             * å†å¸°å‡¦ç?§ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’コピã?ã—ã¾ã™ã?
605             *
606             * æŒ?®šã•れãŸã‚³ãƒ”ã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã‹ã£ãŸã‚Šå­˜åœ¨ã—ãªã?¨ãã?  falseã‚’è¿”ã—ã¾ã™ã?
607             *
608             * @og.rev 4.3.0.0 (2008/07/24) 追�
609             * @og.rev 5.1.6.0 (2010/05/01) å†?ƒ¨å‡¦ç?‚’若干変更ã—ã¾ã™ã?
610             *
611             * @param       fromDir   コピã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒª
612             * @param       toDir     コピã?先ディレクトリ
613             *
614             * @return      ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®ã‚³ãƒ”ã?ãŒæ­£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗]
615             */
616            public static boolean copyDirectry( final File fromDir, final File toDir ) {
617                    return copyDirectry( fromDir, toDir, false );
618            }
619    
620            /**
621             * å†å¸°å‡¦ç?§ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’コピã?ã—ã¾ã™ã?
622             *
623             * æŒ?®šã•れãŸã‚³ãƒ”ã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªãŒãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã‹ã£ãŸã‚Šå­˜åœ¨ã—ãªã?¨ãã?  falseã‚’è¿”ã—ã¾ã™ã?
624             *
625             * @og.rev 4.3.0.0 (2008/07/24) 追�
626             * @og.rev 5.1.6.0 (2010/05/01) å†?ƒ¨å‡¦ç?‚’若干変更ã—ã¾ã™ã?
627             * @og.rev 5.3.7.0 (2011/07/01) フォルãƒ?«ã‚¢ã‚¯ã‚»ã‚¹ã§ããªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼ã‚’è¿”ã—ã¾ã™ã?
628             *
629             * @param       fromDir コピã?å…?ƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒª
630             * @param       toDir   コピã?先ディレクトリ
631             * @param       keepTimeStamp タイãƒ?‚¹ã‚¿ãƒ³ãƒ—ç¶­æŒ[true/false]
632             *
633             * @return      ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®ã‚³ãƒ”ã?ãŒæ­£å¸¸ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗]
634             */
635            public static boolean copyDirectry( final File fromDir, final File toDir, final boolean keepTimeStamp ) {
636                    // コピã?å…?Œãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã??åˆã?falseã‚’è¿”ã™
637                    // 4.3.4.4 (2009/01/01)
638                    if( !fromDir.exists() || !fromDir.isDirectory() ) {
639                            System.err.println( fromDir + " ã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã?‹ã€å­˜åœ¨ã—ã¾ã›ã‚“ã€? );
640                            return false;
641                    }
642    
643                    // 4.3.4.4 (2009/01/01)
644                    if( !toDir.exists() ) {
645                            // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹
646                            if( !toDir.mkdirs() ) {
647                                    System.err.println( toDir + " ã® ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªä½œæ?ã«å¤±æ•—ã—ã¾ã—ãŸã€? );
648                                    return false;
649                            }
650                    }
651    
652                    // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå†??ファイルをã™ã¹ã¦å–å¾—ã™ã‚?
653                    File[] files = fromDir.listFiles();
654    
655                    // 5.3.7.0 (2011/07/01) フォルãƒ?«ã‚¢ã‚¯ã‚»ã‚¹ã§ããªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼
656                    if( files == null ) {
657                            System.err.println( fromDir + " ã¯ã‚¢ã‚¯ã‚»ã‚¹ã§ãã¾ã›ã‚“ã€? );
658                            return false;
659                    }
660    
661                    // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå†??ファイルã«å¯¾ã—コピã?処ç?‚’行ã†
662                    boolean flag = true;
663                    for( int i = 0; files.length>i; i++ ){
664                            if( files[i].isDirectory() ){ // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã?£ãŸå?åˆã?å†å¸°å‘¼ã³å‡ºã—を行ã†
665                                    flag = copyDirectry( files[i], new File( toDir, files[i].getName()),keepTimeStamp );
666                            }
667                            else{ // ファイルã?£ãŸå?åˆã?ファイルコピã?処ç?‚’行ã†
668                                    flag = copy( files[i], new File( toDir, files[i].getName()),keepTimeStamp );
669                            }
670                            if( !flag ) { return false; }
671                    }
672                    return true;
673            }
674    //      public static boolean copyDirectry( final File fromDirectry, final File toDirectry ) {
675    //              // コピã?å…?Œãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã§ãªã??åˆã?falseã‚’è¿”ã™
676    //              // 4.3.4.4 (2009/01/01)
677    //              if( !fromDirectry.exists() || !fromDirectry.isDirectory() ) { return false; }
678    //
679    //              // 4.3.4.4 (2009/01/01)
680    //              boolean flag = true;
681    //              if( !toDirectry.exists() ) {
682    //                      // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作æ?ã™ã‚‹
683    //                      flag = toDirectry.mkdirs();
684    //                      if( ! flag ) { System.err.println( toDirectry.getName() + " ã® ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªä½œæ?ã«å¤±æ•—ã—ã¾ã—ãŸã€? ); }
685    //              }
686    //
687    //              // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå†??ファイルをã™ã¹ã¦å–å¾—ã™ã‚?
688    //              File[] files = fromDirectry.listFiles();
689    //
690    //              // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªå†??ファイルã«å¯¾ã—コピã?処ç?‚’行ã†
691    //              for( int i = 0; files.length>i; i++ ){
692    //                      if( files[i].isDirectory() ){ // ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã?£ãŸå?åˆã?å†å¸°å‘¼ã³å‡ºã—を行ã†
693    //                              copyDirectry(
694    //                              new File( fromDirectry.toString(), files[i].getName() ),
695    //                              new File( toDirectry.toString(), files[i].getName()));
696    //                      }
697    //                      else{ // ファイルã?£ãŸå?åˆã?ファイルコピã?処ç?‚’行ã†
698    //                              copy(
699    //                              new File( fromDirectry.toString(), files[i].getName() ),
700    //                              new File( toDirectry.toString(), files[i].getName()) );
701    //                      }
702    //              }
703    //              return true;
704    //      }
705    
706            /**
707             * æŒ?®šã•れãŸãƒ•ァイルåŠã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆã‚’削除ã—ã¾ã™ã?
708             * ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?サブフォルãƒ?Šã³ãƒ•ァイルも削除ã—ã¾ã™ã?
709             * ?‘ã¤ã§ã‚‚ファイルã®å‰Šé™¤ã«å¤±æ•—ã—ãŸå?åˆã?ãã?時点ã§å‡¦ç?‚’中断ã—falseã‚’è¿”ã—ã¾ã™ã?
710             *
711             * @og.rev 5.3.7.0 (2011/07/01) フォルãƒ?«ã‚¢ã‚¯ã‚»ã‚¹ã§ããªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼ã‚’è¿”ã—ã¾ã™ã?
712             *
713             * @param       file 削除ファイル/ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒª
714             *
715             * @return      ファイル/ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å‰Šé™¤ã«çµ‚äº?—ãŸã‹ã©ã?‹[true:æˆåŠŸ/false:失敗]
716             */
717            public static boolean deleteFiles( final File file ) {
718                    if( file.exists() ) {
719                            if( file.isDirectory() ) {
720                                    File[] list = file.listFiles();
721    
722                                    // 5.3.7.0 (2011/07/01) フォルãƒ?«ã‚¢ã‚¯ã‚»ã‚¹ã§ããªã??åˆã?ã€ã‚¨ãƒ©ãƒ¼
723                                    if( list == null ) {
724                                            System.err.println( file + " ã¯ã‚¢ã‚¯ã‚»ã‚¹ã§ãã¾ã›ã‚“ã€? );
725                                            return false;
726                                    }
727    
728                                    for( int i=0; i<list.length; i++ ) {
729                                            deleteFiles( list[i] );
730                                    }
731                            }
732                            if( !file.delete() ) { return false; }
733                    }
734                    return true;
735            }
736    
737            /**
738             * æŒ?®šã•れãŸãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’基点ã¨ã—ãŸãƒ•ァイルå?パスをå«ã‚?ã®ä¸?¦§ã‚’è¿”ã—ã¾ã™ã?
739             *
740             * @og.rev 4.3.6.6 (2009/05/15) æ–°è¦ä½œæ?
741             * @og.rev 5.4.3.2 (2012/01/06) 引数isCopy追�
742             *
743             * @param dir 基点ã¨ãªã‚‹ãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒª
744             * @param sort ファイルåã§ã‚½ãƒ¼ãƒˆã™ã‚‹ã‹
745             * @param list ファイルåä¸?¦§ã‚’æ?ç´ã™ã‚‹List
746             * @param isCopy コピã?中ファイルを除外ã™ã‚‹ã‹ [true:å«ã‚?false:除外]
747             */
748            public static void getFileList( final File dir, final boolean sort, final List<String> list, boolean isCopy ) {
749                    if( list == null ) { return; }
750                    if( dir.isFile() ) {
751                            // コピã?中判定ã?renameã§è¡Œã†
752                            if( !isCopy && !dir.renameTo( dir ) ){
753                                    return;
754                            }
755                            else{
756                                    list.add( dir.getAbsolutePath() );
757                            }
758                    }
759                    else if( dir.isDirectory() ) {
760                            File[] files = dir.listFiles();
761                            for( int i=0; i<files.length; i++ ) {
762                                    getFileList( files[i], sort, list, isCopy );
763                            }
764                    }
765                    if( sort ) {
766                            Collections.sort( list );
767                    }
768            }
769    
770            /**
771             * æŒ?®šã•れãŸãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’基点ã¨ã—ãŸãƒ•ァイルå?パスをå«ã‚?ã®ä¸?¦§ã‚’è¿”ã—ã¾ã™ã?
772             * äº’æ›æ€§ã®ãŸã‚ã€ã‚³ãƒ”ã?中ファイルもå«ã¿ã¾ã™ã?
773             *
774             * @og.rev 5.4.3.2 (2012/01/06) コピã?中対応ã?ãŸã‚引数?”ã¤ã‚’作æ?ã™ã‚‹
775             *
776             * @param dir 基点ã¨ãªã‚‹ãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒª
777             * @param sort ファイルåã§ã‚½ãƒ¼ãƒˆã™ã‚‹ã‹
778             * @param list ファイルåä¸?¦§ã‚’æ?ç´ã™ã‚‹List
779             */
780            public static void getFileList( final File dir, final boolean sort, final List<String> list ) {
781                            getFileList( dir, sort, list, true );
782            }
783    
784            /**
785             * æŒ?®šã•れãŸãƒ•ァイルå?パスをå«ã‚?ã‹ã‚‰ã€ãƒ‘スも拡張å­ã‚‚ãªã?ƒ•ァイルåã‚’è¿”ã—ã¾ã™ã?
786             *
787             * @og.rev 5.6.1.2 (2013/02/22) æ–°è¦ä½œæ?
788             *
789             * @param filename ファイルå?パスをå«ã‚?
790             * @return パスもã?æ‹¡å¼µå­ã‚‚ãªã?ƒ•ァイルå?
791             */
792            public static String getBaseName( final String filename ) {
793    
794                    if (filename == null) {
795                            return null;
796                    }
797    
798                    // セパレータã®ä½ç½®ã‚’å–å¾—ã?
799                    int lastUnixPos    = filename.lastIndexOf(UNIX_SEPARATOR);
800                    int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
801                    int lastSepPos = Math.max( lastUnixPos , lastWindowsPos );
802    
803                    // æ‹¡å¼µå­ã?ä½ç½®ã‚’å–å¾—ã?
804                    int extPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
805                    if( lastSepPos > extPos ) { extPos = -1; }   // 念ã®ãŸã‚ã€æœ€å¾Œã?セパレータよりå‰ã«ã‚ã‚‹æ‹¡å¼µå­ã?区åˆ?‚Šæ–?­—ã?無効ã€?
806    
807                    if( extPos < 0 ) {
808                            // SEPARATOR ãŒãªã‘れã°ã€lastSepPos + 1 = 0 ã¨ãªã‚Šã?å…ˆé?ã‹ã‚‰å–å¾—ã§ãã‚‹ã€?
809                            return filename.substring( lastSepPos + 1 );
810                    } else {
811                            return filename.substring( lastSepPos + 1 , extPos );
812                    }
813            }
814    
815            /**
816             * PrintWriter を継承ã—ãŸã€System.out/System.err 用ã®ã‚¯ãƒ©ã‚¹ã‚’定義ã—ã¾ã™ã?
817             *
818             * 通常ã®ã€new PrintWriter( OutputStream ) ã§ã€æ±‚ã‚ã‚‹ã?ã¨ã€ã»ã¨ã‚“ã©åŒæ§˜ã?
819             * 処ç?‚’行ã„ã¾ã™ã?
820             * ãŸã ã€close() メソãƒ?ƒ‰ãŒå‘¼ã°ã‚Œã¦ã‚‚ã?何もã—ã¾ã›ã‚“ã€?
821             *
822             */
823            private static final class NonClosePrintWriter extends PrintWriter {
824                    /**
825                     * コンストラクター
826                     *
827                     * new PrintWriter( OutputStream ) を行ã„ã¾ã™ã?
828                     *
829                     * @param out OutputStream
830                     */
831                    public NonClosePrintWriter( final OutputStream out ) {
832                            super( out );
833                    }
834    
835                    /**
836                     * close() メソãƒ?ƒ‰ã‚’オーãƒã?ライドã—ã¾ã™ã?
837                     *
838                     * 何もã—ã¾ã›ã‚“ã€?
839                     *
840                     */
841                    public void close() {
842                            // ã“ã“ã§ã¯å‡¦ç?‚’行ã„ã¾ã›ã‚“ã€?
843                    }
844            }
845    
846            /**
847             * PrintWriter を継承ã—ãŸã€JspWriterãªã©ã® Writer 用ã®ã‚¯ãƒ©ã‚¹ã‚’定義ã—ã¾ã™ã?
848             *
849             * 例ãˆã°ã€JspWriterãªã©ã® JSP/Servletç­‰ã?フレーãƒ?ƒ¯ãƒ¼ã‚¯ã§ä½¿ç”¨ã•れã‚?
850             * Writer ã§ã¯ã€flush ã‚?close 処ç??ã€ãƒ•レーãƒ?ƒ¯ãƒ¼ã‚¯å†?§è¡Œã‚れã¾ã™ã?
851             * ãã?å ´åˆã?通常ã®ãƒ•ァイルã¨åŒã˜ç”¨ã«ã€flush ã‚?close をアプリケーションå´ã§
852             * 行ã†ã¨ã€å?部処ç?š„ã«ä¸æ•´åˆãŒç™ºç”Ÿã—ãŸã‚Šã€æœ€æ‚ªã®å ´åˆã‚¨ãƒ©ãƒ¼ã«ãªã‚Šã¾ã™ã?
853             * ã“ã?クラスã¯ã€å˜ã«ã€??常ã®ã€new PrintWriter( Writer ) ã§ã€æ±‚ã‚ã‚‹ã?ã¨ã€?
854             * ã»ã¨ã‚“ã©åŒæ§˜ã?処ç?‚’行ã„ã¾ã™ã?
855             * ãŸã ã€close() 㨠flush() メソãƒ?ƒ‰ãŒå‘¼ã°ã‚Œã¦ã‚‚ã?何もã—ã¾ã›ã‚“ã€?
856             *
857             */
858            private static final class NonFlushPrintWriter extends PrintWriter {
859                    /**
860                     * コンストラクター
861                     *
862                     * new PrintWriter( Writer ) を行ã„ã¾ã™ã?
863                     *
864                     * @param writer Writer
865                     */
866                    public NonFlushPrintWriter( final Writer writer ) {
867                            super( writer );
868                    }
869    
870                    /**
871                     * close() メソãƒ?ƒ‰ã‚’オーãƒã?ライドã—ã¾ã™ã?
872                     *
873                     * 何もã—ã¾ã›ã‚“ã€?
874                     *
875                     */
876                    public void close() {
877                            // ã“ã“ã§ã¯å‡¦ç?‚’行ã„ã¾ã›ã‚“ã€?
878                    }
879    
880                    /**
881                     * flush() メソãƒ?ƒ‰ã‚’オーãƒã?ライドã—ã¾ã™ã?
882                     *
883                     * 何もã—ã¾ã›ã‚“ã€?
884                     *
885                     */
886                    public void flush() {
887                            // ã“ã“ã§ã¯å‡¦ç?‚’行ã„ã¾ã›ã‚“ã€?
888                    }
889            }
890    
891            /**
892             * ファイルをコピã?ã—ã¾ã™ã?
893             *
894             * 引数㫠<file1> <file2> [<encode1> <encode2>] を指定ã—ã¾ã™ã?
895             * file1 を読ã¿è¾¼ã¿ã€file2 ã«ã‚³ãƒ”ã?ã—ã¾ã™ã?コピã?å‰ã«ã€file2 ã¯ã€file2_backup ã«ã‚³ãƒ”ã?ã—ã¾ã™ã?
896             * file1 ãŒã?ãƒ?‚£ãƒ¬ã‚¯ãƒˆãƒªã®å ´åˆã?ã€ãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã”ã¨ã‚³ãƒ”ã?ã—ã¾ã™ã?
897             * encode1ã€encode2 を指定ã™ã‚‹ã¨ã€ã‚¨ãƒ³ã‚³ãƒ¼ãƒ‰å¤‰æ›ã—ãªãŒã‚‰ã‚³ãƒ”ã?ã«ãªã‚Šã¾ã™ã?
898             * ã“ã?å ´åˆã?ã€ãƒ•ァイルåŒå£«ã®ã‚³ãƒ”ã?ã®ã¿ã«ãªã‚Šã¾ã™ã?
899             *
900             * @og.rev 4.0.0.0 (2007/11/28) メソãƒ?ƒ‰ã®æˆ»ã‚Šå?ã‚’ãƒã‚§ãƒ?‚¯ã—ã¾ã™ã?
901             * @og.rev 5.1.6.0 (2010/05/01) 引数ã®ä¸¦ã³é ??処ç?‚’変更ã—ã¾ã™ã?
902             *
903             * @param       args 引数é…å?  file1 file2 [encode1 encode2]
904             */
905            public static void main( final String[] args ) throws Throwable {
906                    if( args.length != 2 && args.length != 4 ) {
907                            LogWriter.log("Usage: java FileUtil <file1> <file2> [<encode1> <encode2>]" );
908                            return ;
909                    }
910    
911                    File file1 = new File( args[0] );
912                    File file2 = new File( args[1] );
913    
914                    File tempFile = new File( args[1] + "_backup" );
915    
916                    if( args.length < 3 ) {
917                            if( file1.isDirectory() ) {
918                                    FileUtil.copyDirectry( file1, file2, true );
919                            }
920                            else {
921                                    FileUtil.copy( file2,tempFile );
922                                    FileUtil.copy( file1,file2, true );
923                            }
924                    }
925                    else {
926                            String encode1 = args[2];
927                            String encode2 = args[3];
928    
929                            FileUtil.copy( file2,tempFile );
930    
931                            BufferedReader reader = FileUtil.getBufferedReader( file1 ,encode1 );
932                            PrintWriter    writer = FileUtil.getPrintWriter(    file2 ,encode2 );
933    
934                            try {
935                                    String line1;
936                                    while((line1 = reader.readLine()) != null) {
937                                            writer.println( line1 );
938                                    }
939                            }
940                            finally {
941                                    Closer.ioClose( reader ) ;
942                                    Closer.ioClose( writer ) ;
943                            }
944                    }
945            }
946    }