본문으로 바로가기
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class HangulwordCount {
 
    // 한글만 추출
    private static String getHangul(String str) {
        return str.replaceAll("[^\uAC00-\uD7AF\u1100-\u11FF\u3130-\u318F]", "");
    }
 
    // 디렉토리내 파일 추출
    private static String[] getFileList(File targetPath) {
        return targetPath.list();
    }
 
    //실행 메소드
    public static void main(String[] args) {
        String[] flist = getFileList(new File("C:\\test\\lang\\"));
        int textTotal = 0;
        for (int i = 0; i < flist.length; i++) {
 
            File file = new File("C:\\test\\lang\\"+flist[i]);
            String text = "";
            String textall = "";
            try {
 
                //BufferedReader br = new BufferedReader(new FileReader(file));
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
                while ((text = br.readLine()) != null) {
                    textall += getHangul(text);
                }
                br.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            textTotal += textall.length();
            System.out.println(flist[i]+" : "+textall.length());
        }
        System.out.println("File count : " + flist.length);
        System.out.println("total count : " + textTotal);
    }
 
}


'Programming > Java' 카테고리의 다른 글

스프링 get전송시 한글깨질때  (0) 2010.01.19
JSTL 간단한 표현목록  (0) 2009.12.16
Calendar 함수의 getActualMaximum과 getMaximum의 차이  (0) 2009.11.27
자바관련 블로그  (0) 2009.11.04
RMI 개념잡기  (0) 2009.10.16