.net文件流操作

		//using System.IO;
                //using System.IO.Compression;
                private void button1_Click(object sender, EventArgs e)
		{
			Txt txt = new Txt();
			txt.title = textBox1.Text;
			txt.detail = textBox2.Text;

			//创建存储的文件流
			BinaryFormatter bf = new BinaryFormatter();
			using (FileStream fsWrite = new FileStream("txt.bin", FileMode.Create)) {
				bf.Serialize(fsWrite, txt);
			}

			BinaryFormatter binary = new BinaryFormatter();
			//创建读取文件的文件流
			using (FileStream fsRead = new FileStream("txt.bin", FileMode.Open))
			{
				//执行反序列化
				object obj = binary.Deserialize(fsRead);
				Txt pr = obj as Txt;
				//listBox1.Items.Add(pr.title);

			}

			//文件流的使用方式
			string msg = "";
			//将字符串转换成数组
			byte[] bytes = System.Text.Encoding.UTF8.GetBytes(msg);
			//把一个byte数组转换为一个字符串
			string newmsg = System.Text.Encoding.UTF8.GetString(bytes);
			//流操作的是字节,不能直接操作字符串


			//通过fileStream来写文件
			//文件流的使用步骤
			FileStream fsWriteStream = new FileStream("first.txt", FileMode.Create, FileAccess.Write);
			//使用文件流,执行读写操作
			string msg2 = "sadkjfkjsaflkjas,三角阀";
			byte[] byts = System.Text.Encoding.UTF8.GetBytes(msg2);
			//写
			//参数1:将指定的字节数组中的内容写入到文件
			//参数2:参数1数组的偏移量,一般为0
			//参数3:本次文件写入操作要写入的字节数
			fsWriteStream.Write(byts, 0, byts.Length);
			//清空缓冲区,关闭文件流,释放资源
			fsWriteStream.Flush();
			fsWriteStream.Close();
			fsWriteStream.Dispose();



			//通过filestream来读文件
			using (FileStream fsRead = new FileStream("first.txt",FileMode.Open,FileAccess.Read))
			{
				//使用FileStream文件流读取文本文件的时候,不需要指定编码,因为编码实在将byte数组转换
				//为字符串的时候才需要使用编码。而这里是直接读取byte[],所以无需使用编码
				byte[] bytes1 = new byte[fsRead.Length];

				fsRead.Read(bytes1,0,bytes1.Length);
				//将byte转换字符串
				string msg3 = System.Text.Encoding.UTF8.GetString(bytes1);
			}


			//大文件拷贝
			string source = @"c:\";
			string target = @"d:\";
			BigFileCopy(source,target);


		}
		/// 
		/// 通过文件流实现将source拷贝到target中
		/// 
		/// 
		/// 
		private static void BigFileCopy(string source,string target) {
			//创建一个读取源文件的文件流
			using (FileStream fsRead = new FileStream(source,FileMode.Open,FileAccess.Read)) {
				//创建一个写入源文件的文件流
				using (FileStream fsWrite = new FileStream(target,FileMode.Create,FileAccess.Write)) {
					//拷贝文件,创建一个中间缓冲区
					byte[] bytes3 = new byte[1024 * 1024 * 5];
					//返回值表示本次实际读取到的字节数
					int r = fsRead.Read(bytes3,0,bytes3.Length);
					//将读取到的内容写入新文件中
					while (r > 0) {
						//第三个参数是读取到的字节数,而不是数组长度
						fsWrite.Write(bytes3,0,r);
						fsRead.Read(bytes3, 0, r);
						double d = (fsWrite.Position / (double)fsRead.Length) * 100;
						
					}

				}
			}

		}


		//压缩
		private void compressTest() {
			//读取源文件
			using (FileStream fsRead = File.OpenRead("1.txt")) {
				//写入新文件的流
				using (FileStream fsWrite = File.OpenWrite("yasuo.rar")) {
					//创建压缩流
					using (GZipStream zip = new GZipStream(fsWrite,CompressionMode.Compress)) {
						byte[] buffer = new byte[1024 * 10];
						int r;
						while ((r = fsRead.Read(buffer,0,buffer.Length)) > 0) {
							zip.Write(buffer,0,r);
						}
					}
				}
			}
		}

		//解压
		private void deCompressTest() {
			//读取压缩文件
			using (FileStream fsRead = File.OpenRead("yssuo.rar")) {
				//创建压缩流
				using (GZipStream zip = new GZipStream(fsRead, CompressionMode.Decompress)) {
					using (FileStream fsWrite = File.OpenWrite("jieya.txt")) {
						byte[] buffer = new byte[1024 * 10];
						//解压读取
						int r;
						while ((r = zip.Read(buffer,0,buffer.Length)) > 0) {
							//写入新文件
							fsWrite.Write(buffer,0,r);

						}
					} 
				}
			}

		}

		private void StreamTest() {

			using (StreamReader reader = new StreamReader("1.txt", Encoding.Default)) {

				//streamReader是逐行读取文本文件
				//直到文件的末尾,获取一个值,该值指示当前的流位置是否在流结尾。
				while (!reader.EndOfStream) {
					reader.ReadLine();
				}

				string line = null;
				while ((line = reader.ReadLine()) != null) {
					Console.WriteLine(line);
				}
				
			}

			using (StreamWriter writer = new StreamWriter("test.txt", true, Encoding.Default)) {
				for (int i = 0;i < 100;i++) {
					writer.WriteLine(i);
				}
			}

			string directoryPath = @"c:\users\public\reports";
			//创建一个文件夹对象
			DirectoryInfo directorySelecter = new DirectoryInfo(directoryPath);
			//获取当前文件夹下的所有子文件
			foreach (FileInfo fileToCompress in directorySelecter.GetFiles()) {
				//对于每个
				Compress(fileToCompress);
			}
			 
			foreach (FileInfo fileToDecompress in directorySelecter.GetFiles("*.gz")) {
				DeCompress(fileToDecompress);
			}
		}

		public static void Compress(FileInfo fileToCpmpress) {
			//创建一个指向当前文件fileToCompress的文件流
			using (FileStream originalFileStream = fileToCpmpress.OpenRead()) {
				if ((File.GetAttributes(fileToCpmpress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCpmpress.Extension != ".gz" ){
					//创建一个指向新文件的一个文件流
					using (FileStream compressFileStream = File.Create(fileToCpmpress.FullName + ".gz")) {
						using (GZipStream compressionStream = new GZipStream(compressFileStream, CompressionMode.Compress)) {
							//将源文件的文件流直接拷贝到压缩流中compressionStream
							originalFileStream.CopyTo(compressionStream);
							Console.WriteLine("compressed{0} from {1} to {2} bytes",
								fileToCpmpress.Name,fileToCpmpress.Length.ToString(),compressFileStream.Length.ToString());
						}
					}
				}
			}
		}

		public static void DeCompress(FileInfo fileToCompress) {

		}


你可能感兴趣的:(.net)