如何确定文件是否是Eclipse中的文本文件

时间:2016-03-15 18:07:12

标签: eclipse-plugin

鉴于文件资源,我想告诉它的内容类型是否基于文本,即不是二进制文件。

IFile file = ...
IContentType contentType = IDE.getContentType( file );
boolean textBased = ...

1 个答案:

答案 0 :(得分:1)

可以通过IContentTypeManager.CT_TEXT常量获得预定义的文本内容类型。 一种此内容类型的所有内容类型都是基于文本的内容类型。

IFile file = ...
IContentType contentType = IDE.getContentType( file );
IContentType textContentType = Platform.getContentTypeManager().getContentType( IContentTypeManager.CT_TEXT );
boolean textBased = contentType.isKindOf( textContentType );

或者,ITextFileBufferManager可以确定路径是否指向文本文件。

ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
boolean textBased = manager.isTextFileLocation( "/path/to/file",  false );

该位置可以是工作空间资源的完整路径,也可以是本地文件系统中的绝对路径。第二个参数确定是否应将具有未知内容类型的文件视为文本文件(true)或不(false)。

相关问题