zoukankan      html  css  js  c++  java
  • c#读写文本文档-1-用file类

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.IO;
     6 
     7 namespace txtread
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13         //    //File 优点:命令简单,可以读各种类型,但是耗内存,因为是以下子全读入内存了
    14 //15         //    //Create Delete Copy Move
    16         ////1.按字节读取整个文档 所有类型都可以读取,包括多媒体文件
    17         //    byte[] buffer = File.ReadAllBytes(@"C:UsersAdministratorDesktop租房.txt");
    18         //    //这个字节数组我们看不懂,需要转换为字符串我们才能看懂,但用tostring转换不行
    19         //    //得到的是这个数组的命名空间,所以我们需要字节数组中每一个元素按照我们指定的方式
    20         //    //解析成字符串。
    21         //    string s=Encoding.GetEncoding("GBK").GetString(buffer);
    22         //    //string s = Encoding.UTF8.GetString(buffer);//中文乱码了,因为默认保存为ANSI
    23         //    Console.WriteLine(s);
    24         //    Console.ReadKey();
    25 
    26             ////按字节写入到文档
    27             //string str = "我是一个男生";
    28             //byte[] buffer = Encoding.Default.GetBytes(str);
    29             //File.WriteAllBytes(@"C:UsersAdministratorDesktop租房.txt", buffer);//覆盖写入
    30             //Console.WriteLine("写入成功");
    31             //Console.ReadKey();
    32 
    33      ////2.以行的方式进行读取 返回的是字符串数组,意味着可以精确操作文本文件每一行数据
    34      //       string[] contents=File.ReadAllLines(@"C:UsersAdministratorDesktop商品清单.txt",Encoding .Default  );
    35      //       foreach (string item in contents)
    36      //       {
    37      //           Console.WriteLine(item);
    38      //       }
    39      //       Console.ReadKey();
    40      ////3.以全文档的形式读入  用于展示全文用。
    41      //       string str=File.ReadAllText (@"C:UsersAdministratorDesktop商品清单.txt",Encoding .Default );
    42      //       Console .WriteLine (str);
    43      //       Console .ReadKey ();
    44          ////3.1相对路径 尽量使用
    45          //   string str = File.ReadAllText("商品清单.txt", Encoding.Default);
    46          //   Console.WriteLine(str);
    47          //   Console.ReadKey();
    48 //
    49         //// File.WriteAllLines () 将字符串数组一行一行的写入文本文档
    50            // File.WriteAllLines ("商品清单.txt",new string[] {"第一行","第二行"});//将两个字符串覆盖写入两行
    51            // Console.WriteLine ("ok");
    52            // Console.ReadKey ();
    53         //// File.WriteAllText () 将字符串写入文本文档
    54         //    File.WriteAllText("商品清单.txt", "我覆盖原文档内容且不分行 不分行 不分行",Encoding .Default );//将两个字符串覆盖写入两行
    55         //    Console.WriteLine("ok");
    56         //    Console.ReadKey();
    57         //追加写入
    58             File.AppendAllText("商品清单.txt","我是追加的内容,我不会覆盖原内容", Encoding.Default);
    59             Console.WriteLine("ok");
    60             Console.ReadKey();
    61         }
    62     }
    63 }
  • 相关阅读:
    路由策略
    ospf 路由汇总
    OSPF type1 2
    ospf
    TCP 六种标识位
    raid 10 与 01
    SNMP协议
    ffmpeg剪切视频
    ubuntu18安装sbt
    服务器Ubuntu18重启后宝塔访问不了
  • 原文地址:https://www.cnblogs.com/zhubinglong/p/5808468.html
Copyright © 2011-2022 走看看