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);
//            }
//        }
//    }
//}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值