• 文件输入输出知识汇总


      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 }
  • 相关阅读:
    android 2.3 bug android 4.x bug
    设计高性能CSS3动画的几个要素
    开启硬件加速 解决页面闪白 保证动画流畅
    取消input在ios下,输入的时候英文首字母的默认大写 android 上去掉语音输入按钮
    JS中的类型检测
    CSS的一点知识
    HTML Canvas
    HTML DOM Table 对象
    只是有所了解的语言
    骑士巡游问题
  • 原文地址:https://www.cnblogs.com/nsky/p/4398722.html
Copyright © 2020-2023  润新知