C # File Handling
C # File Handling
using System;
using System.IO;
public class FileStreamExample
{
public static void Main(string[] args)
{
FileStream f = new FileStream("e:\\b.txt", FileMode.OpenOrCreate);//cre
ating file stream
f.WriteByte(65);//writing byte into stream
f.Close();//closing stream
}
}
using System;
using System.IO;
public class FileStreamExample
{
public static void Main(string[] args)
{
FileStream f = new FileStream("e:\\b.txt", FileMode.OpenOrCreate);
int i = 0;
while ((i = f.ReadByte()) != -1)
{
Console.Write((char)i);
}
f.Close();
}
}
C# StreamWriter
C# StreamWriter class is used to write characters to a stream in specific
encoding. It inherits TextWriter class. It provides overloaded write() and
writeln() methods to write data into file.
C# StreamWriter example
Let's see a simple example of StreamWriter class which writes a single line
of data into the file.
using System;
using System.IO;
public class StreamWriterExample
{
public static void Main(string[] args)
{
FileStream f = new FileStream("e:\\output.txt", FileMode.Create);
StreamWriter s = new StreamWriter(f);
s.WriteLine("hello c#");
s.Close();
f.Close();
Console.WriteLine("File created successfully...");
}
}
C# StreamReader
C# StreamReader class is used to read string from the stream. It inherits
TextReader class. It provides Read() and ReadLine() methods to read data
from the stream.
using System;
using System.IO;
public class StreamReaderExample
{
public static void Main(string[] args)
{
FileStream f = new FileStream("e:\\output.txt", FileMode.OpenOrCreate);
string line=s.ReadLine();
Console.WriteLine(line);
s.Close();
f.Close();
}
}
Output:
Hello C#
this is file handling
C# TextWriter
C# TextWriter class is an abstract class. It is used to write text or sequential
series of characters into file. It is found in System.IO namespace.
C# TextWriter Example
Let's see the simple example of TextWriter class to write two lines data.
using System;
using System.IO;
namespace TextWriterExample
{
class Program
{
static void Main(string[] args)
{
using (TextWriter writer = File.CreateText("e:\\f.txt"))
{
writer.WriteLine("Hello C#");
writer.WriteLine("C# File Handling by JavaTpoint");
}
Console.WriteLine("Data written successfully...");
}
}
}
C# TextReader
C# TextReader class is found in System.IO namespace. It represents a reader that can be
used to read text or sequential series of characters.
using System;
using System.IO;
namespace TextReaderExample
{
class Program
{
static void Main(string[] args)
{
using (TextReader tr = File.OpenText("e:\\f.txt"))
{
Console.WriteLine(tr.ReadToEnd());
}
}
}
}
C# StringWriter Class
This class is used to write and deal with string data rather than files. It is
derived class of TextWriter class. The string data written by StringWriter
class is stored into StringBuilder.
The purpose of this class is to manipulate string and save result into the
StringBuilder.
C# StringWriter Constructors
Constructors Description
Property Description
C# StringWriter Methods
Methods Description
C# StringWriter Example
In the following program, we are using StringWriter class to write string information
to the StringBuilder class. The StringReader class is used to read written
information to the StringBuilder.
using System;
using System.IO;
using System.Text;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
string text = "Hello, Welcome to the javatpoint \n" +
"It is nice site. \n" +
"It provides technical tutorials";
// Creating StringBuilder instance
StringBuilder sb = new StringBuilder();
// Passing StringBuilder instance into StringWriter
StringWriter writer = new StringWriter(sb);
// Writing data using StringWriter
writer.WriteLine(text);
writer.Flush();
// Closing writer connection
writer.Close();
// Creating StringReader instance and passing StringBuilder
StringReader reader = new StringReader(sb.ToString());
// Reading data
while (reader.Peek() > -1)
{
Console.WriteLine(reader.ReadLine());
}
}
}
}
C# StringReader Class
StringReader class is used to read data written by the StringWriter class. It is
subclass of TextReader class. It enables us to read a string synchronously or
asynchronously. It provides constructors and methods to perform read operations.
C# StringReader Signature
1. [SerializableAttribute]
2. [ComVisibleAttribute(true)]
3. public class StringReader : TextReader
C# StringReader Constructors
StringReader has the following constructors.
Constructors Description
C# StringReader Methods
Following are the methods of StringReader class.
next →← prev
C# StringReader Class
StringReader class is used to read data written by the StringWriter class. It is subclass of
TextReader class. It enables us to read a string synchronously or asynchronously. It provides
constructors and methods to perform read operations.
C# StringReader Signature
1. [SerializableAttribute]
2. [ComVisibleAttribute(true)]
3. public class StringReader : TextReader
C# StringReader Constructors
StringReader has the following constructors.
Constructors Description
C# StringReader Methods
Following are the methods of StringReader class.
Method Description
C# StringReader Example
In the following example, StringWriter class is used to write the string information and
StringReader class is used to read the string, written by the StringWriter class.
1. using System;
2. using System.IO;
3. namespace CSharpProgram
4. {
5. class Program
6. {
7. static void Main(string[] args)
8. {
9. StringWriter str = new StringWriter();
10. str.WriteLine("Hello, this message is read by StringReader class");
11. str.Close();
12. // Creating StringReader instance and passing StringWriter
13. StringReader reader = new StringReader(str.ToString());
14. // Reading data
15. while (reader.Peek() > -1)
16. {
17. Console.WriteLine(reader.ReadLine());
18. }
19. }
20. }
21.}
Output:
C# FileInfo Class
The FileInfo class is used to deal with file and its operations in C#. It provides
properties and methods that are used to create, delete and read file. It uses
StreamWriter class to write data to the file. It is a part of System.IO namespace.
C# FileInfo Constructors
The following table contains constructors for the FileInfo class.
Constructor Description
FileInfo(String) It is used to initialize a new instance of the FileInfo class which acts as
a wrapper for a file path.
C# FileInfo Properties
The following table contains properties of the FileInfo class.
Properties Description
Attributes It is used to get or set the attributes for the current file or directory.
CreationTime It is used to get or set the creation time of the current file or
directory.
IsReadOnly It is used to get or set a value that determines if the current file is
read only.
LastAccessTime It is used to get or set the time from current file or directory was last
accessed.
C# FileInfo Methods
The following table contains methods of the FileInfo class.
Method Description
Replace(String,String) It is used to replace the contents of a specified file with the file
described by the current FileInfo object.
Output:
We can see inside the F drive a file abc.txt is created. A screenshot is given below.
C# FileInfo Example: writing to the file
using System;
using System.IO;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
try
{
// Specifying file location
string loc = "F:\\abc.txt";
// Creating FileInfo instance
FileInfo file = new FileInfo(loc);
// Creating an file instance to write
StreamWriter sw = file.CreateText();
// Writing to the file
sw.WriteLine("This text is written to the file by using StreamWriter class.");
sw.Close();
}catch(IOException e)
{
Console.WriteLine("Something went wrong: "+e);
}
}
}
}
Output:
Output:
C# DirectoryInfo Class
DirectoryInfo class is a part of System.IO namespace. It is used to create, delete
and move directory. It provides methods to perform operations related to directory
and subdirectory. It is a sealed class so, we cannot inherit it.
The DirectoryInfo class provides constructors, methods and properties that are
listed below.
C# DirectoryInfo Syntax
1. [SerializableAttribute]
2. [ComVisibleAttribute(true)]
3. public sealed class DirectoryInfo : FileSystemInfo
C# DirectoryInfo Constructors
The following table contains the constructors for the DirectoryInfo class.
Constructor Description
C# DirectoryInfo Properties
The following table contains the properties of the DirectoryInfo class.
Property Description
Attributes It is used to get or set the attributes for the current file or
directory.
CreationTime It is used to get or set the creation time of the current file or
directory.
Extension It is used to get the string representing the extension part of the
file.
LastAccessTime It is used to get or set the time the current file or directory was
last accessed.
LastWriteTime It is used to get or set the time when the current file or directory
was last written.
LastWriteTimeUtc It is used to get or set the time, in coordinated universal time
(UTC), when the current file or directory was last written.
C# DirectoryInfo Methods
The following table contains the methods of the DirectoryInfo class.
Method Description
C# DirectoryInfo Example
In the following example, we are creating a javatpoint directory by specifying the
directory path.
using System;
using System.IO;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
// Provide directory name with complete location.
DirectoryInfo directory = new DirectoryInfo(@"F:\javatpoint");
try
{
// Check, directory exist or not.
if (directory.Exists)
{
Console.WriteLine("Directory already exist.");
return;
}
// Creating a new directory.
directory.Create();
Console.WriteLine("The directory is created successfully.");
}
catch (Exception e)
{
Console.WriteLine("Directory not created: {0}", e.ToString());
}
}
}
}
Output:
The DirectoryInfo class also provides a delete method to delete created directory.
In the following program, we are deleting a directory that we created in previous
program.
Output: