在Java 7中增加了新的一个方法——probeContentType,其主要作用是可以判断文件的content type。相应代码如下所示:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileContentType {
public static void main(String[] args) { printContentType("D:/Downloads/java.txt");
printContentType("D:/Downloads/java.ppt");
printContentType("D:/Downloads/java.doc");
printContentType("D:/Downloads/java.avi");
}
private static void printContentType(String pathToFile) { Path path = Paths.get(pathToFile);
String contentType = null;
try {
contentType = Files.probeContentType(path);
} catch (IOException e) { e.printStackTrace();
}
System.out.println("File content type is : " + contentType);
}
}

在C#中没有对应的方法,不过要实现同样的功能并不困难。

using System;
using System.IO;
using Microsoft.Win32; namespace Demo
{
class Program
{
static void Main(string[] args)
{
PrintContentType("D:/Downloads/java.txt");
PrintContentType("D:/Downloads/java.ppt");
PrintContentType("D:/Downloads/java.doc");
PrintContentType("D:/Downloads/java.jar"); Console.ReadKey();
} private static void PrintContentType(string pathToFile)
{
string result = string.Empty;
string ext = Path.GetExtension(pathToFile);
using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(ext))
{
if (registryKey != null)
{
var value = registryKey.GetValue("Content Type");
result = value == null ? "null" : value.ToString();
}
}
Console.WriteLine(result);
}
}
}

因为相关的content type信息其实都可以从注册表中取得,故而只需要对注册表做一点操作,即可以达到同样的功能。

原文同步发布于我的个人博客

Java 7新方法probeContentType的C#实现方式的更多相关文章

  1. Java调用WebService方法总结(9,end)--Http方式调用WebService

    Http方式调用WebService,直接发送soap消息到服务端,然后自己解析服务端返回的结果,这种方式比较简单粗暴,也很好用:soap消息可以通过SoapUI来生成,也很方便.文中所使用到的软件版 ...

  2. Java 使用新方法打印Word文档

    前言 我曾写过一篇文章,它主要介绍了如何通过物理打印机和虚拟打印机来打印Word文档.今天这篇教程将介绍一种新的方法来实现对Word文档的打印. 此次使用到的类库仍然是Spire.Doc for Ja ...

  3. Java项目main方法启动的两种方式

    1.打包时指定了主类,可以直接用java -jar xxx.jar. <!--main方法打包jar包插件--> <plugin> <artifactId>mave ...

  4. Java调用WebService方法总结(1)--准备工作

    WebService是一种跨编程语言.跨操作系统平台的远程调用技术,已存在很多年了,很多接口也都是通过WebService方式来发布的:本系列文章主要介绍Java调用WebService的各种方法,使 ...

  5. Java 8新特性-4 方法引用

    对于引用来说我们一般都是用在对象,而对象引用的特点是:不同的引用对象可以操作同一块内容! Java 8的方法引用定义了四种格式: 引用静态方法     ClassName :: staticMetho ...

  6. Java 8新特性——default方法(defender方法)介绍

    我们都知道在Java语言的接口中只能定义方法名,而不能包含方法的具体实现代码.接口中定义的方法必须在接口的非抽象子类中实现.下面就是关于接口的一个例子: 1 2 3 4 5 6 7 8 9 10 11 ...

  7. Java 8新特性探究(一) JEP126特性lambda表达式和默认方法

    Lambda语法 函数式接口 函数式接口(functional interface 也叫功能性接口,其实是同一个东西).简单来说,函数式接口是只包含一个方法的接口.比如Java标准库中的java.la ...

  8. Java 8新特性探究(二)深入解析默认方法

    什么是默认方法,为什么要有默认方法 简单说,就是接口可以有实现方法,而且不需要实现类去实现其方法.只需在方法名前面加个default关键字即可. 为什么要有这个特性?首先,之前的接口是个双刃剑,好处是 ...

  9. Java 8 新特性-菜鸟教程 (4) -Java 8 默认方法

    Java 8 默认方法 Java 8 新增了接口的默认方法. 简单说,默认方法就是接口可以有实现方法,而且不需要实现类去实现其方法. 我们只需在方法名前面加个default关键字即可实现默认方法. 为 ...

随机推荐

  1. C# 委托的学习

    delegate int GetCalculatedValueDelegate(int x, int y);    //定义是个委托实际上就是抽象一类  参数列表形式和返回值相同的函数AddCalcu ...

  2. 平行四边形TikZ作图

    %!TEX program = pdflatex \documentclass[varwidth=true, border=2pt]{standalone} \usepackage{tikz} \us ...

  3. Windows下使用性能监视器监控SqlServer的常见指标

    这篇文章主要介绍了Windows下使用性能监视器监控SqlServer的常见指标,常见指标包括Buffer Cache Hit Ratio.Pages/sec. Available Bytes.Dis ...

  4. Java interview Advanced

    1. Can you override private or static method in Java ? Read more: http://java67.blogspot.com/2012/09 ...

  5. step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework

    文章内容概述: spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of mater ...

  6. 转贴 IT外企那点儿事完整版

    转贴 IT外企那点儿事完整版 第一章:外企也就那么回儿事(http://www.cnblogs.com/forfuture1978/archive/2010/04/30/1725341.html) 1 ...

  7. Windows下MongoDB环境搭建

    MongoDB下载 登录MongoDB官网:www.mongodb.org:点击[Download MongoDB]按钮,进入如下所示界面 选择目标操作系统及其版本,比如这里选择的是64位的Windo ...

  8. POJ3694 Network

    题目大意:已知连通图G有N个点m条无向边,有Q次操作,每次操作为增加一条边,问每次操作后图上有几个桥. 如果添加一条边进行Tarjin搜索一次时间复杂度为m*m*q很大,会超时.真的超时,我试过.看了 ...

  9. 2016中国大学生程序设计竞赛(长春)-重现赛 1010Ugly Problem 回文数 模拟

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  10. Excel表格常用的函数,留着备用

    1. vlookup(lookup_value, table_array, col_index_num, boolean) -- 查找匹配函数 lookup_value: 你要去匹配的值 table_ ...