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

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </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 nish files
{
    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"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值