集册 Java实例教程 用于获取文件和目录信息的文件类。

用于获取文件和目录信息的文件类。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

536
用于获取文件和目录信息的文件类。

import java.io.IOException;

import java.nio.file.DirectoryStream;/** 来 自 N o  w  J a v a . c o m - 时  代  Java**/

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.util.Scanner;


public class Main

{

   public static void main(String[] args) throws IOException

   {

      Path path = Paths.get("c:/");


      if (Files.exists(path)) // if path exists, output info about it

      {

         // display file (or directory) information

        System.out.printf("%n%s exists%n", path.getFileName());/*n o w  j a v a  . c o m 提 供*/

        System.out.printf("%s a directory%n", 

          Files.isDirectory(path) ? "Is" : "Is not");

        System.out.printf("%s an absolute path%n", 

          path.isAbsolute() ? "Is" : "Is not");

        System.out.printf("Last modified: %s%n", 

          Files.getLastModifiedTime(path));

        System.out.printf("Size: %s%n", Files.size(path));

        System.out.printf("Path: %s%n", path);

        System.out.printf("Absolute path: %s%n", path.toAbsolutePath());


         if (Files.isDirectory(path)) // output directory listing

         {

            System.out.printf("%nDirectory contents:%n");

            

            // object for iterating through a directory's contents

            DirectoryStream<Path> directoryStream = 

               
展开阅读全文