您的位置:首页 > 移动开发 > 微信开发

文件和目录操作的思考练习(按指定path读写文件,四段小程序)

2009-10-02 22:29 447 查看
文本文件创建或追加(用Append)

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.IO;

namespaceFileReadWrite
{
publicclassProgram
{
staticvoidMain()
{
Console.WriteLine("请输入要创建的目录所在的驱动器名");
stringdriverName=Console.ReadLine();
Console.WriteLine("请输入要创建的目录名");
stringdirectoryName=Console.ReadLine();
stringpath=driverName+":\\"+directoryName;
if(!Directory.Exists(path))
Directory.CreateDirectory(path);
Console.WriteLine("请输入文件名(文本文件)");
stringfileName=Console.ReadLine();
path+="\\"+fileName;
Console.WriteLine("请输入文件文本内容,回车结束");
stringcontentVal=Console.ReadLine();
File.AppendAllText(path,contentVal);//新的内容可以不断添加到原文件中
Console.ReadLine();
}
}
}


按用户指定创建或追加文件

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.IO;

namespaceFileReadWrite
{
publicclassProgram
{
staticvoidMain()
{
Console.WriteLine("请输入要创建的目录所在的驱动器名");
stringdriverName=Console.ReadLine();
Console.WriteLine("请输入要创建的目录名");
stringdirectoryName=Console.ReadLine();
stringpath=driverName+":\\"+directoryName;
if(!Directory.Exists(path))
Directory.CreateDirectory(path);
Console.WriteLine("请输入文件名(文本文件)");
stringfileName=Console.ReadLine();
path+="\\"+fileName;
Console.WriteLine("请输入文件文本内容,回车结束");
stringcontentVal=Console.ReadLine();
stringcontentVal1="第1个附加buffer";
FileStreamfs=newFileStream(path,FileMode.Append,FileAccess.Write);//用FileMode.Append可以追加原有文件,Append打开现有文件并查找到文件尾,或创建新文件。FileMode.Append只能同FileAccess.Write一起使用。
byte[]byteBuffer=newUTF8Encoding(true).GetBytes(contentVal);//将字符串转换编码为一个字节序列。
byte[]byteBuffer1=newUTF8Encoding(true).GetBytes(contentVal1);
fs.Write(byteBuffer,0,byteBuffer.Length);//Length为Array类的Length属性
fs.Write(byteBuffer1,0,byteBuffer1.Length);//同样将缓冲中内容又往文件里录入了一次
contentVal1="附加buffer值第一次改变";
byteBuffer1=newUTF8Encoding(true).GetBytes(contentVal1);
fs.Write(byteBuffer1,0,byteBuffer1.Length);
fs.Flush();//清除该流的所有缓冲区,使得所有缓冲的数据都被写入到基础设备。基础设备指什么?此处存留疑问??????

Console.ReadLine();
}
}
}



按用户指定创建或改写文件

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.IO;

namespaceFileReadWrite
{
publicclassProgram
{
staticvoidMain()
{
Console.WriteLine("请输入要创建的目录所在的驱动器名");
stringdriverName=Console.ReadLine();
Console.WriteLine("请输入要创建的目录名");
stringdirectoryName=Console.ReadLine();
stringpath=driverName+":\\"+directoryName;
if(!Directory.Exists(path))
Directory.CreateDirectory(path);
Console.WriteLine("请输入文件名(文本文件)");
stringfileName=Console.ReadLine();
path+="\\"+fileName;
Console.WriteLine("请输入文件文本内容,回车结束");
stringcontentVal=Console.ReadLine();
stringcontentVal1="第1个附加buffer";
FileStreamfs=newFileStream(path,FileMode.Create,FileAccess.ReadWrite);//用FileMode.Create,只能创建新文件或重写源文件
byte[]byteBuffer=newUTF8Encoding(true).GetBytes(contentVal);//将字符串转换编码为一个字节序列。
byte[]byteBuffer1=newUTF8Encoding(true).GetBytes(contentVal1);
fs.Write(byteBuffer,0,byteBuffer.Length);//Length为Array类的Length属性
fs.Write(byteBuffer1,0,byteBuffer1.Length);//同样将缓冲中内容又往文件里录入了一次
contentVal1="附加buffer值第一次改变";
byteBuffer1=newUTF8Encoding(true).GetBytes(contentVal1);
fs.Write(byteBuffer1,0,byteBuffer1.Length);
fs.Flush();//清除该流的所有缓冲区,使得所有缓冲的数据都被写入到基础设备。基础设备指什么?此处存留疑问??????

Console.ReadLine();

}
}
}

按用户指定位置读文件

usingSystem;
usingSystem.Text;
usingSystem.Collections.Generic;
usingSystem.IO;

namespaceFileReadWrite
{
publicclassProgram
{
staticvoidMain()
{
Console.WriteLine("请输入要读取的目录所在的驱动器名");
stringdriverName=Console.ReadLine();
Console.WriteLine("请输入要读取的目录名");
stringdirectoryName=Console.ReadLine();
Console.WriteLine("请输入要读取的文件名(文本文件)");
stringfileName=Console.ReadLine();
stringpath=driverName+":\\"+directoryName+"\\"+fileName;
if(!File.Exists(path))
Console.WriteLine("没有该文件!");
else
{
intbyteNumber;
FileStreamfs=newFileStream(path,FileMode.Open,FileAccess.Read);
byte[]byteBuffer=newbyte[1000];
while((byteNumber=fs.Read(byteBuffer,0,byteBuffer.Length))!=0)
{
Console.WriteLine(newUTF8Encoding(true).GetString(byteBuffer));
Array.Clear(byteBuffer,0,byteBuffer.Length);
}
fs.Flush();
}

Console.ReadLine();

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: