StreamReader and StreamWriter class

Dealing with File Handling with the help of StreamReader and StreamWriter class



Imports System
'System.IO namespace is used to deal with the file input output hanlding.
Imports System.IO
'Imports System.Text

Module Module3

Public Class textEx
Public Shared Sub main()
'Path is a variable that store path of file.
Dim path As String = "c:\test.txt"
'File is a class that contain Exists method and it takes argument as path and return true or false.
'If file exists then return true else return false.
If File.Exists(path) = False Then
'StreamWriter is a class that is used to write the text in the file.


Dim sw As StreamWriter = File.CreateText(path)
sw.WriteLine("Hello")
sw.WriteLine("Hello How are you")
'Flush method is used to clear all the buffer for the current writer.
sw.Flush()
'Closes the streamWriter object
sw.Close()
Else
System.Console.WriteLine("File Already Present")

End If
Try
'StreamReader is used to read the data from file.
'File.OpenText(Path_of_file) is used to open file in reading mode.
Dim sr As StreamReader = File.OpenText(path)
'Peek method returns the next character in the stream and return negative values when reaches to end of file.
Do While sr.Peek >= 0
Console.WriteLine(sr.ReadLine)
Loop
sr.Close()
Dim path2 As String = "c:\one.txt"
'File.Delete(File_Path) is used to delete particular file.
File.Delete(path2)
'File.Copy(Source,Destination) is used to copy the file.
File.Copy(path, path2)
Console.WriteLine("{0} was copied to {1}", path, path2)


Catch ex As Exception
Console.WriteLine("The Process Fail :{0}", ex.ToString())

End Try
End Sub
End Class
End Module


regards
Neetu


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: