### Read Textfile to String

To read the content of a textfile into a string, use the TextReader method.
Example loading content into a String
Dim tr As TextReader
Try
tr.Initialize(File.OpenInput(File.DirApp, "myfilename.txt") )
Dim strText As String = tr.ReadAll
Catch
Log("Error loading form from file:" & LastException.Message)
End Try
Example as a Sub
' Get the content of a file and assign to a string
' Parameter: Full path to the file
' Returns the content of the file as string
`Public Sub FileToString(path As String) As String
Dim result As String = ""
Dim tr As TextReader
Try
tr.Initialize(File.OpenInput(GetFilePath(path), GetFileName(path)) )
result = tr.ReadAll
Catch
Log("Error loading form from file:" & LastException.Message)
End Try
Return result
End Sub`
' Returns the filename from a fullpath
`Public Sub GetFileName(path As String) As String
' Define the JavaObject
Dim fileO As JavaObject
' Get the fileio class
fileO.InitializeNewInstance("java.io.File", Array As Object(path))
' Getthe filename
Return fileO.RunMethod("getName", Null)
End Sub`
' Returns the filepath from a fullpath
`Public Sub GetFilePath(path As String) As String
' Define the JavaObject
Dim fileO As JavaObject
' Get the fileio class
fileO.InitializeNewInstance("java.io.File", Array As Object(path))
' Get the filename and path
Dim n As String = fileO.RunMethod("getName", Null)
Dim p As String = fileO.RunMethod("getPath", Null)
p = p.Replace(n, "")
Return p
End Sub`
Example loading HTML file into a String and replace a Placeholder
`Sub loadFormFromFile
Dim tr As TextReader
Try
tr.Initialize(File.OpenInput(File.DirApp, "feedbackform.html") )
Dim strForm As String = tr.ReadAll
' Replace #date# placeholder by current date & time
strForm = strForm.Replace("#date#", DateTime.Date(DateTime.now) & " " & DateTime.Time(DateTime.now))
wvwMain.LoadHtml(strForm)
Catch
Log("Error loading form from file:" & LastException.Message)
End Try
End Sub`

Changed: , Category: TextReader and TextWriter, File: T6C263M1954.htm, ID: 1954