Sunteți pe pagina 1din 9

For more info please visit

http://awesomecsharp.blogspot.in/2013/07/file-input-and-outputstreaming-by.html
Friday, July 5, 2013

File Input and Output (Streaming) by santosh

File Input and output in awesome c sharp


by santosh Some words Hi..... Guys how are u all ? It's really a lovely day i m very happy to see one of the my old friend doing awesome in his job keep it up bhai.. [ bhai means brother ]. I m back with my new post on File input and Output on awesome c sharp. Today i m going to post on streaming or in more technical words it better to call it Conduit Are u ready friends here we go.....

File Input and Output :


Friends data is the most important thing for the programmer. Data is the thing which is need to stored in a secure memory location so that it could not be accessed illegally or unauthorized ways. So information is saved permanently on a disk, you can use a file. A file is stream of characters or a flow of related data. Streams enables you to write and read bytes to and from a storage medium , respectively. Streams can be used to perform fundamental operations such as read and write. Friends File input and output is also known as stream and concept as streaming, some of the programmer user to call it conduit. Friends Computer works on IPO cycle input process and output. Data transferred from one location to the another location in the forms of bytes streams. The origin point is referred as Source and final location is called Destination. Think about a river stream in which many things flow with water like sand particles, leafs, tree branches, fishes and river other animals. That flowing water is calledstream and that stream will surely have an source and destination. So in simple words Streaming is flow of data. Same as data flowing in transmission channel is also streaming it could be data streaming or electrical pulse streaming, will be considered as File Input and Output or read / write. In computers data is stored in the disk. In disk information is saved on the container called as file yes file is container kind of thing which holds your data.. File storing data will have a directory path, when you open a file for reading or writing data it becomes streaming. Stream is the sequence of bytes travelling from source to destination over a channel. Two basic streaming are input and output. An input streaming is used for read operation and the output stream is used for write operations.

