C # Read text files in binary _c# tutorial

Source: Internet
Author: User
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);
}
}

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.