国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Reading/Writing text files using C#(轉:初學)

2024-07-21 02:28:01
字體:
來源:轉載
供稿:網友
注冊會員,創建你的web開發資料庫,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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 车致| 西乌| 贵溪市| 郴州市| 临桂县| 新安县| 绥芬河市| 南木林县| 嘉鱼县| 景洪市| 锡林浩特市| 武威市| 阳城县| 平泉县| 长丰县| 江门市| 建始县| 盘锦市| 巴林右旗| 玉林市| 云浮市| 吴桥县| 沂水县| 洛浦县| 惠州市| 西吉县| 西平县| 桑植县| 比如县| 蒙城县| 会同县| 布拖县| 色达县| 双流县| 松滋市| 交城县| 乌鲁木齐县| 石河子市| 芦溪县| 惠东县| 浠水县|