usingSystem.Collections.Generic;namespacedemo1;usingSystem.IO;/// <summary>/// System.IO下的所有的Stream類是所有數(shù)據(jù)流的基類/// 流是用于傳輸數(shù)據(jù)的對象,流就是用來傳輸數(shù)據(jù)的/// 數(shù)據(jù)傳輸?shù)膬煞N方式:1、數(shù)據(jù)從外部源傳輸?shù)匠绦蛑?#xff0c;這種叫做讀取流,2、數(shù)據(jù)從程序中傳輸?shù)酵獠吭?#xff0c;這種叫做寫入流/// 流一般具有三種操作:/// 讀取操作:讀出流對象中的數(shù)據(jù),并且把它存放在另外一個數(shù)據(jù)結(jié)構(gòu)中/// 寫入操作:從一種數(shù)據(jù)結(jié)構(gòu)中讀取數(shù)據(jù)并且存放在流對象中/// 搜索操作:從流中當(dāng)前位置搜索到指定位置/// </summary>classproj{internalstaticvoidMain(string[] args){Console.WriteLine("二進(jìn)制文件的寫入");Console.WriteLine("請輸入文件名");string path=Console.ReadLine();//初始化FileStream對象FileStream fs=newFileStream(path, FileMode.OpenOrCreate);//初始化一個BinaryWriter對象BinaryWriter bw=newBinaryWriter(fs);int a =40;double b =3.14;bool c =true;string d ="hello world";//寫入文件bw.Write(a);bw.Write(b);bw.Write(c);bw.Write(d);Console.WriteLine("成功寫入");bw.Close();//關(guān)閉BinaryWriter對象fs.Close();//關(guān)閉文件流Console.WriteLine("二進(jìn)制文件的讀取");BinaryReader br=newBinaryReader(newFileStream(path,FileMode.Open));int e = br.ReadInt32();Console.WriteLine("int 型整型數(shù)據(jù)\t{0}",e);double f = br.ReadDouble();Console.WriteLine("double 數(shù)據(jù) \t{0}",f);bool g = br.ReadBoolean();Console.WriteLine("bool 數(shù)據(jù) \t{0}", g);string h = br.ReadString();Console.WriteLine("字符串類型數(shù)據(jù)\t{0}", h);br.Close();Console.WriteLine("讀取完成");}}
c#遍歷文件夾
usingSystem.Collections.Generic;namespacedemo1;usingSystem.IO;usingSystem.Drawing;classproj{internalstaticvoidMain(string[] args){DirectoryInfo dir =newDirectoryInfo("E:\\Desktop\\c#\\data");FileSystemInfo[] fs=dir.GetFileSystemInfos();foreach(FileSystemInfo i in fs){if( i isDirectoryInfo){Console.WriteLine("是文件夾{0}",i.FullName);string[] a=Directory.GetFiles(i.FullName);foreach(string s in a){ Console.WriteLine("文件:{0}",s);}}else{Console.WriteLine("不是文件夾{0}",i.FullName);FileStream fb=File.OpenRead("E:\\Desktop\\c#\\data\\data\\apple_1.jpg");int file_lenth=(int)fb.Length;Byte[] image =newByte[file_lenth];//建立一個字節(jié)數(shù)組fb.Read(image,0, file_lenth );//按字節(jié)流讀取}}}}