MorkaLork Development

Interesting stuff I've picked up over the years...

Input/Output 1: The basics

2009-04-16 17:52:46 | 418 views | files save save data data library input output stream text textfile

The Basics



When you write program you will often find yourself in need to save data in some form. It might just be a username or a setting, but it might also be names and addresses or other data.
You might not always need an entire SQL database so you want to put it into a textfile(.txt).

C# makes it quite easy to work with input/output and it utilizes a Stream class.

Getting into it



The first thing you have to do is add the correct library at the top:



using System.IO;


This contains all classes we will need to read and write data to file.

Now, the code to write and read a file can be simple, like this:



using System;
using System.IO;

namespace IO
{
class Program
{
public static void Main(string[] args)
{
//Specify a filestream containing the filename
FileStream fs = new FileStream("path.txt", FileMode.OpenOrCreate, FileAccess.Write);

//Create a stream to write to
StreamWriter sw = new StreamWriter(fs);

//Write
sw.WriteLine("Hello world!");
sw.WriteLine("What's hanging?");

//Close the stream
sw.Close();

//Close the file
fs.Close();

//Specify the the file to read from
fs = new FileStream("path.txt", FileMode.OpenOrCreate, FileAccess.Read);

//Create a stream to read to
StreamReader sr = new StreamReader(fs);

//Read the file
string theText = sr.ReadToEnd();

Console.WriteLine(theText);

sr.Close();

fs.Close();

Console.Read();
}
}
}



Advanced



Filestream:


FileStream fs = new FileStream("path.txt", FileMode.OpenOrCreate, FileAccess.Write);

This either creates a file if it doesn't exist or opens it if it already exists. It uses the FileMode, FileAccess enumeration.
These are the FileMode enumerations:

FileMode EnumerationDescription
Create FileMode.Create value creates a file and if the file already exists, it will be overwritten.
CreateNew Creates a file and if the file already exists, an IOException is thrown.
Append Opens the file and starts writing at the end of the file. If the file doesn't exist a file is created. Note that you can use FileMode.Append only with FileAccess.Write or an exception of type ArgumentException is thrown.
Open Opens the file if it exists. If the file does not exist, an exception of type FileNotFoundException is thrown.
OpenOrCreate Opens the file if it exists. If the file does not exist, the file will be created.
Truncate Opens a file and truncates it to make it a size of zero bytes.


These are the FileAccess enumerations:

FileAccess EnumerationDescription
Read Sets read access to the file.
Write Sets write access to the file.
ReadWrite Sets read/write access to the file.


Then there is a third enumerator, FileShare, that provides values for how the file is shared with other streams by the running process or other processes.

FileShare EnumerationDescription
None Obtains an exclusive access to the file until it's closed, which means that other streams can't read from or write to the file until this stream is closed.
Read Other streams (in the current process or in another one) can obtain read access to the file.
Write Other streams (in the current process or in another one) can obtain write access to the file
ReadWrite Other streams can obtain ReadWrite access to the file





Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.