The table shows the most useful File methods we can use.
Method name | Use |
File.ReadAllBytes | Useful for files not stored as plain text. |
File.ReadAllLines | Microsoft: "Opens a file, reads all lines of the file with the specified encoding, and closes the file." |
File.ReadAllText | Returns the contents of the text file at the specified path as a string. |
File.WriteAllBytes | Not covered here. |
File.WriteAllLines | Stores a string array in the specified file, overwriting the contents. |
File.WriteAllText | Writes the contents string to a text file. |
File.AppendAllText | Use to append the contents string to the file at path. |
File.AppendText | Not covered here. |
File.ReadAllBytes.
we can use it to cache an image in memory for performance.It will increase the performance too.
static class ImageCache |
File.ReadAllLines
The following code reads in each line in the file "file.txt" into an array.
// Read in every line in specified file. |
StreamReader ReadLine
This method is not part of the File static class, but it is in the System.IO
namespace. I provide it here as a comparison to the File.ReadAllLines method.
Here is some data about how the two methods to read lines perform on different
sized files.
// Read in every line in the file. |
File.ReadAllText
It is used to rad the text from our data file.
void Example() { // // A. // Read in file with File class. // string text1 = File.ReadAllText("file.txt"); // // B. // Read in file text with helper method and StreamReader. // 40% faster! // string text2 = ReadFileString("file.txt"); } static string ReadFileString(string path) { // // Use StreamReader to consume the entire text file. // using (StreamReader reader = new StreamReader(path)) { return reader.ReadToEnd(); } } |
StreamReader helper. In some projects, it would be worthwhile to use the above ReadFileString custom static method.
File.ReadAllLines
List and ArrayList are extremely useful data structures for us programmers, as they allow us to rapidly expand (or shrink) large (or small) 'collections' of objects. Here we see how we can use LINQ to get a List of lines from a file in one line.
// // Read in all lines in the file, // and then convert to List with LINQ. // List |
File.ReadAllLines
Do you need to count the lines in a file? Every developer has had this requirement at some time. Are there 10 or 10,000 lines in the web server log file? We don't need to write ten lines of code to do this. Simply reference the Length property.
// Another method of counting lines in a file. // This is NOT the most efficient way, and it // counts empty lines. // int lineCount = File.ReadAllLines("file.txt").Length; |
See if a line exists in a file
Does a line containing a specific string exist in the file? Maybe you want to see if a name or location exists in a line in the file. Here again we can harness the power of LINQ to find any matching line.
// One way to see if a certain string is a line // in the specified file. Uses LINQ to count elements // (matching lines), and then sets |exists| to true // if more than 0 matches were found. // bool exists = (from line in File.ReadAllLines("file.txt") where line == "Some line match" select line).Count() > 0; |
File.WriteAllLines
When you are done with your in-memory processing, you often need to write the data to disk. Fortunately, the File class offers an excellent WriteAllLines method. It receives the file path and then the array to write. This will replace all the file contents.
// Write a string array to a file. string[] stringArray = new string[] { "cat", "dog", "arrow" }; File.WriteAllLines("file.txt", stringArray); // File contains: // cat // dog // arrow |
Append text to files
The previous example will replace the file's contents, but for a log file or error listing, we must append to the file. (Sure, we could read in the file, append to that in memory, and then write it out completely again. But that's slow.)
// Append text to a file. // This method will create a new file if one isn't already there. // No newlines will be automatically added. // File.AppendAllText("file.txt", "test"); // File contains: // test File.AppendAllText("file.txt", "more"); // File contains: // testmore |
This information is really useful. Gud Work!!
ReplyDeleteI want to know that would these codes work for xml file also? Actually I ve requirement as the application would read the existing xml file, search for particular attribute (that has been generated by application as in form of random number) and again generate the random number not same as the existing attributes.
Hi Jaya,
ReplyDeleteI have explained how to read and write data from XML in my post
"Read XML in C#.net"
Hope this will solve your problem.