Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Writing Files | Additional Structures & File Handling
course content

Зміст курсу

C# Beyond Basics

Writing FilesWriting Files

For writing text in a file we create a StreamWriter object with the path of the file, similar to how we create a StreamReader object.

The platform currently doesn't support file writing so this can be practiced on a compiler on your PC.

The StreamWriter object offers a WriteLine method which can write text into a file. We will now try to write something to the text.txt file.

cs

index.cs

The above code will overwrite the previous content of the text.txt file and replace it with:

If the targeted file does not exist, it will create a new file with the specified name and add text to it so this approach can also be used for creating new files. However if the required folders in the path do not exist then it will give a runtime error causing the program to crash.

StreamWriter has a second parameter called "append" which takes in a boolean value and is set to false by default. If we set the append parameter to true then the code will not overwrite the older text rather it would append the new text to the end of the file.

For-example, consider the file text.txt has the following text in it:

txt

text.txt

We will use the following code to add a new line to it:

cs

index.cs

The file will look like this after running the code:

txt

text.txt

1. What is the correct way of creating a StreamWriter object for appending text into a file?
2. What method is used to write a line to a text file using the StreamWriter class?
3. Which method is used for closing a file after use?

What is the correct way of creating a StreamWriter object for appending text into a file?

Виберіть правильну відповідь

What method is used to write a line to a text file using the StreamWriter class?

Виберіть правильну відповідь

Which method is used for closing a file after use?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 8
course content

Зміст курсу

C# Beyond Basics

Writing FilesWriting Files

For writing text in a file we create a StreamWriter object with the path of the file, similar to how we create a StreamReader object.

The platform currently doesn't support file writing so this can be practiced on a compiler on your PC.

The StreamWriter object offers a WriteLine method which can write text into a file. We will now try to write something to the text.txt file.

cs

index.cs

The above code will overwrite the previous content of the text.txt file and replace it with:

If the targeted file does not exist, it will create a new file with the specified name and add text to it so this approach can also be used for creating new files. However if the required folders in the path do not exist then it will give a runtime error causing the program to crash.

StreamWriter has a second parameter called "append" which takes in a boolean value and is set to false by default. If we set the append parameter to true then the code will not overwrite the older text rather it would append the new text to the end of the file.

For-example, consider the file text.txt has the following text in it:

txt

text.txt

We will use the following code to add a new line to it:

cs

index.cs

The file will look like this after running the code:

txt

text.txt

1. What is the correct way of creating a StreamWriter object for appending text into a file?
2. What method is used to write a line to a text file using the StreamWriter class?
3. Which method is used for closing a file after use?

What is the correct way of creating a StreamWriter object for appending text into a file?

Виберіть правильну відповідь

What method is used to write a line to a text file using the StreamWriter class?

Виберіть правильну відповідь

Which method is used for closing a file after use?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 8
some-alt