Skip to main content

Posts

Showing posts with the label File IO

Data Serialization in .Net

Namespace: System.Runtime.Serialization Serialization is the process of converting object so that they can be transmitted over the network, or stored in file for later use, or passing them to a web service or any other application. .Net WebServices, Session Object, .Net Remoting and clipboard rely on Serialization. Binary Serialization: It’s efficient but not portable solution. String data = “Hello”; FileStream fs = new FileStream(“FileStream.Data”, FileMode.Create); BinaryFormatter bf = new BinarFormatter (); Bf.Serilize(fs, data); //Wrting Objects Bf.Serilize(fs,System.DateTime.Now); Fs.close(); Deserializing Objects FileStream fs = new FileStream(“FileStream.Data”, FileMode.Open); BinaryFormatter bf = new BinarFormatter (); String Data = (String)bf.Deserialize(fs); DateTime vTime = (DateTime) bf.Derialize(fs); To serialize a class one must the Serializeable attribute to that class. To omit some attribute from deseriali...

Text Encoding and Decoding using .Net

Namespace: System.Text To read a file, an application must write information about the encoding in which the file was written. So, the application that creates a file must write encoding information, otherwise viewer will see block and question marks in file. Unicode UTF-32 encoding : Represents a character as sequence of four bytes (32 bits). UTF32Encoding class can be used in that perspective. Unicode UTF-16 encoding : Represents a character in 16 bits. UnicodeEncoding class can be used to convert to and from UTF-16 encoding. Unicode UTF-8 encoding : Unicode UTF-8 uses 8-bit, 16-bit, 24-bit, and up to 48-bit encoding. Values 0 through 127 use 8-bit encoding and exactly match ASCII values, 128 through 2047 use 16-bit encoding, and values from 2048 and 65535 uses 24-bit encoding. UTF8Encoding class can be used to convert to and from UTF-8 encoding. ASCII encoding : ASCII encoding encodes Latin alphabets as single 7-Bit ASCII character. It is not well su...

Compressing Streams in .Net

Namespace: System.IO.Compression There are two types of compression streams, GZip and Deflate . GZip leaves a standard header information block that can be used by the tools like Gzip to decompress the information. Deflate does not leaves any header information thus it is only decompressed by the respective application only. Both streams can only compress 4GB of uncompressed data. FileStream source = new FileStream(@“C:\file.dat”); FileStream destination = new FileStream(@“C:\file.gzip”); GZipStream Gs = new GZipStream(destination, CompressionMode.Compress); int b = source.ReadByte(); While(b!=-1) {                 Gs.WriteByte((byte)b);                 b = source.ReadByte(); } And to decompress a compressed file : FileStream source = new FileStream(@“ C:\file.gzip ”);   //reversed ...

Reading and Writing Files with .Net

Understanding Streams : Sequential and random access streams. All stream classes inherit from Stream class , these classes include: FileStream (System.IO), MemoryStream (System.IO), CryptoStream(System.Security), NetworkStream(System.Net), GZipStream(System.Compression). File class is an important class used to open file for reading/writing, Exist, Delete, Move and etc… Create/open Methods of File will return FileStream Object. For Sequential reading and writing some methods also return StreamReader and StreamWriter and they encapsulate FileSteam object. Few simple operations are identical to FileInfo object. MemoryStream is a specialized class provided to manage data in memory stream to gain some optimization. How to read from a File : FileStream theFile = File.Open(@“C:\boot.ini”, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(thefile); Console.Write(sr.ReadToEnd()); Sr.Close(); theFile.Close(); OR StreamReader s...