• System.IO应用


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;

    namespace StreamReader类
    {
        class Program
        {
            //static void Main(string[] args)
            //{

            //}
            public static void Main()
            {
                string path = @"c: empMyTest.txt";
                try
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }

                    using (StreamWriter sw = new StreamWriter(path))//path 参数可以是文件名,包括统一命名约定 (UNC) 共享上的文件。如果此文件已存在,将覆盖它;否则,将创建一个新文件。
                    {                                               //不要求 path 参数必须是存储在磁盘上的文件;它可以是系统中支持通过流进行访问的任何部分。
                        sw.WriteLine("This");  //WriteLine方法是继承TextWriter基类的
                        sw.WriteLine("is some text");
                        sw.WriteLine("to test");
                        sw.WriteLine("Reading");
                    }

                    using (StreamReader sr = new StreamReader(path))
                    {
                        while (sr.Peek() >= 0)  //Peek方法返回下一个可用的字符,但不使用它。次方法的返回值:下一个要读取的字符,如果没有更多的可用字符或此流不支持查找,则为 -1。
                        {
                            Console.WriteLine(sr.ReadLine());   //ReadLine方法是重写基类TextRead的          
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("The process failed: {0}", e.ToString());
                }
            }

        }
    }

    /*
    StreamReader.ReadLine 方法 :从当前流中读取一行字符并将数据作为字符串返回。此方法的返回值:输入流中的下一行;如果到达了输入流的末尾,则为空引用(在VB中为Nothing,C#中为null)。
     ----继承关系:
      System.Object
       System.MarshalByRefObject
        System.IO.TextReader
           System.IO.StreamReader
           System.IO.StringReader
     
     ----继承关系:
      System.Object
       System.MarshalByRefObject
        System.IO.TextWriter
           System.CodeDom.Compiler.IndentedTextWriter
           System.IO.StreamWriter
           System.IO.StringWriter
           System.Web.HttpWriter
           System.Web.UI.HtmlTextWriter

    StreamReader的构造函数必须要求指定文件必须已经存在,如果不存在则会发生异常
    StreamWriter的构造函数不要求指定的文件必须已经存在,如果不存在则会创建该文件,如果存在则会改写或者追加,这要看你用那一个构造函数而定
    StreamReader中的Close,Peek,Read,ReadLine,ReadToEnd都是重写基类TextRead中的虚拟方法
    StreamWriter中的Close,Flush,Write是重写基类TextWriter中的虚拟方法,而WriteLine是继承基类TextWriter(虚拟的)中的方法
    一般在用完一个流之后要用Close关闭这个流,例如StreamReader和StreamWriter


    */

    //using System;
    //using System.IO;
    //using System.Text;

    //class Test
    //{
    //    public static void Main()
    //    {
    //        string path = @"c: empMyTest.txt";

    //        try
    //        {

    //            // Delete the file if it exists.
    //            if (File.Exists(path))
    //            {
    //                // Note that no lock is put on the
    //                // file and the possibliity exists
    //                // that another process could do
    //                // something with it between
    //                // the calls to Exists and Delete.
    //                File.Delete(path);
    //            }

    //            // Create the file.
    //            using (FileStream fs = File.Create(path))
    //            {
    //                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
    //                // Add some information to the file.
    //                fs.Write(info, 0, info.Length);
    //            }

    //            // Open the stream and read it back.
    //            using (StreamReader sr = File.OpenText(path)) //File.OpenText (string Path)//返回类型为SteamReader,打开现有UTF-8编码文本文件以进行读取

    //            {
    //                string s = "";
    //                while ((s = sr.ReadLine()) != null)
    //                {
    //                    Console.WriteLine(s);
    //                }
    //            }
    //        }

    //        catch (Exception Ex)
    //        {
    //            Console.WriteLine(Ex.ToString());
    //        }
    //    }
    //}

    //using System;
    //using System.IO;

    //class Test
    //{
    //    public static void Main()
    //    {
    //        string path = @"c: empMyTest.txt";
    //        if (!File.Exists(path))
    //        {
    //            // Create a file to write to.
    //            using (StreamWriter sw = File.CreateText(path))
    //            {
    //                sw.WriteLine("Hello");
    //                sw.WriteLine("And");
    //                sw.WriteLine("Welcome");
    //            }   
    //        }

    //        // Open the file to read from.
    //        using (StreamReader sr = File.OpenText(path))
    //        {
    //            string s = "";
    //            while ((s = sr.ReadLine()) != null)
    //            {
    //                Console.WriteLine(s);
    //            }
    //        }
    //    }
    //}


    本文来自CSDN博客:http://blog.csdn.net/Terry001/archive/2007/08/28/1762486.aspx

  • 相关阅读:
    兴趣与心态比较重要【转】
    网站发布到iis上,附加进程调试,打不到断点
    MVC时间格式化
    javascript数组扁平化处理
    Object.prototype.toString.call()
    获取浏览器大小
    HTTP状态码
    未能加载文件或程序集“Microsoft.ReportViewer.WebForms, Version=10.0.0.0
    javascript基础知识-1
    Html5上传图片的预览
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/1746859.html
Copyright © 2020-2023  润新知