Showing posts with label ASCIIEncoding. Show all posts
Showing posts with label ASCIIEncoding. Show all posts

Saturday, 28 April 2012

Now use the BinaryReader and BinaryWriter classes to read and write primitive types to a file.The BinaryWriter writes primitive objects in their native format, so in order to read them using the BinaryReader, you must select the appropriate Read method.
The Example shows you how to do that; in this case, you are writing a value from a number of different primitive types to the text file and then reading the same value.

Here are sample Example :

C# Example :
        ///////Write to a file
        System.IO.BinaryWriter binarywriter =new System.IO.BinaryWriter(System.IO.File.Create(MapPath
 ("BinaryFile.dat")));
        binarywriter.Write("This is a sample string.");
        binarywriter.Write(0x12346789abcdef);
        binarywriter.Write(0x12345678);
        binarywriter.Write('c');
        binarywriter.Write(1.5f);
        binarywriter.Write(1000.2m);
        binarywriter.Close();

        ////////Read from a file
        System.IO.BinaryReader binaryreader = new System.IO.BinaryReader(System.IO.File.Open
(MapPath("BinaryFile.dat"), System.IO.FileMode.Open));
        string strA = binaryreader.ReadString();
        long lngL = binaryreader.ReadInt64();
        int intI = binaryreader.ReadInt32();
        char chrC = binaryreader.ReadChar();
        float fltF = binaryreader.ReadSingle();
        decimal dclD = binaryreader.ReadDecimal();
        binaryreader.Close();


If you open this file in Notepad, you will see that the BinaryWriter has written the nonreadable binary data to the file. The BinaryReader provides a number of different methods for reading various kinds of Primitive types from the stream. In this Example, you use a different Read method for each primitive type that you Write to the file.

VB.net Example :
    Dim binarywriter As New System.IO.BinaryWriter(System.IO.File.Create(MapPath("binary.dat")))
    binarywriter.Write("a string")
    binarywriter.Write(&H12346789ABCDEF)
    binarywriter.Write(&H12345678)
    binarywriter.Write("c"c)
    binarywriter.Write(1.5F)
    binarywriter.Write(100.2D)
    binarywriter.Close()

    Dim binaryreader As New System.IO.BinaryReader(System.IO.File.Open(MapPath
     ("binary.dat"),System.IO.FileMode.Open))
    Dim a As String = binaryreader.ReadString()
    Dim l As Long = binaryreader.ReadInt64()
    Dim i As Integer = binaryreader.ReadInt32()
    Dim c As Char = binaryreader.ReadChar()
    Dim f As Double = binaryreader.ReadSingle()
    Dim d As Decimal = binaryreader.ReadDecimal()
    binaryreader.Close()

Friday, 27 April 2012

StreamReader and StreamWriter classes to write a string to a text file and then read the contents of that text file.

Here are Exmaple for Write and Read File.

C# Example :
         /////Write to a file
        System.IO.StreamWriter streamwriter = new System.IO.StreamWriter(System.IO.File.Open
(MapPath("TextFile.txt"),System.IO.FileMode.OpenOrCreate));
        streamwriter.Write("This is a sample string");
        streamwriter.Close();

        //////Read from a file
        System.IO.StreamReader reader =new System.IO.StreamReader(System.IO.File.Open(MapPath
("TextFile.txt"),System.IO.FileMode.Open));
        string tmp = reader.ReadToEnd();
        reader.Close();


When you create a StreamReader, you must pass an existing stream instance as a constructor
parameter. The reader uses this stream as its underlying data source.
In this sample, you use the File class’s Open method to open a writable FileStream for your StreamWriter.

Also notice that you no longer have to deal with byte arrays. The StreamReader takes care of converting the data to a type that’s more user-friendly than a byte array. In this example, you are using the ReadToEnd method to read the entire stream and convert it to a string.The StreamReader provides a number of different methods for reading data that you can use depending on exactly how you want to read the data, from reading a single character using the Read method, to reading the entire file using the ReadToEnd method.

