Skip to content

Instantly share code, notes, and snippets.

@lucamauri
Created December 22, 2013 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucamauri/8089041 to your computer and use it in GitHub Desktop.
Save lucamauri/8089041 to your computer and use it in GitHub Desktop.
A collection of three simple class that allow you to read files from disk and from internet and write a stream back to a file. It is a simple task for a programmer, but it may comes in hand from time to time to have it ready-to-use.
Imports System.IO
Imports System.Text
Imports System.Net
Module modText
Function textFromInternet(ByVal address As String) As StringBuilder
Dim Request As WebRequest
Dim Response As WebResponse
Dim Reader As StreamReader
Dim Line As String
Dim page As StringBuilder
Try
page = New StringBuilder
Request = WebRequest.Create(address)
Response = Request.GetResponse
Reader = New StreamReader(Response.GetResponseStream, System.Text.Encoding.UTF8)
Line = Reader.ReadLine
Do
page.Append(Line & Environment.NewLine)
Line = Reader.ReadLine
Loop Until Line Is Nothing
Return page
Catch ex As Exception
Return Nothing
Finally
If Reader IsNot Nothing Then
Reader.Close()
End If
End Try
End Function
Function textFromFile(ByVal filePath As String) As StringBuilder
Dim sourceFile As New StreamReader(filePath, System.Text.Encoding.UTF8)
Dim page As New StringBuilder
Dim line As String
Try
Do
line = sourceFile.ReadLine
page.Append(line & Environment.NewLine)
Loop Until line Is Nothing
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
sourceFile.Close()
End Try
End Function
Function textToFile(ByVal destFile As String, ByVal data As String) As Boolean
Dim outputFile As StreamWriter
Try
outputFile = New StreamWriter(destFile, True)
outputFile.Write(data)
outputFile.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment