C# 文件读写

 

 

1.文本文件读写

 

//
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs, Encoding.Unicode);

while ((readStr = reader.ReadLine()) != null)
{
    string str = readStr.ToString();
}

fs.Close();
reader.Close();

 

 

//
StreamWriter writer;
writer = new StreamWriter(filePath, false, Encoding.Unicode);
string str = "hello";
writer.WriteLine((string)str);
writer.Flush();
writer.Close();

 

 

 

2.二进制文件读写

 

//读
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs, Encoding.Unicode); try
{   while (true)   {     string str= br.ReadString();   } } catch (EndOfStreamException ex) {   Debug.WriteLine("读取完毕"); }

 

//
FileStream fs;
BinaryWriter bw;
fs = new FileStream(filePath,FileMode.Create);
bw = new BinaryWriter(fs, Encoding.Unicode);
string str = "hello";
bw.Write((string)str);

 

posted @ 2016-07-28 22:23  Ficow  阅读(276)  评论(0编辑  收藏  举报