package com.local.java.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class FileReadTest {
private void fileRead(File readFile) {
try {
File fileName = null;
String []fileList = readFile.list();
FileReader fr = null;
BufferedReader br = null;
//가나다순 정렬 ----
Arrays.sort(fileList);
String sortList = Arrays.toString(fileList);
sortList = sortList.replace("[","");
sortList = sortList.replace("]","");
StringTokenizer strToken = new StringTokenizer(sortList, ",");
int token = 0;
while(strToken.hasMoreTokens()) {
fileList[token++] = strToken.nextToken().trim();
}
----//
for(int i=0; i<fileList.length; i++) {
fileName = new File(readFile.getAbsolutePath() + "/" + fileList[i]);
if(!fileName.isDirectory()) {
if(fileList[i].substring(fileList[i].lastIndexOf(".")).toUpperCase().equals(".TXT")) {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String ch = "";
StringBuilder strSql = new StringBuilder();
while((ch=br.readLine()) != null) {
if(ch == null) {
System.out.println("break");
break;
}else {
strSql.append(ch);
strSql.append("\n");
}
}
System.out.println(strSql.toString());
}
}else {
System.out.println(fileName);
fileRead(fileName); //디렉토리일때 재귀호출로 디렉토리 안의 파일도 수행.
}
}
}catch(Exception e) {
}
}
public static void main(String[] args) {
FileReadTest fr = new FileReadTest();
fr.fileRead(new File("d:/test"));
}
}
'Java' 카테고리의 다른 글
[Java] Pass by Value (0) | 2009.07.06 |
---|---|
[Java] 가나다순, 숫자 정렬 (0) | 2009.07.03 |
[Java] 이클립스 Tab -> Space변환 (0) | 2009.06.08 |
[Java] StringBuffer와 StringBuilder 테스트 (0) | 2009.06.01 |
[Java] 객체직렬화란?? (0) | 2009.05.27 |