Advertisement
Methods | Description |
---|---|
FileStream Create(String path) | This method creates or overwrites a file in the specified path and it returns a FileStream object that provides read/write access to the specified file. |
FileStream Open(String path, FileMode mode) | This method opens a FileStream on the specified path with read/write access with no sharing. The FileMode value specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten. |
void Move(String sourceFileName, String destFileName) | This method moves a specified file to a new location, providing the option to specify a new file name at the destination. |
void Delete(String path) | This method deletes the file specified by the path. |
void Copy(String sourceFileName, String destFileName) | This method copies an existing file to a new file. You are not allowed to overwrite a file of the same name. |
FileStream OpenRead(String path) | This method opens an existing file for reading and it returns a FileStream object that provides read access to the specified file. |
FileStream OpenWrite(String path) | This method opens an existing file or create a new file for writing and it returns an unshared FileStream object that provides read access to the specified file. |
bool Exists(String path) | This method determines whether the specified file exists or not. |
DateTimeGetCreationTime(String path) | This method returns the creation time of the specified file or directory. |
void SetCreationTime(String path, DateTime time) | This method sets the creation time the specified file or directory. |
DateTime GetLastAccessTime(String path) | This method returns the last access time of the specified file or directory. |
void SetLastAccessTime(String path, DateTime time) | This method sets the last access time of the specified file or directory was written to. |
DateTime GetLastWriteTime(String path) | This method returns the last access time of the specified file or directory was written to. |
void SetLastWriteTime(String path, DateTime time) | This method sets the last time the specified file or directory was written to. |
bool Exists(String path) | This method determines whether a specified file exists or not. |
String[] ReadAllLines(String path) | This method opens a text file, reads all lines from it and then closes the file. |
String[] ReadAllLines(String path, String[] contents) | This method create a new file, writes a String array to it and closes it. |
//C# Using the File class to create a file and write to it
using System;
using System.IO;
using System.Text;
class A
{
public static void Main(String[] ar)
{
try
{
//Calling the Create() method of File
//Which creates a new specified file
//and returns a FileStream object to write to the file
FileStream fs = File.Create("D:\\FileEx1.txt");
//Creating a String object
String str = "Live your life to the fullest!";
//Creating a byte array from a String
byte[] bA = Encoding.ASCII.GetBytes(str);
//Writing to a file(one byte a time)
//using the FileStream object.
foreach(byte bt in bA)
{
//writing one byte at a time to the file.
fs.WriteByte(bt);
}
fs.Flush();
fs.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Live your life to the fullest!
Advertisement
//C# Using the File class to open and read an existing file
//We will call the OpenRead(String path) method of File class
//which returns us the FileStream object to read the file
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//Calling the OpenRead(String path) method of File
//which opens the specified path with read access to it
//and returns a FileStream object to read the file
FileStream fs = File.OpenRead("D:\\FileEx1.txt");
int c;
//Calling the ReadByte() method of FileStream to reading one byte out of a file
Console.WriteLine("Reading the contents of a file: ");
while((c = fs.ReadByte()) !=-1)
{
//Casting byte to char for displaying the read character on the screen
Console.Write((char)c);
}
//Closing the byte stream i.e. FileStream
fs.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Reading the contents of a file:
Live your life to the fullest
Live your life to the fullest!
//C# Using the File to move a file to another location(also providing it a new name)
//Next, we will use the File class to open and read the newly moved file
//By calling the OpenRead(String path) method of File class
//which returns us the FileStream object to read the file
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//Calling the Move() method of File
//which moves a file present in D: Drive to E: Drive and with a new name
File.Move("D:\\FileEx1.txt", "E:\\E-File1.txt");
//Calling the OpenRead() method of File
//To read the newly moved file
FileStream fs = File.OpenRead("E:\\E-File1.txt");
int c;
//Calling the ReadByte() method of FileStream to reading one byte out of a file
Console.WriteLine("Reading the contents of the newly moved file: ");
while((c = fs.ReadByte()) !=-1)
{
//Casting byte to char for displaying the read character on the screen
Console.Write((char)c);
}
//Closing the byte stream i.e. FileStream
fs.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Reading the contents of the newly moved file:
Live your life to the fullest
Advertisement
Live your life to the fullest!
//C# Using the File to copy a file to another location(also providing it a new name)
//Next, we will use the File class to open and read the newly copied file
//By calling the OpenRead(String path) method of File class
//which returns us the FileStream object to read the file
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//Calling the Move() method of File
//which copies a file present in E: Drive to E: Drive's 'New Folder' and with a new name
File.Copy("E:\\E-File1.txt", "E:\\E-File2.txt");
//Calling the OpenRead() method of File
//To read the copy of the file
FileStream fs = File.OpenRead("E:\\E-File2.txt");
int c;
//Calling the ReadByte() method of FileStream to reading one byte out of a file
Console.WriteLine("Reading the contents of the copy of the file: ");
while((c = fs.ReadByte()) !=-1)
{
//Casting byte to char for displaying the read character on the screen
Console.Write((char)c);
}
//Closing the byte stream i.e. FileStream
fs.Close();
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Reading the contents of the copy of the file:
Live your life to the fullest!
//C# Using the File to find get and set the file information
//such as its last access time, creating time and last write time.
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
Console.WriteLine("Getting the file specific information about the file 'E-File1.txt' in E Drive");
//Getting the creating time of the specified file
DateTime d = File.GetCreationTime("E:\\E-File1.txt");
Console.WriteLine("Creation time of the file: " + d.ToString());
//Getting the last write time of the specified file
d = File.GetLastWriteTime("E:\\E-File1.txt");
Console.WriteLine("Last write time of the file: " + d.ToString());
//Getting the last access time of the specified file
d = File.GetLastAccessTime("E:\\E-File1.txt");
Console.WriteLine("Last access time of file: " + d.ToString());
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Getting the file specific information about the file 'E-File1.txt' in E Drive
Creation time of the file: 17-01-2020 13:35:19
Last write time of the file: 17-01-2020 13:01:35
Last access time of file: 17-01-2020 13:49:52
//C# Calling the Delete() method of File class to delete an existing file
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//Calling the Delete() method of File class to delete an existing file
File.Delete("E:\\E-File2.txt");
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement