Ruby:如何确定正在读取的文件是二进制文件还是文本文件 [英] Ruby: How to determine if file being read is binary or text

查看:47
本文介绍了Ruby:如何确定正在读取的文件是二进制文件还是文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Ruby编写一个程序,该程序将在目录内的文本文件中搜索字符串-与Grep类似.

I am writing a program in Ruby which will search for strings in text files within a directory - similar to Grep.

我不希望它尝试搜索二进制文件,但是我无法在Ruby中找到确定文件是二进制文件还是文本文件的方法.

I don't want it to attempt to search in binary files but I can't find a way in Ruby to determine whether a file is binary or text.

该程序需要同时在Windows和Linux上运行.

The program needs to work on both Windows and Linux.

如果有人能指出我正确的方向,那就太好了.

If anyone could point me in the right direction that would be great.

谢谢

Xanthalas

Xanthalas

推荐答案

gem install ptools
require 'ptools'
File.binary?(file)

这篇关于Ruby:如何确定正在读取的文件是二进制文件还是文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