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

80酷酷网    80kuku.com

  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>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: