Documente Academic
Documente Profesional
Documente Cultură
http://awesomecsharp.blogspot.in/2013/07/file-input-and-outputstreaming-by.html
Friday, July 5, 2013
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
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(); }}
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 "); }}
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
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);
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