Sunday, December 23, 2012

Explain File Handling



File Handling





  • System.MarshalByRefObject—Base object class for .NET classes that are remotable; permits marshaling of data between application domains.
  •  FileSystemInfo—Base class that represents any file system object.
  •  FileInfo and File—These classes represent a file on the file system.
  •  DirectoryInfo and Directory—These classes represent a folder on the file system.
  •  Path—This class contains static members that you can use to manipulate pathnames.
 Classes for Files and Folders

  • Directory and File contain only static methods and are never instantiated. You use these classes by supplying the path to the appropriate file system object whenever you call a member method. If you only want to do one operation on a folder or file then using these classes is more efficient, because it saves the overhead of instantiating a .NET class.
  • DirectoryInfo and FileInfo implement roughly the same public methods as Directory and File, as well as some public properties and constructors, but they are stateful and the members of these classes are not static. You need to instantiate these classes before each instance is associated with a particular folder or file. This means that these classes are more efficient if you’re performing multiple operations using the same object.

Reading and Writing to Files: Streams

·         If the data is being transferred from some outside source into your program, then that is reading from the stream.
·         If the data is being transferred from your program to some outside source, then that is writing to the stream

FileStream—This class is intended for reading and writing binary data in a binary file.However, you can also use it to read from or write to any file.
·          StreamReader and StreamWriter—These classes are designed specifically for reading from and writing to text files.

Filestream Class
·         The file you want to access.
·         The mode, which indicates how you want to open the file. For example, are you intending to create a new file or open an existing file, and if opening an existing file, should any write operations be interpreted as overwriting the contents of the file or appending to the file?
·         The access, indicating how you want access to file; for example, do you want to read or write to the file or do both.

Reading and Writing to Text Files
·         Streamreader Class
·         Streamwriter Class

Writing to a text file
class Test
{
    public static void Main()
    {
        // Create an instance of StreamWriter to write text to a file.
        // The using statement also closes the StreamWriter.
        using (StreamWriter sw = new StreamWriter("TestFile.txt"))
        {
            // Add some text to the file.
            sw.Write("This is the ");
            sw.WriteLine("header for the file.");
            sw.WriteLine("-------------------");
            // Arbitrary objects can also be written to the file.
            sw.Write("The date is: ");
            sw.WriteLine(DateTime.Now);
        }
    }
}

Reading text from a file
class Test
{
    public static void Main()
    {
        try
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt"))
            {
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}

No comments:

Post a Comment