VB.net Example :
    /////Write to a file
    Dim streamwriter As New System.IO.StreamWriter(System.IO.File.Open(MapPath
                     ("TextFile.txt"),System.IO.FileMode.Open))
    streamwriter.Write("This is a string")
    streamwriter.Close()

    //////Read from a file
    Dim reader As New System.IO.StreamReader(System.IO.File.Open(MapPath
                   ("TextFile.txt"),System.IO.FileMode.Open))
    Dim tmp As String = reader.ReadToEnd()
    reader.Close()

Thursday, 26 April 2012

There is a situation where you want to write data in Memory Stream.
You can write to the stream by encoding a string containing the information you want to write to a byte array and then using the stream’s Write method to write the byte array to the MemoryStreams.
The Close method also calls Flush internally to commit the data to the data store.

Here are sample Example
C# Example :
    byte[] data = System.Text.Encoding.ASCII.GetBytes("This is a sample string");
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    ms.Write(data, 0, data.Length);
    ms.Close();

VB.net Example :
    Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes("This is a sample string")
    Dim ms As New System.IO.MemoryStream()
    ms.Write(data, 0, data.Length)
    ms.Close()

Wednesday, 25 April 2012

Any type of I/O operation you are performing in .NET, if you want to read or write data you eventually use a stream of some type. Streams are the basic mechanism. .NET uses to transfer data to and from its underlying source, it will be a file, communication pipe, or TCP/IP socket. The Stream class provides the basic functionality to read and write I/O data, but because the Stream class is marked as abstract, you most likely need to use one of the several classes derived from Stream. Each Stream derivation is specialized to make transferring data from a specific source easy.
Here is Table to lists some of the classes derived from the Stream class.

TABLE :

CLASS DESCRIPTION
System.IO.FileStream Reads and writes files on a file system, as well as other file related operating system handles (including pipes, standard input, standard output, and so on).
System.IO.MemoryStream Creates streams that have memory as a backing store instead of a disk or a network connection. This can be useful in eliminating the need to write temporary files to disk or to store binary information in a database.
System.IO.UnmanagedMemoryStream Supports access to unmanaged memory using the existing stream-based model and does not require that the contents in the unmanaged memory be copied to the heap.
System.IO.BufferedStream Extends the Stream class by adding a buffering layer to read and write operations on another stream. The stream performs reads and writes in blocks (4096 bytes by default), which can result in improved efficiency.
System.Net.Sockets.NetworkStream Implements the standard .NET Framework stream to send and receive data through network sockets. It supports both synchronous and asynchronous access to the network data stream.
System.Security.Cryptography.CryptoStream Enables you to read and write data through cryptographic transformations.
System.IO.Compression.GZipStream Enables you to compress data using the GZip data format.
System.IO.Compression.DeflateStream Enables you to compress data using the Deflate algorithm.
System.Net.Security.NegotiateStream Uses the Negotiate security protocol to authenticate the client,and optionally the server, in client-server communication.
System.Net.Security.SslStream Necessary for client-server communication that uses the Secure Socket Layer (SSL) security protocol to authenticate the server and optionally the client.

Tuesday, 24 April 2012

There are many times you want to read text file.
There are many ways to read files. One way is to use FileStream class.
Here are sample example to read the file and display it's content on page.

C# Example :
        System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("TextFile.txt"),System.IO.FileMode.Open);
        byte[] data = new byte[fs.Length];
        fs.Read(data, 0, (int)fs.Length);
        fs.Close();
        Response.Write( System.Text.ASCIIEncoding.Default.GetString(data));

In this example creating a byte array the length of the stream, using the Length property to properly size the array, and then passing it to the Read method.
The Read method fills the byte array with the stream data, in this case reading the entire stream into the byte array. If you want to read only a chunk of the stream or to start at a specific point in the stream simply change the value of the parameters you pass to the Read method.

Streams must always be explicitly closed in order to release the resources they are using, which in this case is the file. Failing to explicitly close the stream can cause memory leaks, and it may also deny other users and applications access to the resource.

VB.net Example :
        Dim fs As New System.IO.FileStream(Server.MapPath("TextFile.txt"), System.IO.FileMode.Open)
        Dim data(fs.Length) As Byte
        fs.Read(data, 0, fs.Length)
        fs.Close()
        Response.Write(  ASCIIEncoding.Default.GetString(data) )