Working with the File class, Additional methods of...

Working with the File class

The File class provides almost the same capabilities as the Filelnfo class, but uses static methods for this. Like the Filelnfo class, the File class provides the AppendText (), Create (), CreateText (), Open (), OpenRead (), OpenWrite (), and OpenText () methods. In fact, in many cases, the File and Filelnfo classes can be used interchangeably. As a demonstration, you can rewrite the previous examples for the FileStream class using the File class:

// Get the FileStream object using File.Create ().

FileStream fs = File.Create (@ C: Test.dat );

// Get the FileStream object with File.Open ().

FileStream fs2 = File.Open (@ C: Test2.dat & quot ;, FileMode.OpenOrCreate, FileAccess.ReadWrite.FileShare.None);

// Get the FileStream read-only object FileStream readOnlyStream = File.OpenRead (@ Test3.dat );

// Get the FileStream object only for writing FileStream writeOnlyStream = File.OpenWrite (@ Test4.dat );

// Get the StreamReader object

StreamReader sreader = File.OpenText (@ C: oot.ini );

// Get the StreamReader object for the new StreamWriter file swriter = File.CreateText (@ C: Test3.txt );

// Get the StreamReader object to add StreamWriter text swAppend = File.AppendText (@ C: FinTst.txt );

Additional methods of the File class

The File class also supports several special methods, which are explained in the following table. 9.5, which greatly simplify the process of reading and writing text data.

Table 9.5

Methods of the File class

Method

Description

ReadAllBytes ()

opens the specified file, returns the binary data as an array of type byte and then closes the file

ReadAllLines ()

opens the specified file, returns character data as an array of lines, and then closes the file

ReadAllText ()

opens the specified file, returns the character data as a single line, and then closes the file

WriteAllBytes ()

opens the specified file, writes an array of type byte and then closes the file

WriteAllLines ()

opens the specified file, writes an array of strings, and then closes the file

WriteAllTextQ

opens the specified file, writes a character string and then closes the file

Using these methods of the File class allows you to read and write a portion of data using several operators.

For example, in the following example, the character data is written to a new file:

string [] myTasks = { Translate text , Call my mother };

// Write all data to a file.

File.WriteAllLines (@ C: asks.txt & quot ;, myTasks);

// Read all the recorded data and output it to the foreach screen (string task in File.ReadAIILines (@: C: asks.txt))) Console.WriteLine ( Must do: {0} & quot ;, task );

thematic pictures

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)