判断文件是否文本文件还是二进制文件的两个有效方法
2012-03-03 00:31
387 查看
/// <summary>
/// 判断文件是否是二进制文件
/// </summary>
/// http://www.xuehai.net/
/// <param name="filePath">文件路径</param>
/// <returns>返回True为二进制文件,否则是文本文件</returns>
public bool IsBinaryFile(string filePath)
{
FileStream fs = File.OpenRead(filePath);
int len = (int)fs.Length;
int count = 0;
byte[] content = new byte[len];
int size = fs.Read(content, 0, len);
for (int i = 0; i < size; i++)
{
if (content[i] == 0)
{
count++;
if (count == 4)
{
return true;
}
}
else
{
count = 0;
}
}
return false;
}
/// <summary>
/// 判断文件是否是文本文件
/// </summary>
/// http://www.xuehai.net/
/// <param name="filePath">文件路径</param>
/// <returns>返回True为文本文件,否则是二进制文件</returns>
public bool IsTextFile(string filePath)
{
FileStream file = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] byteData = new byte[1];
while (file.Read(byteData, 0, byteData.Length) > 0)
{
if (byteData[0] == 0)
return false;
}
return true;
}
/// 判断文件是否是二进制文件
/// </summary>
/// http://www.xuehai.net/
/// <param name="filePath">文件路径</param>
/// <returns>返回True为二进制文件,否则是文本文件</returns>
public bool IsBinaryFile(string filePath)
{
FileStream fs = File.OpenRead(filePath);
int len = (int)fs.Length;
int count = 0;
byte[] content = new byte[len];
int size = fs.Read(content, 0, len);
for (int i = 0; i < size; i++)
{
if (content[i] == 0)
{
count++;
if (count == 4)
{
return true;
}
}
else
{
count = 0;
}
}
return false;
}
/// <summary>
/// 判断文件是否是文本文件
/// </summary>
/// http://www.xuehai.net/
/// <param name="filePath">文件路径</param>
/// <returns>返回True为文本文件,否则是二进制文件</returns>
public bool IsTextFile(string filePath)
{
FileStream file = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] byteData = new byte[1];
while (file.Read(byteData, 0, byteData.Length) > 0)
{
if (byteData[0] == 0)
return false;
}
return true;
}
相关文章推荐
- 检测一个文件是否为文本文件/二进制文件的方法
- Linux 判断是否是一个目录还是一个文件的方法
- 判断文件是二进制文件还是文本文件的函数(VC,Delphi)
- 判断文件是二进制文件还是文本文件的函数(VC,Delphi)
- 判断文件是二进制文件还是文本文件的函数(VC,Delphi)
- Unix学习笔记-------目录遍历实例----Linux下判断是否是一个目录还是一个文件的方法
- Linux 判断是否是一个目录还是一个文件的方法
- C# 如何判断两个文件内容是否相同的方法
- 嵌入式 Linux下判断是否是一个目录还是一个文件的方法
- Java 判断两个文件是否相同的两种方法
- DICOM文件中读取对齐方式以及判断元素是否为显示VR还是隐式VR的方法
- VC 判断文件是否存在的几种方法
- JS 数组的删除与判断是否存在的两个扩展方法
- JS判断两个对象内容是否相等的方法示例及开发面试题汇总(图)
- C# 判断文件是否文本文件
- Java--判断两个文件是否相同
- 用Java判断一个URL是否有效的两种方法
- Ubuntu下判断文件是否存在的方法
- 断文件是二进制文件还是文本文件的函数
- Python 判断文件是否存在的三种方法