要提取单个属性而不是批量提取所有属性,请使用getAttribute()方法。
import java.io.IOException; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path;// 来自 时代Java - N o w J a v a . c o m import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; public class Main { public static void main(String[] args) { BasicFileAttributes attr = null; Path path = Paths.get("D:\\folder0\\Java\\JDK7\\folder5\\code\\folder1\\folder2\\folder6", "test.txt"); //extract a single attribute with getAttribute try { long size = (Long) Files.getAttribute(path, "basic:size", LinkOption.NOFOLLOW_LINKS);