我要从串口读数据,为了画图方便,需要把数据一行八个存到txt里面,用语句怎么实现呀?
用文件流不就行了吗,,,
///
/// 将数据保存到文件中
///
/// 文件路径
/// 文件名
/// 数据信息
internal static void SaveFile(string fileName, string msg)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string file = path + "\" + fileName;
if (!File.Exists(file))
{
FileStream fs = File.Create(file);
fs.Close();
}
using (StreamWriter sw = new StreamWriter(file, true))
{
sw.Write(msg);
}
}
Directory.Exists(path)和File.Exists(file)是判断路径以及文件是否存在;
using (StreamWriter sw = new StreamWriter(file, true))
{ sw.WriteLine(msg);//新写进一行数据
}这句是将字符串写进文件中,true是追加,false是覆盖
你对你的数据进行下处理就行了