【Java-파일】파일 정보보기

【Java-파일】파일 정보보기

1. 설명

이 포스트의 예제는 파일이나 디렉토리의 정보를 Map에 담아서 가져오는 코드입니다.

2. 소스코드

- 메서드

public Map getFileInfo(String path) { File target = new File(path); if ( !target.exists() ) { return null; // 복사할 파일이나 디렉토리가 없다면 } Map map = new HashMap(); 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 map = fu.getFileInfo(path); for (Entry 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. 전체코드

https://github.com/leeyoungseung/template-java

from http://koiking.tistory.com/34 by ccl(A) rewrite - 2021-09-13 02:00:57