Method Create (), Open () method - Object-oriented programming

Create () method

The Create () method of the Filelnfo class is the main method for creating new files. It returns a reference to an object of type FileStream, which can perform read/write operations on the created file. After you finish working with the FileStream object, you must close this object (stream) to free up the resource:

// Create a new file on device C:

Filelnfo f = new Filelnfo (@ C: Test.dat );

FileStream fs = f.Create ();

//. . . we are working with a new file

// Close the file stream

fs.Close ();

Open () method

To open an existing file or create a new file, you can use the Open () method of the Filelnfo class. The Open () method allows you to describe the created file more accurately than the Create () method. The result of executing the Open () method is a reference to the FileStream object:

// Create a new file using the Open method ()

Filelnfo f2 = new Filelnfo (@ C: Test2.dat );

FileStream fs2 = f2.0pen (FileMode.OpenOrCreate,

FileAccess.ReadWrite, FileShare.None);

// ...

fs2.Close ();

There are several overloaded Open () methods. The main variant of this method takes three parameters.

The first parameter describes the general information about opening a file (for example: creating a new file, opening an existing file, adding data to the end of the file, etc.), which is set using the FileMode enumeration (Table 9.4):

public enum FileMode {CreateNew, Create, Open, OpenOrCreate, Truncate, Append}

The second parameter is the value of the FileAccess enumeration that defines the types of I/O operations that are performed: public enum FileAccess {Read, Write, Read Write}

Finally, the third parameter, FileShare, specifies the mode of working with this file with different applications: public enum FileShare {None, Read, Write, ReadWrite}

Table 9.4

FileMode enumeration elements




Create a new file. If a file with this name already exists, an IOException will be thrown


Create a new file. If a file with this name already exists, it will be overwritten


Open an existing file. If such file is not present, the exception FileNotFoundException will be generated


open the file if it already exists; if not, then it is created


opening a file and deleting its contents to a size of 0 bytes


open the file, move to the end of the file content and go into the write mode (this value can only be used with a write-only stream).

If there is no such file, it is created

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)