C # Reading text files in binary format

Source: Internet
Author: User

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 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.