CopyCode The Code is as follows: using system;
Using system. IO;
Public class fileapp
{
Public static void main ()
{
// Create a file named myfile.txt in the current directory and have the read and write permissions on the file.
Filestream fsmyfile = new filestream ("myfile.txt", filemode. Create, fileaccess. readwrite );
// Create a data stream writer and associate it with the opened file
Streamwriter swmyfile = new streamwriter (fsmyfile );
// Write an object in text format
Swmyfile. writeline ("Hello, world ");
Swmyfile. writeline ("abcdefghijklmnopqrstuvwxyz ");
Swmyfile. writeline ("abcdefghijklmnopqrstuvwxyz ");
Swmyfile. writeline ("0123456789 ");
// Flush data (write the data to a file)
// Try to comment out the sentence,ProgramWill report an error
Swmyfile. Flush ();
// Read an object in text format
// Create a data stream reader and associate it with the opened file
Streamreader srmyfile = new streamreader (fsmyfile );
// Relocates the file pointer to the beginning of the file
Srmyfile. basestream. Seek (0, seekorigin. Begin );
// Print the prompt information
Console. writeline **************** *****");
// Print the file text
String S1;
While (S1 = srmyfile. Readline ())! = NULL)
{
Console. writeline (S1 );
}
Console. writeline ();
// End by reading the file in text mode
// Read the file in binary mode
// Create a binary data stream reader and associate it with the opened file
Binaryreader brmyfile = new binaryreader (fsmyfile );
// Relocates the file pointer to the beginning of the file
Brmyfile. basestream. Seek (0, seekorigin. Begin );
// Print the prompt information
Console. writeline ("***************** read files in binary mode **************** *****");
// Print the file text
Byte B1;
While (brmyfile. peekchar ()>-1)
{
B1 = brmyfile. readbyte ();
// 13 is "\ n", indicating carriage return; 10 is "\ r", indicating line feed
If (b1! = 13 & B1! = 10)
{
Console. Write ("{0}", b1.tostring ());
Console. Write (".");
}
Else
{
Console. writeline ();
}
}
Console. writeline ("\ n ");
// End by reading the file in binary mode
// Close all objects of the new object above
Brmyfile. Close ();
Srmyfile. Close ();
Fsmyfile. Close ();
// Read File Attributes
// Print the prompt information
Console. writeline ****************** ***");
Fileinfo fimyfile = new fileinfo ("myfile.txt ");
Console. writeline ("File Name: {0}", fimyfile. Name );
Console. writeline ("file name (including path): {0}", fimyfile. fullname );
Console. writeline ("file size (bytes): {0}", fimyfile. Length );
Console. writeline ("File Creation Time: {0}", fimyfile. creationtime );
}
}