您的位置:首页 > 编程语言 > C#

C# 读写文件

2016-05-25 10:42 323 查看
读文件到byte[]的函数:

public static bool readFile(string filePathName, out byte[] bytes)
{
FileStream stream = new FileStream(filePathName, FileMode.Open);
bool ret = false;
bytes = null;
if (null != stream)
{
int len = (int)stream.Length;
bytes = new byte[len];
int readLend = stream.Read(bytes, 0, len);
stream.Flush();
stream.Close();
ret = readLend == len;
}
return ret;
}

写byte[]到文件

private void write2File(string filePathName, byte[] bytes)
{
if(File.Exists(filePathName))
{
File.Delete(filePathName);
}
FileStream stream = new FileStream(filePathName, FileMode.Create);
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
stream.Close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: