Reading/Writing text files using C#(转:初学)

来源:互联网 发布:华为云计算工程师面试 编辑:程序博客网 时间:2024/06/14 09:16
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
Introduction
Reading and Writing text files may sometimes be quite handy in programming. You might want to maintain your own text-style configuration files. Or edit autoexec.bat from your program. In .Net we have an abstract class called a Stream class which provides methods to read and write from a store. The filestream class is a Stream class derived class which wraps the streaming functionality around a file. In this article I'll demonstrate how you can use this class along with several reader and writer classes to read from a file, write to a file, create a file and even retrieve information about a file. I have provided a commented program below.

The Program
using System;
using System.IO;
public class nishfiles
{
    public static void Main(String[] args)
    {
            //Create a file 'nish.txt' in the current directory
        filestream fs = new filestream("nish.txt" , FileMode.Create, FileAccess.ReadWrite);         
        
        //Now let's put some text into the file using the StreamWriter
        StreamWriter sw = new StreamWriter(fs);         
        sw.WriteLine("Hey now! Hey now!/r/nIko, Iko, unday");
        sw.WriteLine("Jockamo feeno ai nan ay?/r/nJockamo fee nan ay?");
        sw.Flush();
        
        //We can read the file now using StreamReader        
        StreamReader sr= new StreamReader(fs);
        sr.BaseStream.Seek(0, SeekOrigin.Begin);
        string s1;
        Console.WriteLine("about to read file using StreamReader.ReadLine()");
        Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        while((s1 = sr.ReadLine())!=null)
            Console.WriteLine(s1);
        Console.WriteLine();
        
        //We can read the file now using BinaryReader        
        BinaryReader br= new BinaryReader (fs);
        br.BaseStream.Seek(0, SeekOrigin.Begin);
        Byte b1;
        Console.WriteLine("about to read file using BinaryReader.ReadByte()");
        Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        while(br.PeekChar()>-1)
        {
            b1=br.ReadByte();
            Console.Write("{0}",b1.ToChar());
            if(b1!=13 && b1!=10)
                Console.Write(".");    
        }        
        br.Close();
        Console.WriteLine();
            
        sw.Close();
        sr.Close();        
        fs.Close();

        //Use the File class to get some info on our file        
        Console.WriteLine("Print some info on our file using the File class");
        Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        File f=new File("nish.txt");
        Console.WriteLine("File name          : {0}",f.Name);
        Console.WriteLine("File name in full  : {0}",f.FullName);
        Console.WriteLine("File size in bytes : {0}",f.Length);
        Console.WriteLine("File creation time : {0}",f.CreationTime);
        
    }
}

The Output and explanation
This was the output I got on my machine.  

F:/c#/files>files1
about to read file using StreamReader.ReadLine()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Hey now! Hey now!
Iko, Iko, unday
Jockamo feeno ai nan ay?
Jockamo fee nan ay?

about to read file using BinaryReader.ReadByte()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
H.e.y. .n.o.w.!. .H.e.y. .n.o.w.!.
I.k.o.,. .I.k.o.,. .u.n.d.a.y.
J.o.c.k.a.m.o. .f.e.e.n.o. .a.i. .n.a.n. .a.y.?.
J.o.c.k.a.m.o. .f.e.e. .n.a.n. .a.y.?.

Print some info on our file using the File class
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File name          : nish.txt
File name in full  : F:/c#/files/nish.txt
File size in bytes : 83
File creation time : 10/13/01 2:18 PM

F:/c#/files>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击