FileStream myFile = new FileStream("123.txt", FileMode.OpenOrCreate);
FileStream myFile2 = new FileStream("456.txt", FileMode.OpenOrCreate);
//写的方法
// 1
//string sWrite = "hello";
//byte[] byteWrite = Encoding.Default.GetBytes(sWrite);
//myFile.Write(byteWrite, 0, byteWrite.Length);
//myFile.Flush(); //用于刷新 不使用此方法 可能写入不成功
// 2 StreamWriter
StreamWriter sw = new StreamWriter(myFile);
string sWrite2 = "hello2";
sw.Write(sWrite2);
sw.Flush();
//读的方法
// 1
// byte[] byteRead = new byte[myFile.Length];
////光标移动的两种方法
// //myFile.Position = 0; //把光标的位置移动到0
// myFile.Seek(0, SeekOrigin.Begin); //移动光标的位置
// myFile.Read(byteRead,0,byteRead.Length);
// string sRead = Encoding.Default.GetString(byteRead);
// Console.WriteLine(sRead);
// 2 StreamReader
myFile.Position = 0;
StreamReader sr = new StreamReader(myFile);
string sRead2 = sr.Read().ToString();
Console.WriteLine(sRead2);
//读一个写一个的方法
myFile.Position = 0;
int b = 0;
while ((b = myFile.ReadByte()) != -1)
{
myFile2.WriteByte((Byte)b);
myFile2.Flush();
}
myFile.Dispose(); //释放内存
myFile2.Dispose();
Console.ReadKey();
FileStream myFile2 = new FileStream("456.txt", FileMode.OpenOrCreate);
//写的方法
// 1
//string sWrite = "hello";
//byte[] byteWrite = Encoding.Default.GetBytes(sWrite);
//myFile.Write(byteWrite, 0, byteWrite.Length);
//myFile.Flush(); //用于刷新 不使用此方法 可能写入不成功
// 2 StreamWriter
StreamWriter sw = new StreamWriter(myFile);
string sWrite2 = "hello2";
sw.Write(sWrite2);
sw.Flush();
//读的方法
// 1
// byte[] byteRead = new byte[myFile.Length];
////光标移动的两种方法
// //myFile.Position = 0; //把光标的位置移动到0
// myFile.Seek(0, SeekOrigin.Begin); //移动光标的位置
// myFile.Read(byteRead,0,byteRead.Length);
// string sRead = Encoding.Default.GetString(byteRead);
// Console.WriteLine(sRead);
// 2 StreamReader
myFile.Position = 0;
StreamReader sr = new StreamReader(myFile);
string sRead2 = sr.Read().ToString();
Console.WriteLine(sRead2);
//读一个写一个的方法
myFile.Position = 0;
int b = 0;
while ((b = myFile.ReadByte()) != -1)
{
myFile2.WriteByte((Byte)b);
myFile2.Flush();
}
myFile.Dispose(); //释放内存
myFile2.Dispose();
Console.ReadKey();