• 周五. 7月 1st, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

FileStream 数据的写入和读取

admin

11月 28, 2021

using System;
using System.IO;
using System.Text;

namespace Demo.App
{
class Program
{
static void Main(string[] args)
{

string Sourcepath = System.AppDomain.CurrentDomain.BaseDirectory + “\temp\” + “调解笔录.docx”;
string savePath = System.AppDomain.CurrentDomain.BaseDirectory + “\temp\” + “调解笔录_” + DateTime.Now.ToString(“yyyyMMddHHmmssfff”) + “.docx”;
var tempByte = File.ReadAllBytes(Sourcepath);
WriteByteToWord(tempByte, savePath);
Console.ReadLine();
}

/// <summary>
/// 数据写入文件
/// </summary>
/// <param name=”tempByte”></param>
/// <param name=”savePath”>保存的路径</param>
static void WriteByteToWord(byte[] tempByte, string savePath)
{

try
{
using (FileStream fs = new FileStream(savePath, FileMode.Create))
{
byte[] buffer = tempByte;
fs.Write(buffer, 0, buffer.Length);
}
}
catch (Exception ex)
{

Console.WriteLine(“写入异常错误:” + ex.Message);

}

}

/// <summary>
/// 数据从文件读取
/// </summary>
/// <param name=”SourcePath”>文件路径</param>
static void ReadDataByPath(string SourcePath,out string msg)
{

try
{
using (FileStream fs = new FileStream(SourcePath, FileMode.Open))
{
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
msg = sr.ReadToEnd();
}
}
}
catch (Exception ex)
{
msg = “”;
Console.WriteLine(“读取数据异常:” + ex.Message);
}

}
}
}

本文来自博客园,作者:.net&new,转载请注明原文链接:https://www.cnblogs.com/wugh8726254/p/15150132.html

发表评论

您的电子邮箱地址不会被公开。