public class Sample1 {
public static void main(String[] args) {
/*
* 상대 경로 : ./ -> 현재 (프로그램이 동작하는) 위치를 의미한다.
* ../ -> 현재 위치의 상위 디렉터리를 의미한다.
* ./ , ../ 생략하고 바로 디렉터리명/파일명을 사용
* 절대 경로 : Windows -> C:\ 부터 모든 경로를 작성하는 것
* MacOs/Linux -> / 부터 모든 경로를 작성하는 것
*/
File f = new File("config/"); //경로에 파일 생성(상대경로 생략한 것)
System.out.println(f.getAbsolutePath()); //전체 경로 메서드 출력
// 경로.exists() -> 경로에 file/directory(folder)가 존재하는지 확인한다.
if(!f.exists()) { //!if로 없으면 알아서 디렉토리 만들게 처리하는 간편한 방법
f.mkdirs();
}
f = new File("config/conf.cnf");
try {
f.createNewFile();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//복잡한 방법 트라이캐치 여러번 쓰는 거 비추
// try { //config 디렉토리 없으므로 에러 뜰 것
// f.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// f.mkdirs(); //config폴더 만들어야함 해당 위치에 디렉터리를 만드는 명령어
// try { //뉴파일해서 트라이캐치또한것
// f.createNewFile();
// } catch (IOException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
// e.printStackTrace(); //에러 잡아서 출력
// }
f = new File("./");
String[] fList = f.list(); //현재 경로의 리스트 목록 뽑아내기
System.out.println(Arrays.toString(fList));
//파일 바로 아래에 있는 file list를 배열로 리턴
File files[] = f.listFiles();
for(int i = 0; i < fList.length; i++) {
System.out.println("file = " + files[i]);
}
//필터링해서 골라내기 .포함된 것만 프린트
String[] sList = f.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.contains(".");
}
});
System.out.println(Arrays.toString(sList));
}
}