본문 바로가기

Java

[JAVA] 파일 유틸 클래스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package common.util;
import java.io.File;
import java.util.ArrayList;
 
public class FileUtil
{
//디렉토리의 모든 파일정보를 List에 담음
 public static void visitAllDirectory(ArrayList<File> files, File dir)
 {
        if(dir.isDirectory())
        {
            File[] children = dir.listFiles();
            
            for(File f : children)
            {
             visitAllDirectory(files, f);
            }
        }
        else
        {
            files.add(dir);
        }
    }
 //확장자 체크
  public static boolean isBadExtension(File file, String[] badExtension)
  {
        String fileName = file.getName();
        String ext = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
        
        for (String mExt : badExtension)
        {
            if (ext.equalsIgnoreCase(mExt))
            {
             return true;
            }
        }
        
        return false;
  }
  //확장자 변경
  public static String changeExtension(String fileName, String aftExt)
  {
   String befExt = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
   String extStr = fileName.replace(befExt, aftExt);
   
   return extStr;
  }
}
 
cs