using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*************************************************************************
* File 文件IO (有利于较小的文件整体读写操作)
* FileStream 文件流IO (有利于字节流操作)
* StreamReader/StreamWriter 文本流读写 (有利用文本文件字符串行流操作)
**************************************************************************/
namespace _002.FileInfoFileStream目录和文件IO示例
{
class Program
{
static void Main(string[] args)
{
//目录信息
Console.WriteLine("\n当前目录信息:--------------------------------");
DirectoryInfo directoryInfo = new DirectoryInfo("./");
string fullName = directoryInfo.FullName;
string name = directoryInfo.Name;
Console.WriteLine(fullName);
Console.WriteLine(name);
//目录下的文件信息
Console.WriteLine("\n当前目录下的每个文件信息:--------------------------------");
FileInfo[] fileInfos = directoryInfo.GetFiles();
foreach (FileInfo fileInfo in fileInfos)
{
Console.WriteLine(fileInfo.FullName);
Console.WriteLine(fileInfo.Name);
Console.WriteLine(fileInfo.Extension);
Console.WriteLine(fileInfo.Exists);
}
//目录下的文件夹信息
Console.WriteLine("\n当前目录下的每个文件夹信息:--------------------------------");
DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
Console.WriteLine(directoryInfos.Count());
foreach(DirectoryInfo directoryInfo1 in directoryInfos)
{
string fullName1 = directoryInfo1.FullName;
string name1 = directoryInfo1.Name;
Console.WriteLine(fullName1);
Console.WriteLine(name1);
}
string s = @"第一行 11111 111111 aaaa
第二行 asdf 卢卡斯京东方 laslkdfjasd
第三行 绿卡就是的了拉萨酱豆腐乐山大佛aaaa
第五行 laskdjfasldf
";
byte[] sb = Encoding.UTF8.GetBytes(s);
byte[] read = new byte[200];
//************************************************************************
//File 文件IO (有利于较小的文件整体读写操作)
//写
File.WriteAllText("newFile.txt",s); //会覆盖
File.AppendAllText("newFile.txt", s);
//读
string read2Str = File.ReadAllText("newFile.txt");
Console.WriteLine(read2Str);
//***********************************************************************
//FileStream 文件流IO (有利于字节流操作)
//写
FileStream fileStream = new FileStream("nameFile.txt",FileMode.Create); //会覆盖
fileStream.Write(sb,0,sb.Length);
fileStream.Close();
//读
FileStream fileStream1 = new FileStream("nameFile.txt", FileMode.Open);
int leng = fileStream1.Read(read,0,read.Length);
for (int i= 0 ;i < leng;i++) {
Console.WriteLine(read[i]);
Console.WriteLine(((char)read[i]));
}
fileStream1.Close();
//************************************************************************
//StreamReader/StreamWriter 文本流读写 (有利用文本文件字符串行流操作)
//写
StreamWriter streamWriter = new StreamWriter("3.txt"); //会覆盖
streamWriter.Write(s);
streamWriter.Close();
//读
StreamReader streamReader = new StreamReader("3.txt");
string s3 = streamReader.ReadLine(); //读一行
Console.WriteLine(s3);
streamReader.Read();
char char1 = (char)streamReader.Read(); //读一个字符,汉字也可以
Console.WriteLine(char1);
streamReader.Close();
Console.ReadKey();
}
}
}
002.FileInfoFileStream目录和文件IO示例.csproj
最新推荐文章于 2022-08-30 16:27:10 发布