2013-06-06 6 views
1

Files 클래스의 readAttributes 메서드를 사용하여 모든 파일 특성에 즉시 액세스하는 방법을 알지 못합니다. 내 문제는 메서드를 호출하지 않고 대량으로 모든 특성을 인쇄하려고한다는 것입니다.readAttributes 메서드를 사용하는 방법?

나는 다음과 같이 하나의 방법 하나를 호출하지 않으 :

Path file = ...; 
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); 

System.out.println("creationTime: " + attr.creationTime()); 
System.out.println("lastAccessTime: " + attr.lastAccessTime()); 
System.out.println("lastModifiedTime: " + attr.lastModifiedTime()); 

System.out.println("isDirectory: " + attr.isDirectory()); 
System.out.println("isOther: " + attr.isOther()); 
System.out.println("isRegularFile: " + attr.isRegularFile()); 
System.out.println("isSymbolicLink: " + attr.isSymbolicLink()); 
System.out.println("size: " + attr.size()); 

나는 한 번에 모든 BasicFileAttribute 접근하고 싶습니다. readAttributes를 사용하여 한 번에 모든 속성에 액세스 할 수없는 경우이를 수행 할 수있는 다른 방법이 있습니다.

+0

는 * 그런 식으로 작업을하고 그렇습니다 그래서 우리 대부분. 왜 당신이 * 하나씩 메서드를 호출하고 싶지 않은지 명시 적으로 말할 수 있습니까? 이들 메소드는 단순한 getter이고 이미 가져온 값을 반환하는 디스크 IO을 수행하지 않습니다. 그래서 문제는 무엇입니까? – inquisitive

+0

'System.out.println (attr);'을 사용해보십시오. 당신이 그것을 좋아하지 않는다면 당신은 스스로 문자열을 형성해야하지만 여전히 모든 방법을 호출해야합니다. –

+0

시도해 보았지만 "[email protected]"@Bhesh – javadb9

답변

1

이이 클래스 BasicFileAttributes 클래스의 readAttributes() 방법을 사용하는 방법의 또 다른 예입니다

import java.nio.file.*; 
import java.nio.file.attribute.BasicFileAttributes; 
import java.io.*; 
import java.util.Map; 

import static java.nio.file.StandardCopyOption.*; 

public class ListOfNumbers { 

    public static void main(String[] args) throws IOException { 
     Path path = Paths.get("C:\\Documents and Settings\\Administrator\\Desktop\\kk.txt"); 
     Map f = Files.readAttributes(path, "*"); 
     System.out.println(f); 
    } 
} 
0

작동합니다 : 프레임 워크가 * 설계되었습니다

Path path = Paths.get("C:\\Users\\jorgesys\\workspaceJava\\myfile.txt"); 
    BasicFileAttributes attr; 
    try { 
    attr = Files.readAttributes(path, BasicFileAttributes.class); 
    System.out.println("Creation time: " + attr.creationTime()); 
    System.out.println("Last access time: " + attr.lastAccessTime()); 
    System.out.println("Last modified time: " + attr.lastModifiedTime()); 
    } catch (IOException e) { 
    System.out.println("oops un error! " + e.getMessage()); 
    } 
관련 문제