### Use java.io.File Class

The Java IO class is decribed here:
http://docs.oracle.com/javase/8/docs/api/java/io/File.html
Examples
' Returns the canonical pathname string of this abstract pathname.
`Sub GetCanonicalPath(Path As String) As String
Dim fileO As JavaObject
fileO.InitializeNewInstance("java.io.File", Array As Object(Path))
Return fileO.RunMethod("getCanonicalPath", Null)
End Sub`
' Returns the absolute pathname string of this abstract pathname.
`Sub GetAbsolutePath(Path As String) As String
Dim fileO As JavaObject
fileO.InitializeNewInstance("java.io.File", Array As Object(Path))
Return fileO.RunMethod("getPath", Null)
End Sub`
' Get the last modification date & time for a file
' Parameter Path full filename
' Returns DateTime last modified as Long
' Example:
' Dim lm As Long = GetLastModified(File.DirApp & "\a.txt")
' Log(DateTime.Date(lm) & " " & DateTime.Time(lm))
`Sub GetLastModified(Path As String) As Long
Dim fileO As JavaObject
fileO.InitializeNewInstance("java.io.File", Array As Object(Path))
Return fileO.RunMethod("lastModified", Null)
End Sub`
Example Calls
' Filepath is returned as short filename (8.3)
Dim strFolder As String = File.DirApp
Log("Filepath0:" & strFolder)
Output:
Filepath0:C:\Daten\b4j\OPENSO~1\ROSAND~1\Objects
' Filepath is returned as long filename
Log("Filepath1:" & GetCanonicalPath(File.GetFileParent(strFolder)))
Output:
Filepath1:C:\Daten\b4j\opensource\rosandbox
' Filepath is returned as long filename with file:/ as prefix
Log("Filepath2:" & File.GetUri(GetCanonicalPath(File.GetFileParent(strFolder)),""))
Output:
Filepath2:file:/C:/Daten/b4j/opensource/rosandbox/
' Filepath is returned as long filename
Log("Filepath3:" & GetAbsolutePath(File.GetFileParent(strFolder)))
Output:
Filepath3:C:\Daten\b4j\OPENSO~1\ROSAND~1
Log("Filepath4:" & GetCanonicalPath(File.DirApp))
Output:
Filepath4:C:\Daten\b4j\opensource\rosandbox\Objects
Using getName and getPath
' Set the filename
Dim mediaFile As String = "C:\Daten\b4j\b4jHowTo\B4JHowToPlayMedia\Objects\test1.mp3"
' Define the JavaObject
Dim fileO As JavaObject
' Get the fileio class
fileO.InitializeNewInstance("java.io.File", Array As Object(mediaFile))
' And use API methods
Log("getName:" & fileO.RunMethod("getName", Null))
Output:
getName:test1.mp3
Log("getPath:" & fileO.RunMethod("getPath", Null))
Output:
getPath:C:\Daten\b4j\b4jHowTo\B4JHowToPlayMedia\Objects\test1.mp3
To split filename and filepath
' Set the filename
Dim mediaFile As String = "C:\Daten\b4j\b4jHowTo\B4JHowToPlayMedia\Objects\test1.mp3"
Dim mediaFolder As String
' Define the JavaObject
Dim fileO As JavaObject
' Get the fileio class
fileO.InitializeNewInstance("java.io.File", Array As Object(mediaFile))
' And use API methods
mediaFile = fileO.RunMethod("getName", Null)
mediaFolder = fileO.RunMethod("getPath", Null)
mediaFolder = mediaFolder.Replace(mediaFile, "")
Output:
mediaFile:test1.mp3
mediaFolder:C:\Daten\b4j\b4jHowTo\B4JHowToPlayMedia\Objects\

Changed: , Category: Files, File: T6C193M1400.htm, ID: 1400