集册 Java实例教程 从Files.probeContentType获取文件类型

从Files.probeContentType获取文件类型

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

776
从Files.probeContentType获取文件类型

import java.nio.file.*;

import java.io.IOException;
/*来自 
 n o w    j a v a  . c o m*/


public class FileType {

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

        if (args.length == 0) {

            System.err.println("usage: java FileType file...");

            System.exit(-1);

        }

        for (String arg : args) {

            Path file = Paths.get(arg);

            String type;

            if (Files.isDirectory(file)) {

                type = "directory";/*来 自 时 代 J a v a - N o w J a v a . c o m*/

            } else {

                type = Files.probeContentType(file);

                if (type == null)

                    type = "<not recognized>";

            }

            System.out.format("%s\t%s%n", file, type);

        }

    }

}


展开阅读全文