文件和目录操作的思考练习(按指定path读写文件,四段小程序)
文本文件创建或追加(用Append)
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace FileReadWrite { public class Program { static void Main() { Console.WriteLine("请输入要创建的目录所在的驱动器名"); string driverName = Console.ReadLine(); Console.WriteLine("请输入要创建的目录名"); string directoryName = Console.ReadLine(); string path = driverName + ":\\" + directoryName; if (!Directory.Exists(path)) Directory.CreateDirectory(path); Console.WriteLine("请输入文件名(文本文件)"); string fileName = Console.ReadLine(); path += "\\" + fileName; Console.WriteLine("请输入文件文本内容,回车结束"); string contentVal = Console.ReadLine(); File.AppendAllText(path, contentVal);//新的内容可以不断添加到原文件中 Console.ReadLine(); } } }
按用户指定创建或追加文件
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace FileReadWrite { public class Program { static void Main() { Console.WriteLine("请输入要创建的目录所在的驱动器名"); string driverName = Console.ReadLine(); Console.WriteLine("请输入要创建的目录名"); string directoryName = Console.ReadLine(); string path = driverName + ":\\" + directoryName; if (!Directory.Exists(path)) Directory.CreateDirectory(path); Console.WriteLine("请输入文件名(文本文件)"); string fileName = Console.ReadLine(); path += "\\" + fileName; Console.WriteLine("请输入文件文本内容,回车结束"); string contentVal = Console.ReadLine(); string contentVal1 = "第1个附加buffer"; FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);//用FileMode.Append可以追加原有文件,Append 打开现有文件并查找到文件尾,或创建新文件。FileMode.Append 只能同 FileAccess.Write 一起使用。 byte[] byteBuffer = new UTF8Encoding(true).GetBytes(contentVal);//将字符串转换编码为一个字节序列。 byte[] byteBuffer1 = new UTF8Encoding(true).GetBytes(contentVal1); fs.Write(byteBuffer, 0, byteBuffer.Length);//Length为Array类的Length属性 fs.Write(byteBuffer1, 0, byteBuffer1.Length);//同样将缓冲中内容又往文件里录入了一次 contentVal1 = "附加buffer值第一次改变"; byteBuffer1 = new UTF8Encoding(true).GetBytes(contentVal1); fs.Write(byteBuffer1, 0, byteBuffer1.Length); fs.Flush();//清除该流的所有缓冲区,使得所有缓冲的数据都被写入到基础设备。基础设备指什么?此处存留疑问?????? Console.ReadLine(); } } }
按用户指定创建或改写文件
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace FileReadWrite { public class Program { static void Main() { Console.WriteLine("请输入要创建的目录所在的驱动器名"); string driverName = Console.ReadLine(); Console.WriteLine("请输入要创建的目录名"); string directoryName = Console.ReadLine(); string path = driverName + ":\\" + directoryName; if (!Directory.Exists(path)) Directory.CreateDirectory(path); Console.WriteLine("请输入文件名(文本文件)"); string fileName = Console.ReadLine(); path += "\\" + fileName; Console.WriteLine("请输入文件文本内容,回车结束"); string contentVal = Console.ReadLine(); string contentVal1 = "第1个附加buffer"; FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);//用FileMode.Create,只能创建新文件或重写源文件 byte[] byteBuffer = new UTF8Encoding(true).GetBytes(contentVal);//将字符串转换编码为一个字节序列。 byte[] byteBuffer1 = new UTF8Encoding(true).GetBytes(contentVal1); fs.Write(byteBuffer, 0, byteBuffer.Length);//Length为Array类的Length属性 fs.Write(byteBuffer1, 0, byteBuffer1.Length);//同样将缓冲中内容又往文件里录入了一次 contentVal1 = "附加buffer值第一次改变"; byteBuffer1 = new UTF8Encoding(true).GetBytes(contentVal1); fs.Write(byteBuffer1, 0, byteBuffer1.Length); fs.Flush();//清除该流的所有缓冲区,使得所有缓冲的数据都被写入到基础设备。基础设备指什么?此处存留疑问?????? Console.ReadLine(); } } }
按用户指定位置读文件
using System; using System.Text; using System.Collections.Generic; using System.IO; namespace FileReadWrite { public class Program { static void Main() { Console.WriteLine("请输入要读取的目录所在的驱动器名"); string driverName = Console.ReadLine(); Console.WriteLine("请输入要读取的目录名"); string directoryName = Console.ReadLine(); Console.WriteLine("请输入要读取的文件名(文本文件)"); string fileName = Console.ReadLine(); string path = driverName + ":\\" + directoryName + "\\" + fileName; if (!File.Exists(path)) Console.WriteLine("没有该文件!"); else { int byteNumber; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] byteBuffer = new byte[1000]; while ((byteNumber = fs.Read(byteBuffer, 0, byteBuffer.Length)) != 0) { Console.WriteLine(new UTF8Encoding(true).GetString(byteBuffer)); Array.Clear(byteBuffer, 0, byteBuffer.Length); } fs.Flush(); } Console.ReadLine(); } } }