public class TestMain {
// 폴더내 파일들을 모두 검색하여 원하는 결과 값 찾기
public static void main(String[] args) {
final String date = "2014-01-24";
String empno = "003135";
String desc = "연동 결과 >>> sn";
File dir = new File("C:/POWIS/workspace2_opr/S07020Build/buildfile/LOG1/app_debug_log");
File file = null;
if(dir.isDirectory()) {
File[] fList = dir.listFiles();
// Arrays.sort(fList, new Comparator<File>() {
//
// @Override
// public int compare(File o1, File o2) {
// return o1.lastModified() > o2.lastModified() ? 1 : -1;
// }
// });
ArrayList<String> fileList = new ArrayList<String>();
for(File f : fList) {
// System.out.println("File name >>> "+f.getName());
fileList = showFile(fileList, f, date, empno, desc);
}
Collections.sort(fileList, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int start1 = s1.indexOf(date);
int end1 = start1 + date.length()+ 13;
int start2 = s2.indexOf(date);
int end2 = start2 + date.length()+ 13;
// System.out.println(s1.substring(start1, end1));
return s1.substring(start1, end1).compareTo(s2.substring(start1, end1));
}
});
for(String s : fileList) {
System.out.println(s);
}
}
}
//파일 내용 조회 메서드
public static boolean showFile(File file, String date, String empno) {
boolean result = false; //완료여부
if( file.exists() ) { //복사 원본 파일 존재시
try
{
BufferedReader reader =
new BufferedReader(new FileReader(file));
String txt = "";
while((txt = reader.readLine()) != null)
{
if(txt.contains(date) && txt.contains(empno)) {
System.out.println(txt);
}
}
reader.close();
}catch(Exception e) {
System.out.println("오류");
}
}else { //복사 원본 파일 존재하지 않으면
System.out.println("잘못된 파일경로입니다.");
}
return result;
}//end copyFile()
//파일 내용 조회 메서드
public static ArrayList<String> showFile(ArrayList<String> list, File file, String date, String empno, String desc) {
if( file.exists() ) { //복사 원본 파일 존재시
try
{
BufferedReader reader =
new BufferedReader(new FileReader(file));
String txt = "";
while((txt = reader.readLine()) != null)
{
if(txt.contains(date) && txt.contains(empno) && txt.contains(desc)) {
list.add(txt);
// System.out.println(txt);
}
}
reader.close();
}catch(Exception e) {
System.out.println("오류");
}
}else { //복사 원본 파일 존재하지 않으면
System.out.println("잘못된 파일경로입니다.");
}
return list;
}//end copyFile()
'Dev. 자바 > 참고소스' 카테고리의 다른 글
[java 팁] split - pipeline(|)을 구분자로 문자열 쪼갤때 주의 (1) | 2014.06.11 |
---|---|
[reflection] 클래스(Class) 의 필드명, 값 map으로 가져오기 (0) | 2014.05.08 |
[Java 예제] SimpleDateFormat 간단한 사용 예제 (0) | 2014.01.08 |
sorted set 사용 예제 (0) | 2013.12.13 |
[java 예제] 정규식 실 사용 예제 (0) | 2013.12.06 |