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>