OSDN Git Service

2.102.3-SNAPSHOT 開発開始
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / binio / FileUtils.java
1 /*
2  * file utils
3  *
4  * License : The MIT License
5  * Copyright(c) 2011 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.binio;
9
10 import java.io.File;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.nio.channels.FileChannel;
14
15 /**
16  * ファイルユーティリティ。
17  */
18 public final class FileUtils {
19
20     /**
21      * コンストラクタ。
22      */
23     private FileUtils(){
24         super();
25         assert false;
26         throw new AssertionError();
27     }
28
29
30     /**
31      * 既に存在する通常ファイルか否か判定する。
32      * @param file 判定対象
33      * @return 既に存在する通常ファイルならtrue
34      */
35     public static boolean isExistsNormalFile(File file){
36         if( ! file.exists() ) return false;
37         if( ! file.isFile() ) return false;
38         return true;
39     }
40
41     /**
42      * 既に存在する特殊ファイルか否か判定する。
43      * @param file 判定対象
44      * @return 既に存在する特殊ファイルならtrue
45      */
46     public static boolean isExistsUnnormalFile(File file){
47         if( ! file.exists() ) return false;
48         if( file.isFile() ) return false;
49         return true;
50     }
51
52     /**
53      * ファイルサイズを0に切り詰める。
54      * <p>既に存在する通常ファイルでないならなにもしない。
55      * @param file ファイル
56      * @throws IOException 入出力エラー
57      */
58     public static void trunc(File file) throws IOException{
59         if( ! isExistsNormalFile(file) ) return;
60         if(file.length() <= 0L) return;
61
62         FileOutputStream foStream = new FileOutputStream(file);
63         try{
64             FileChannel channnel = foStream.getChannel();
65             try{
66                 channnel.truncate(0L);
67             }finally{
68                 channnel.close();
69             }
70         }finally{
71             foStream.close();
72         }
73
74         assert file.length() <= 0L;
75
76         return;
77     }
78
79 }