System.IO Namespace :
Friends namespace is the collection of the various classes which are used to perform various functions such as File creation, File deletion so on.... in awesome c sharp you can work with streams by using FileStream class. This class supports random file access to file, which means to read from and write to any location within a file. FileStream class comes under System.IO namespace. FileStream class inherits the abstract class stream. Providing the syntax for streaming. FileStream <object name> = new FileStream (<file Name>, <FileMode Enumerator> , <FileAccess Enumerator>, FileShare Enumerator>

Let see it live working demos In this demo i m going to create a notepad file through Awesome C sharp code in my d drive. I have given the name batman to my d drive. Look carefully i doesn't have any notepad file naming Awesome_C_sharp i m going to create it right now via code

Friends now i m going to code on visual studio

Explaining friends : In this code i have used a namespace using System.IO; which will provide me some of the classes and functions so that streaming can be performed. After using this namespace System.IO, everything is performed on Main() function in which FileStream class is used which is used in making file steaming, by defining some enumerator like filename for defining name of file to be created, FileMode which defines the operation to b e performed on file like create is used here, FileAccess

which defines the accessing reason for example here we are accessing to write on a file and FileShare defines about file sharing among different processes. Friends here i m not using file sharing enumerator it will also work without it, Here is code

using System; using System.IO; class Car { public static void Main() { FileStream fs = new FileStream("d:\\Awesome_C_sharp.txt", FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); Console.WriteLine("please write some thing "); string data = Console.ReadLine(); sw.WriteLine(data); sw.Flush(); sw.Close(); fs.Close(); Console.WriteLine("Data written successfully in d drive"); Console.Read(); }}

now time to see the output

friends this is the way of file creation in awesome c sharp, you must be thinking that how to read it? Now let's try to read it

FileStream class motive is same as it was in Stream writer, here also it provides the steam path channel for reading a file, but with changed enumerator like FileMode is changed to Open, and FileAccess is changed to Read, these enumerators defines that this stream will work for reading purpose, whereas it was writing purpose in previous code of SteamWriter. StreamReader class is used to provide a reading byte stream. Code like sr.BaseStream.Seek(0, SeekOrigin.Begin); is used to define the seeking feature for reading bytes stream starting from zero level to the last level. And all the data content is stored in a string variable which can be read easily. Friends be careful while working on steams. Here instead of StreamWriter we used StreamReader because SteamReader class provides the reading stream features in memory already defined. here is code using System; using System.IO; class Car { public static void Main() { FileStream fs = new FileStream("d:\\Awesome_C_sharp.txt", FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); sr.BaseStream.Seek(0, SeekOrigin.Begin); string str = sr.ReadToEnd(); Console.WriteLine(str); sr.Close(); fs.Close(); Console.ReadLine(); }} Friends you can also work on System Directories and other input output working Let's see how ? DirectoryInfo and FileInfo classes Friends u can access System Directories and files via DirectoryInfo or FileInfo classes as i m doing here. these classes includes various methods. in this code i m trying to read my d drive directory song which consist 40 songs i m going to read them and displaylist of them in command promp. DirectoryInfo class is used to read directory information and FileInfo class is used to read File information, GetFiles is a inbuilt function in FileInfo class and foreach is a loop which will loop 40 times.

friends u can take any directory for your demonstration Friends code is here using System; using System.IO; class Car { public static void Main() { int numn=0; DirectoryInfo d = new DirectoryInfo("d:\\songs"); FileInfo[] f = d.GetFiles(); Console.ForegroundColor = ConsoleColor.Green; numn = f.Length; Console.WriteLine("no of files "+numn); Console.ForegroundColor = ConsoleColor.White; foreach (FileInfo ff in f) { Console.WriteLine("============================== ======="); Console.WriteLine("File name : "+ff.Name); Console.WriteLine("Full name : "+ff.FullName); Console.WriteLine("File Extension : "+ff.Extension); Console.WriteLine("Last access time : " +ff.LastAccessTime); Console.WriteLine(); Console.WriteLine("========================================="); }}}

Create Directory
Directory creation is very simple just in one simple code line Directory.CreateDirectory("d:\\awesome c sharp "); check out code picture in visual studio

code is here using System; using System.IO; class Car { public static void Main() { Directory.CreateDirectory("d:\\awesome c sharp "); Console.WriteLine("created see your d drive "); }}

see the picture

same way u can delete Files and Directories...... Lot's more can be done Binary Reader Writer Friends all the information in the system stored in the text form would be displayed on a screen as text. This mean 'A' will be written as 'A' in the files, Similarly the numbers -12345.678 will be written as the string format "-12345.678" This means that u can directly display the content of the file on the screen. Binary read or write means that the number -12345.678 is written as a float representation consuming 4 bytes space. Binary Reader or Writer is used for reading and writing the binary data into the files. Binary reader is used to read binary data from the file and Binary Writer is used to write binary on the file now let's see technical demonstration of them combined. See this drive picture yago i m going to read it and then write it into awesome_c_sharp folder

now see the code

Code is here guys using System.IO; using System; class Car { public static void Main(string[] args) { FileStream fs = new FileStream("d:\\yago.gif", FileMode.Open, FileAccess.Read);

BinaryReader br = new BinaryReader(fs); long length = fs.Length; byte[] b = br.ReadBytes((int)length);

FileStream fs1 = new FileStream("d:\\awesome c sharp\\yago.gif", FileMode.Create, FileAccess.ReadWrite); BinaryWriter bw = new BinaryWriter(fs1); bw.Write(b); bw.Flush(); br.Close(); fs.Close(); Console.WriteLine("done"); Console.ReadLine(); }}

in this code data will be in bytes form so we will require a byte array to hold it and explicit casting is also required because value will be on long data type which needs to be cast in bytes now see the output

Friends as u can see that Yago animated parrot picture is written in awesome c sharp directory. So friends it's really AWESOME to to Streaming. When will will reach beyond basic programming concept i will try to explain on network streaming ip [ internet packet ] streaming and so on.

Thanks guys for reading this article of input output steaming on awesome c sharp. It's Raining here. i m going to have some tea and snacks. See u in next post guys. ====================================================== all right reserved it 2013 awesome c sharp brought to u by vardana solution =====================================================
For more info please visit

http://awesomecsharp.blogspot.in/2013/07/file-input-and-output-streamingby.html

S-ar putea să vă placă și