zoukankan      html  css  js  c++  java
  • C#中对文件的读写

    FileStream-------表示可以被写或被读,或二者都可的文件,此文件可以同步或异步读和写
    StreamReader------从流中读取字符数据,并可通过使用 FileStream被创建为基类。
    StreamWriter------向流写字符数据,可通过使用FileStream被创建为基类。

    1. 利用StreamWriter,StreamReader实现对文件的读写:
                StreamWriter sw = new StreamWriter("D:\\firsttext.txt", true);
                sw.WriteLine("you ran the StreamWriteDemo program on:");
                sw.WriteLine(DateTime.Now);
                sw.Close();

                StreamReader sr = new StreamReader("D:\\firsttext.txt");
                string strline = null;
                while ((strline = sr.ReadLine()) != null)
                {
                    Console.WriteLine(strline);
                }


    2. 利用FileStream构造StreamWriter和StreamReader来读写文件
                    FileStream fs = new FileStream("D:\\firsttext.txt", FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine("Are you Fightting now?");
                    sw.Close();

                     FileStream fsread = new FileStream("D:\\firsttext.txt", FileMode.OpenOrCreate, FileAccess.Read);
                    StreamReader sr = new StreamReader(fsread);
                    while ((strline = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(strline);
                    }

    3. 利用FileStream读写文件
                FileStream fwrite = new FileStream("D:\\stream.txt", FileMode.Append, FileAccess.Write);
                string str = Console.ReadLine();
                byte[] bt = new byte[1000];
                bt = Encoding.ASCII.GetBytes("\n" + str);
                fwrite.Write(bt, 0, str.Length);
                fwrite.Close();

                FileStream fread = new FileStream("D:\\stream.txt", FileMode.OpenOrCreate, FileAccess.Read);
                byte[] btread = new byte[1000];
                fread.Read(btread, 0, 1000);
                string s = Encoding.ASCII.GetString(btread);
                fread.Close();

  • 相关阅读:
    ITU 测试向量 下载地址
    转:数字集群移动通信系统技术体制综述及优选准则
    转:留一手教你在美国亚马逊网购
    离散度的测量(来自百度百科)与应用(自己理解)
    G.718的mos分
    【转】关于Alchemy
    Ogg Squish 0.98 源代码
    转:分布式视频编码关键技术及其发展趋势
    分布式视频编码概述与应用(来自百度百科)和WynerZiv Coding算法
    @PostConstruct和@PreDestroy注解在spring源码中生效的流程
  • 原文地址:https://www.cnblogs.com/mqingqing123/p/1808099.html
Copyright © 2011-2022 走看看