프로그래밍/Java-자주쓰는예제
【Java-파일】파일 정보보기
코이킹
2021. 9. 13. 01:32
반응형
1. 설명
이 포스트의 예제는 파일이나 디렉토리의 정보를 Map에 담아서 가져오는 코드입니다.
2. 소스코드
- 메서드
public Map<String, String> getFileInfo(String path) {
File target = new File(path);
if ( !target.exists() ) {
return null; // 복사할 파일이나 디렉토리가 없다면
}
Map<String, String> map = new HashMap<String, String>();
try {
map.put("canonicalPath", target.getCanonicalPath());
map.put("absolutePath", target.getAbsolutePath());
String filename = target.getName();
map.put("name", target.getName());
map.put("extension", filename.substring(filename.lastIndexOf(".") + 1));
map.put("parent", target.getParent());
map.put("path", target.getPath());
map.put("permission", String.valueOf(checkPermission(path)));
map.put("lastModified", ""+target.lastModified());
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
- 메인
public class File_09_GetInfo {
public static void main(String[] args) {
try {
for (String str : args ) {
System.out.println("Param : "+str);
}
String path = args[0];
FileUtil fu = new FileUtil();
Map<String, String> map = fu.getFileInfo(path);
for (Entry<String, String> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() +
" ,Value : " + entry.getValue());
}
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
3. 실행결과【Windows(이클립스) / Linux】
4. 전체코드
반응형