| by kirupa  |  
					29 March 2007
 In the
					
					previous page I provided some minor background 
					information on what files are, and more importantly, you got 
					to see an example of the code used to read a text file. In 
					this page, I will explain what each line of code does and 
					begin talking about how to write text files. 
 
						StreamReader
						reader 
						= new
						StreamReader("C:\\Users\\Kirupa\\Desktop\\quote.txt"); First, let's create a reader object that is of type 
					StreamReader. As part of the StreamReader constructor, I am 
					passing in the path to my text file. Notice that the folders 
					in the path are not separated by single slashes as  
					C:\Users\Kirupa\Desktop\quote.txt. Instead, the folder paths 
					are delimited by double slashes because of the way strings 
					in .NET are parsed. You will receive an error if you attempt 
					to keep the path using single-slashes. Another common way to deal with string-based paths is by 
					using the @ symbol. The @ symbol allows you to treat strings 
					literally without having them be parsed. With this 
					modification, our code would look like the following: 
						StreamReader
						reader 
						= new
						StreamReader(@"C:\Users\Kirupa\Desktop\quote.txt"); Notice that I am no longer using double-slashes because 
					the @ symbol before our quotes informs the compiler to not 
					parse the string.  
 
						string
						readerLine
						= 
						reader.ReadLine(); Next, I create a new string variable called readerLine. I 
					initialize readerLine to the first line from our text file 
					by using the reader object's ReadLine() method. 
 
						while
						(readerLine
						!= 
						null) { 
							Console.WriteLine(readerLine);
							readerLine
							= 
							reader.ReadLine(); } The above loop is where the main reading of your file 
					takes place. The loop will run as long as our readerLine 
					variable still holds some text from our text file. As you 
					progress through the loop, you set the readerLine variable 
					equal to the next line from our text file by using the 
					ReadLine() method. Once you reach the end of your text file, 
					reader.ReadLine() will return a null because there are no 
					more lines of text to show. Once the null value is returned, 
					our readerLine variable also equals null, and if you recall, 
					our loop only loops when readerLine is not equal to null! 
 Reading a file is fairly straightforward. The only thing 
					to watch out for is making sure you are ending your loop 
					properly. Common mistakes I've made include forgetting to 
					initialize the readerLine variable to the first line of text 
					prior to looping. That causes your loop to break 
					immediately. Another mistake I made is forgetting to set the 
					readerLine variable equal to the next line inside the loop. Besides what I listed above, reading files should is 
					pretty straightforward. Of course, reading files is only 
					half the fun! In the 
					next 
					page, you will learn how to use code to 
					write text to a file. Onwards to the
					
					next page! |