### Open & Save File Dialog

Examples of using FileChooser ShowOpen and ShowSave.
Helper to obtain filename and fileapth. These methods require the JavaObject Library
' Returns the filename from a fullpath
`Public Sub File_GetName(path As String) As String
'Define the JavaObject
Dim joFile As JavaObject
'Get the fileio class
joFile .InitializeNewInstance("java.io.File", Array As Object(path))
'Get the filename and return
Return joFile .RunMethod("getName", Null)
End Sub`
' Returns the filepath from a fullpath
`Public Sub File_GetPath(path As String) As String
'Define the JavaObject
Dim joFile As JavaObject
'Get the fileio class
joFile.InitializeNewInstance("java.io.File", Array As Object(path))
'Get the filename and path
Dim n As String = joFile.RunMethod("getName", Null)
Dim p As String = joFile.RunMethod("getPath", Null)
p = p.Replace(n, "")
'Return the path
Return p
End Sub`
Load the content of a text file to a string
' Dim str As String = TextFileToString(MainForm)
`Private Sub TextFileToString(Owner As Form) As String
Log("Textfile to string")
Dim Result As String = ""
Dim Filename As String
Dim FileChoose As FileChooser
FileChoose.Initialize
FileChoose.InitialDirectory = File.DirApp
FileChoose.Title = "Select a File"
Filename = FileChoose.ShowOpen(Owner)
If Filename.Length = 0 Then Return Result
Try
Result = File.ReadString(File_GetPath(Filename), File_GetName(Filename))
Catch
Log("[ERROR] Read Text File:" & LastException.Message)
End Try
Return Result
End Sub`
Save Content TextArea To a TextFile
' Dim Str As String = "Hello World"
' StringToTextFile(MainForm, Str)
`Private Sub StringToTextFile(Owner As Form, Str As String)
Dim Filename As String
Dim FileChoose As FileChooser
If Str.Trim.Length = 0 Then Return
FileChoose.Initialize
FileChoose.InitialDirectory = File.DirApp
FileChoose.InitialFileName = "content.txt"
FileChoose.Title = "Save File"
Filename = FileChoose.ShowSave(Owner)
If Filename.Length = 0 Then Return
Try
File.WriteString(File_GetPath(Filename), File_GetName(Filename), Str)
Catch
Log("[ERROR] Write Text File: " & LastException.Message)
End Try
End Sub`
Subs using TextReader and TextWriter
These methods are not recommended - use File.Read... and File.Write...
Select a Textfile and load content into TextArea
`Private Sub openTextFile
Dim f As String
Dim fc As FileChooser
fc.Initialize
fc.InitialDirectory = File.DirApp
fc.InitialFileName = "test.txt"
fc.Title = "Select a File"
f = fc.ShowOpen(MainForm) ' Show the dialog and select
If f.Length = 0 Then Return
Try
Dim tr As TextReader
tr.Initialize(File.OpenInput(File.DirApp, "test.txt"))
txtTextArea.Text = tr.ReadAll
tr.Close
Catch
Log("Read Text File: Error " & LastException.Message)
If tr.IsInitialized Then tr.Close
End Try
End Sub`
Save Content TextArea to a TextFile
`Private Sub saveTextFile
Dim fc As FileChooser
fc.Initialize
fc.InitialDirectory = File.DirApp
fc.InitialFileName = "test.txt"
fc.Title = "Save File"
Dim f As String = fc.ShowSave(MainForm) ' Show the dialog and select
If f.Length = 0 Then Return
Try
Dim tw As TextWriter
tw.Initialize(File.OpenOutput(File.DirApp, f, False))
tw.Write(txtTextArea.Text)
tw.Flush
tw.Close
Catch
Log("Write Text File: Error " & LastException.Message)
If tw.IsInitialized Then tw.Close
End Try
End Sub`
Hint on getting filename and filepath from the full path
as returned by fc.ShowOpen or fc.ShowSave. This requires the JavaObject to include.
...
Dim f As String = fc.ShowSave(MainForm) ' Show the dialog and select
...
Dim filepath As String = getFilePath(f)
Dim filename As String = getFileName(f)
' Returns the filename from a fullpath
`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
`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))
' Getthe 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`

Changed: 2018-04-08 09:47, Category: Files, File: T6C193M1386.htm, ID: 1386