Copy Code code as follows:
Using System;
Using System.IO;
public class Fileapp
{
public static void Main ()
{
Create a file MyFile.txt in the current directory with read and write access to the file
FileStream fsmyfile = new FileStream ("MyFile.txt", FileMode.Create, FileAccess.ReadWrite);
Create a data flow writer, associating with open files
StreamWriter swmyfile = new StreamWriter (fsmyfile);
Write a file as text
Swmyfile.writeline ("Hello, World");
Swmyfile.writeline ("abcdefghijklmnopqrstuvwxyz");
Swmyfile.writeline ("abcdefghijklmnopqrstuvwxyz");
Swmyfile.writeline ("0123456789");
Scour the data (the data is actually written to the file)
Note this sentence to try, the program will error
Swmyfile.flush ();
Read files in text mode
Create a data flow reader, associating with open files
StreamReader srmyfile= New StreamReader (Fsmyfile);
Reposition the file pointer to the beginning of the file
SrMyfile.BaseStream.Seek (0, Seekorigin.begin);
Print Tips
Console.WriteLine ("**************** read file ********************* in text mode");
Print File text content
string S1;
while ((S1 = Srmyfile.readline ())!=null)
{
Console.WriteLine (S1);
}
Console.WriteLine ();
End of read file as text
Read files in binary mode
Create a binary data Flow reader, associating with open files
BinaryReader brmyfile= New BinaryReader (Fsmyfile);
Reposition the file pointer to the beginning of the file
BrMyfile.BaseStream.Seek (0, Seekorigin.begin);
Print Tips
Console.WriteLine ("**************** read file ********************* in binary Way");
Print File text content
Byte B1;
while (Brmyfile.peekchar () >-1)
{
B1=brmyfile.readbyte ();
13 is "\ n", which means carriage return, 10 is "\ r", which means a newline
if (B1!= && B1!= 10)
{
Console.Write ("{0}", B1.) ToString ());
Console.Write (".");
}
Else
{
Console.WriteLine ();
}
}
Console.WriteLine ("\ n");
End of read file in binary mode
Close each object of the above new
Brmyfile.close ();
Srmyfile.close ();
Fsmyfile.close ();
Read File properties
Print Tips
Console.WriteLine ("**************** Read file attribute *********************");
FileInfo fimyfile=new FileInfo ("MyFile.txt");
Console.WriteLine ("FileName: {0}", fimyfile.name);
Console.WriteLine ("FileName (including path): {0}", fimyfile.fullname);
Console.WriteLine ("File Size (bytes): {0}", fimyfile.length);
Console.WriteLine ("File creation time: {0}", fimyfile.creationtime);
}
}