Skip to content

kiwi.io.File

Nikos Siatras edited this page Oct 8, 2022 · 7 revisions

The File class is an abstract representation of file and directory pathnames. Kiwi's File type can be found under kiwi/io/File.bi path

File.isFile

function File.isFile() as Boolean

Tests whether the file denoted by this abstract pathname is a normal file.
@return true if and only if the file denoted by this abstract pathname exists and is a normal file, otherwise return false

File.isDirectory

function File.isFile() as Boolean

Tests whether the file denoted by this abstract pathname is a normal directory. @return true if and only if the file denoted by this abstract pathname exists and is a directory, otherwise return false

File.exists

function File.exists() as Boolean

Tests whether the file or directory denoted by this abstract pathname exists. Returns true if and only if the file or directory denoted by this abstract pathname exists. @return true if and only if the file denoted by this abstract pathname exists, otherwise return false

File.canRead

function File.canRead() as Boolean

Tests whether the application can read the file denoted by this abstract pathname.
@return true if and only if the file specified by this abstract pathname exists and can be read by the application. Otherwise it returns false

File.canWrite

function File.canWrite() as Boolean

Tests whether the application can write the file denoted by this abstract pathname.
@return true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file.Otherwise it returns false

File.createNewFile

function File.createNewFile() as Boolean

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.
@return true if and only the file is created

File.delete

function File.delete() as Boolean

Deletes the file or directory denoted by this abstract pathname.
@return true if and only the file is delete

File.listFiles

sub File.listFiles(files() as File)

Returns an array of Files contained in the directory denoted by this abstract pathname.

File.setPathName

sub File.setPathName(ByVal pathname as String)

Sets the pathname of this File instance

File.getPath

function File.getPath() as String

Converts abstract pathname into a pathname string and returns it.

File.getSize

function File.getSize() as LongInt

Returns the size of a file (in bytes). The size may differ from the actual size on the file system due to compression, support for sparse files, or other reasons.

File Examples

Simple File Declaration

#include once "kiwi\kiwi.bi"
#include once "kiwi\io.bi"

' Declare a new file
Dim myFile as File = File("C:\Users\nsiat\Desktop\Test.txt")

' Check if file exists and print the result
print "File exists: " & myFile.exists()

Create a new File

#include once "kiwi\kiwi.bi"
#include once "kiwi\io.bi"

' Declare a new file
Dim myFile as File = File("C:\Users\nsiat\Desktop\Test.txt")

' Try creating the file
if myFile.createNewFile() = true then
	print "File " & myFile.getPath() & " has been created!"
else
	print "Unable to create file " & myFile.getPath() & "!"
end if

Delete a File

#include once "kiwi\kiwi.bi"
#include once "kiwi\io.bi"

' Declare a new file
Dim myFile as File = File("C:\Users\nsiat\Desktop\Test2.txt")

' Check if file exists and  try delete it
if myFile.exists() then
	if myFile.deleteFile() then
		print "File " & myFile.getPath() & " has been deleted!"
	else
		print "Unable to delete file " & myFile.getPath()
	end if
else
	print "File doesn't exist!"
end if

Check if Path leads to a File or a Directory

#include once "kiwi\kiwi.bi"
#include once "kiwi\io.bi"

' Declare a new file
Dim myFile as File = File("C:\Users\nsiat\Desktop\Test.txt")

' Check if path leads to a file
if myFile.isFile() then
	print "Path " & myFile.getPath() & " leads to a file"
end if

' Check if path leads to a directory
if myFile.isDirectory() then
	print "Path " & myFile.getPath() & " leads to a directory"
end if

List Files inside a directory

#include once "kiwi\kiwi.bi"
#include once "kiwi\io.bi"

' Declare a new file 
Dim myDirectory as File = File(Environ("userprofile"))

Dim filesInDirectory() as File 				' This array holds the files included in the directory
myDirectory.listFiles(filesInDirectory())	' List all files from the directory to filesInDirectory() array

print "Files count: " & ubound(filesInDirectory) + 1

for i as Integer = 0 to ubound(filesInDirectory)
	print "File " & i & " " & filesInDirectory(i).getPath()
next