ARTICLE

FileStream : Read or Write Data Streams

Posted by Mahesh Chand Articles | C# Language December 13, 2000
FileStream class can be used to read/write data to a file with the help of StreamReader and StreamWriter classes.
Reader Level:

Tools Used: Visual C# .NET

The FileStream class is derived from Stream class. This class can be used for reading from and writing to files such as bytes, characters, strings, and other data-type values to a file. Other class I have used in this sample is StreamWriter. The StreamWriter class's Write and WriteLine members write to a file.

In this sample code, I have used FileStream class to create a text file and StreamWrite to write text to the text file.

namespace mcFileStream
{
using System;
using System.IO;
public class mcLogFile
{
public static void Main(String[] args)
{
// Create a text file C:\temp\mcb.txt
FileStream fs = new FileStream(@"c:\temp\mcb.txt" , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter =
new StreamWriter(fs);
// Write to the file using StreamWriter class
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.Write(" File Write Operation Starts : ");
m_streamWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
m_streamWriter.WriteLine(" First Line : Data is first line \n");
m_streamWriter.WriteLine(" This is next line in the text file. \n ");
m_streamWriter.Flush();
// Read from the file using StreamReader class
// StreamReader m_streamReader = new StreamReader(fs);
// string str = m_streamReader.ReadLine();
}
}
}

Login to add your contents and source code to this article
post comment
     

@Nidhi, you need to make sure you have read/write permissions on mcb.txt file and the file is there. Also make sure to Close the file everytime you open it. I am going to update this article and put Open and Close within a try..catch block.

Posted by Mahesh Chand May 30, 2012

Ahmed, If you would have used the search feature, you could have found this. This article was posted 12 years ago.

Posted by Mahesh Chand May 30, 2012

Hi, I think it was really waste of time as my question was posted in 2010 and the response came after two years it is the longest response I'v ever seen. Thanks mate

Posted by Ahmed Aldabbagh May 17, 2012

FileStream fs = new FileStream(@"c:\\mcb.txt", FileMode.Create, FileAccess.Write);StreamWriter sw = new StreamWriter(fs);Console.WriteLine("Please enter your information");string str = Console.ReadLine();sw.WriteLine(str);sw.Close();fs.Close(); Console.ReadLine(); My program give me an error "Access to the path 'c:\mcb.txt' is denied." Pls tell me what to do if possible so send me ans at my accout i.e. nidhibisht122@gmail.com pls reply as soon as possible

Posted by nidhi bisht May 17, 2012

Hi Mahesh,

Regarding this coding: What Project Reference should be used ?

FileStream : Read or Write Data Streams

Posted by Lennie Kuah Sep 26, 2010
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts