文件输入输出知识汇总
2015-04-07 16:47 糯米粥 阅读(258) 评论(0) 编辑 收藏 举报1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 7 namespace iio 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 // FileInfo f = new FileInfo("ir.txt"); 14 //// Console.WriteLine(f.Name); 15 16 // //using (FileStream fs = f.Create()) 17 // //{ 18 // //} 19 // using (StreamWriter sw = f.AppendText()) 20 // { 21 // sw.WriteLine("ii"); 22 // } 23 // //FileStream fi = File.Create(""); 24 25 //获取一个FileStream对象 26 using (FileStream fs = File.Open("one.dat", FileMode.Create)) 27 { 28 //把字符串编码成字节数组 29 string msg = "Hello"; 30 byte[] buffer = Encoding.Default.GetBytes(msg); 31 32 //把byte[]写入文件 33 fs.Write(buffer, 0, buffer.Length); 34 35 //重置内部流的位置 36 fs.Position = 0; 37 38 byte[] rebyte = new byte[buffer.Length]; 39 40 for (int i = 0; i < buffer.Length; i++) 41 { 42 rebyte[i] = (byte)fs.ReadByte(); 43 Console.WriteLine(rebyte[i]); 44 45 } 46 Console.WriteLine(Encoding.Default.GetString(rebyte)); 47 } 48 49 50 using (StreamWriter sw = File.CreateText("two.txt")) 51 { 52 sw.WriteLine("1"); 53 sw.WriteLine("2"); 54 sw.Write("3"); 55 sw.Write("3"); 56 sw.Write(sw.NewLine); 57 sw.Write(9); 58 } 59 60 using (StreamReader sr = File.OpenText("two.txt")) 61 { 62 string input = null; 63 while ((input = sr.ReadLine()) != null) 64 { 65 Console.WriteLine(input); 66 } 67 } 68 69 70 using (StringWriter sr = new StringWriter()) 71 { 72 //sr.GetStringBuilder(); 73 74 sr.WriteLine("ioio"); 75 StringBuilder bu = sr.GetStringBuilder(); 76 bu.AppendLine("99"); 77 // Console.WriteLine(bu); 78 79 using (StringReader s = new StringReader(sr.ToString())) 80 { 81 string input; 82 while ((input = s.ReadLine()) != null) 83 { 84 Console.WriteLine(input); 85 } 86 } 87 } 88 89 90 FileInfo info = new FileInfo("binfile.txt"); 91 92 using (BinaryWriter bw = new BinaryWriter(info.OpenWrite())) 93 { 94 int num = 89090; 95 bw.Write(num); 96 bw.Write(9.8); 97 } 98 99 //从流中读取二进制数据 100 using (BinaryReader br = new BinaryReader(info.OpenRead())) 101 { 102 Console.WriteLine(br.ReadInt32()); 103 } 104 105 106 //确定指向要观察的目录路径 107 FileSystemWatcher watcher = new FileSystemWatcher(); 108 watcher.Path = "folder"; 109 110 //设置需要"留意"的事情 111 watcher.NotifyFilter = NotifyFilters.LastAccess 112 | NotifyFilters.LastWrite 113 | NotifyFilters.FileName 114 | NotifyFilters.DirectoryName; 115 116 117 //只观察文本文件 118 watcher.Filter = "*.txt"; 119 watcher.Changed += new FileSystemEventHandler(OnChanged); 120 121 //开始观察目录 122 watcher.EnableRaisingEvents = true; 123 124 125 126 Console.ReadLine(); 127 } 128 129 static void OnChanged(object sender, FileSystemEventArgs e) 130 { 131 Console.WriteLine("Fil:{0} {1}", e.FullPath, e.ChangeType.ToString() == "Changed" ? "修改" : e.ChangeType.ToString()); 132 } 133 } 134 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?