using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace _5_4
{
class Program
{
private const string path = "E:\\test2.txt";
static void Main(string[] args)
{
StreamWriter sw = null;
try
{
sw = createfile();
writetext(sw);
readtext(sw);
}
catch (IOException e)
{
Console.WriteLine(e.StackTrace);
}
finally
{
if (sw != null)
sw.Close();
}
}
private static StreamWriter createfile()
{
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
return new StreamWriter(fs);
}
private static void writetext(StreamWriter sw)
{
sw.WriteLine("testing the streamwriter capabilities");
sw.WriteLine("writing bboolean {0}",true);
sw.WriteLine("writing float {0}",6.78);
sw.WriteLine("writing long {0}",4576457L);
sw.Write(445);
sw.WriteLine(new person("jack"));
sw.Flush();
}
private static void readtext(StreamWriter sw) //读取流中数据
{
StreamReader sr = new StreamReader(sw.BaseStream); //返回基础流,也不知道是什么意思??????懂得的说下啊
sr.BaseStream.Seek(0, SeekOrigin.Begin);
while (sr.Peek() > -1) //检测 下一个字符是否可用,但指针并不向后移动。如到末尾,则返回-1
{
Console.WriteLine(sr.ReadLine()); //读取一行后,指针向后移动,
}
}
}
class person
{
string name;
public person(string name) { this.name = name; }
}
}
评论