c sharp reads text files in binary

  • 2020-05-05 11:51:00
  • OfStack


using System; 
using System.IO; 

public class FileApp 
{ 
    public static void Main() 
    { 
        //  Create a file in the current directory myfile.txt , has read and write rights to the file  
        FileStream fsMyfile = new FileStream("myfile.txt" , FileMode.Create, FileAccess.ReadWrite); 

        //  Creates a data stream writer associated with the open file  
        StreamWriter swMyfile = new StreamWriter(fsMyfile); 

        //  Write a file as text  
        swMyfile.WriteLine("Hello, World"); 
        swMyfile.WriteLine("abcdefghijklmnopqrstuvwxyz"); 
        swMyfile.WriteLine("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 
        swMyfile.WriteLine("0123456789"); 

        //  Scour data ( Actually write the data to the file ) 
        //  Try this sentence and the program will report an error  
        swMyfile.Flush(); 

        //  Read the file as text  
        //  Creates a data stream reader associated with the open file  
        StreamReader srMyfile= new StreamReader(fsMyfile); 

        //  Reposition the file pointer to the beginning of the file  
        srMyfile.BaseStream.Seek(0, SeekOrigin.Begin); 

        //  Print prompt  
        Console.WriteLine("**************** Read the file as text *********************"); 

        //  Print the text content of the file  
        string s1; 
        while((s1 = srMyfile.ReadLine())!=null) 
        { 
            Console.WriteLine(s1); 
        } 
        Console.WriteLine(); 
        //  End the file with a text read  


        //  Read files in binary mode  
        //  Creates a binary data stream reader associated with the open file  
        BinaryReader brMyfile= new BinaryReader (fsMyfile); 

        //  Reposition the file pointer to the beginning of the file  
        brMyfile.BaseStream.Seek(0, SeekOrigin.Begin); 

        //  Print prompt  
        Console.WriteLine("**************** Read files in binary mode *********************"); 

        //  Print the text content of the file  
        Byte b1; 
        while(brMyfile.PeekChar()>-1) 
        { 
            b1=brMyfile.ReadByte(); 
            // 13 for "\n" , means return; 10 for "\r" , represents a line break  
            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  

        //  Shut down more than new Each object of  
        brMyfile.Close(); 
        srMyfile.Close(); 
        fsMyfile.Close(); 

        //  Read file properties  
        //  Print prompt  
        Console.WriteLine("**************** Read file properties *********************"); 

        FileInfo fiMyfile=new FileInfo("myfile.txt"); 
        Console.WriteLine(" The file name  : {0}",fiMyfile.Name); 
        Console.WriteLine(" The file name ( Contains the path to the ) : {0}",fiMyfile.FullName); 
        Console.WriteLine(" The file size (bytes) : {0}",fiMyfile.Length); 
        Console.WriteLine(" File creation time  : {0}",fiMyfile.CreationTime); 
    } 
} 

Related articles: