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);