B4J Programming Press on the image to return to the main documentation page.

Files (Core)

List of types:

File
InputStream
OutputStream
TextReader
TextWriter

File

File is a predefined object that holds methods for working with files.

Events:

None

Members:


  Combine (Dir As String, FileName As String) As String

  Copy (DirSource As String, FileSource As String, DirTarget As String, FileTarget As String)

  Copy2 (In As java.io.InputStream, Out As java.io.OutputStream)

  Copy2Async (In As java.io.InputStream, Out As java.io.OutputStream) As Object

  CopyAsync (DirSource As String, FileSource As String, DirTarget As String, FileTarget As String) As Object

  Delete (Dir As String, FileName As String) As Boolean

  DirApp As String [read only]

  DirAssets As String [read only]

  DirData (AppName As String) As String

  DirTemp As String [read only]

  Exists (Dir As String, FileName As String) As Boolean

  GetFileParent (FileName As String) As String

  GetName (FilePath As String) As String

  GetUri (Dir As String, FileName As String) As String

  IsDirectory (Dir As String, FileName As String) As Boolean

  LastModified (Dir As String, FileName As String) As Long

  ListFiles (Dir As String) As List

  ListFilesAsync (Dir As String) As Object

  MakeDir (Parent As String, Dir As String)

  OpenInput (Dir As String, FileName As String) As InputStream

  OpenOutput (Dir As String, FileName As String, Append As Boolean) As OutputStream

  ReadBytes (Dir As String, FileName As String) As Byte()

  ReadList (Dir As String, FileName As String) As List

  ReadMap (Dir As String, FileName As String) As Map

  ReadMap2 (Dir As String, FileName As String, Map As Map) As Map

  ReadString (Dir As String, FileName As String) As String

  Size (Dir As String, FileName As String) As Long

  WriteBytes (Dir As String, FileName As String, Data() As Byte)

  WriteList (Dir As String, FileName As String, List As List)

  WriteMap (Dir As String, FileName As String, Map As Map)

  WriteString (Dir As String, FileName As String, Text As String)

Members description:

Combine (Dir As String, FileName As String) As String
Returns the full path to the given file.
This methods does not support files in the assets folder.
Copy (DirSource As String, FileSource As String, DirTarget As String, FileTarget As String)
Copies the specified source file to the target path.
Note that it is not possible to copy files to the Assets folder.
Copy2 (In As java.io.InputStream, Out As java.io.OutputStream)
Copies all the available data from the input stream into the output stream.
The input stream is automatically closed at the end.
Copy2Async (In As java.io.InputStream, Out As java.io.OutputStream) As Object
Asynchronously copies all the available data from the input stream into the output stream.
The input stream is automatically closed at the end.
Returns an object that should be used as the sender filter.
Example:
Wait For (File.Copy2Async(in, out)) Complete (Success As Boolean)
Log("Success: " & Success)
CopyAsync (DirSource As String, FileSource As String, DirTarget As String, FileTarget As String) As Object
Asynchronously copies the source file to the target path.
Note that it is not possible to copy files to the Assets folder.
Returns an object that should be used as the sender filter.
Example:
Wait For (File.CopyAsync(File.DirAssets, "1.txt", File.DirTemp, "1.txt")) Complete (Success As Boolean)
Log("Success: " & Success)
Delete (Dir As String, FileName As String) As Boolean
Deletes the specified file. If the file name is a directory then it must be empty in order to be deleted.
Returns true if the file was successfully deleted.
Files in the assets folder cannot be deleted.
DirApp As String [read only]
Returns the application folder.
DirAssets As String [read only]
Returns a reference to the files added to the Files tab. These files are read-only (in Release mode).
DirData (AppName As String) As String
Returns the path to a folder that is suitable for writing files.
On Windows, folders under Program Files are read-only. Therefore File.DirApp will be read-only as well.
This method returns the same path as File.DirApp on non-Windows computers.
On Windows it returns the path to the user data folder. For example:
C:\Users\[user name]\AppData\Roaming\[AppName]
DirTemp As String [read only]
Returns the temporary folder.
Exists (Dir As String, FileName As String) As Boolean
Returns true if the specified FileName exists in the specified Dir.
Note that the file system is case sensitive.
This method does not support File.DirAssets.

Example:
If File.Exists(File.DirApp, "MyFile.txt") Then ...
GetFileParent (FileName As String) As String
Returns the path of the file or folder parent.
GetName (FilePath As String) As String
Returns the file name from the full path (or the directory name in case of a directory).
GetUri (Dir As String, FileName As String) As String
Returns a Uri string ("file://...") that points to the given file.
IsDirectory (Dir As String, FileName As String) As Boolean
Tests whether the specified file is a directory.
LastModified (Dir As String, FileName As String) As Long
Returns the last modified date of the specified file.
This method does not support files in the assets folder.
Example:
Dim d As Long
d = File.LastModified(File.DirApp, "1.txt")
Msgbox(DateTime.Date(d), "Last modified")
ListFiles (Dir As String) As List
Returns a read only list with all the files and directories which are stored in the specified path.
An uninitialized list will be returned if the folder is not accessible.
This method does not support the assets folder.
ListFilesAsync (Dir As String) As Object
Asynchronous version of ListFiles. Should be used with Wait For.
Example:
Wait For (File.ListFilesAsync(Dir)) Complete (Success As Boolean, Files As List)
MakeDir (Parent As String, Dir As String)
Creates the given folder (creates all folders as needed).
Example:
File.MakeDir(File.DirApp, "music/90")
OpenInput (Dir As String, FileName As String) As InputStream
Opens the specified file name which is located in the Dir folder for reading.
Note that the file names are case sensitive.
OpenOutput (Dir As String, FileName As String, Append As Boolean) As OutputStream
Opens (or creates) the specified file which is located in the Dir folder for writing.
If Append is true then the new data will be written at the end of the existing file.
This method does not support files in the assets folder.
ReadBytes (Dir As String, FileName As String) As Byte()
Reads the data from the given file.
ReadList (Dir As String, FileName As String) As List
Reads the entire file and returns a List with all lines (as strings).
Example:
Dim List1 As List
List1 = File.ReadList(File.DirApp, "1.txt")
For i = 0 to List1.Size - 1
  Log(List1.Get(i))
Next
ReadMap (Dir As String, FileName As String) As Map
Reads the file and parses each line as a key-value pair (of strings).
See this link for more information about the actual format:
Properties format.
You can use File.WriteMap to write a map to a file.
Note that the order of items in the map may not be the same as the order in the file.
ReadMap2 (Dir As String, FileName As String, Map As Map) As Map
Similar to ReadMap. ReadMap2 adds the items to the given Map.
By using ReadMap2 with a populated map you can force the items order as needed.
Example:
Dim m As Map
m.Initialize
m.Put("Item #1", "")
m.Put("Item #2", "")
m = File.ReadMap2(File.DirApp, "settings.txt", m)
ReadString (Dir As String, FileName As String) As String
Reads the file and returns its content as a string.
Example:
Dim text As String
text = File.ReadString(File.DirApp, "1.txt")
Size (Dir As String, FileName As String) As Long
Returns the size in bytes of the specified file.
This method does not support files in the assets folder.
WriteBytes (Dir As String, FileName As String, Data() As Byte)
Writes the data to the given file.
WriteList (Dir As String, FileName As String, List As List)
Writes each item in the list as a single line.
Note that a value containing CRLF will be saved as two lines (which will return two item when read with ReadList).
All values will be converted to strings.
Example:
File.WriteList (File.DirApp, "mylist.txt", List1)
WriteMap (Dir As String, FileName As String, Map As Map)
Creates a new file and writes the given map. Each key value pair is written as a single line.
All values are converted to strings.
See this link for more information about the actual format:
Properties format.
You can use File.ReadMap to read this file.
WriteString (Dir As String, FileName As String, Text As String)
Writes the given text to a new file.
Example:
File.WriteString(File.DirApp, "1.txt", "Some text")

InputStream

A stream that you can read from. Usually you will pass the stream to a "higher level" object like TextReader that will handle the reading.
You can use File.OpenInput to get a file input stream.

Events:

None

Members:


  BytesAvailable As Int

  Close

  InitializeFromBytesArray (Buffer() As Byte, StartOffset As Int, MaxCount As Int)

  IsInitialized As Boolean

  ReadBytes (Buffer() As Byte, StartOffset As Int, MaxCount As Int) As Int

Members description:

BytesAvailable As Int
Returns an estimation of the number of bytes available without blocking.
Close
Closes the stream.
InitializeFromBytesArray (Buffer() As Byte, StartOffset As Int, MaxCount As Int)
Use File.OpenInput to get a file input stream.
This method should be used to read data from a bytes array.
Initializes the input stream and sets it to read from the specified bytes array.
StartOffset - The first byte that will be read.
MaxCount - Maximum number of bytes to read.
IsInitialized As Boolean
ReadBytes (Buffer() As Byte, StartOffset As Int, MaxCount As Int) As Int
Reads up to MaxCount bytes from the stream and writes it to the given Buffer.
The first byte will be written at StartOffset.
Returns the number of bytes actually read.
Returns -1 if there are no more bytes to read.
Otherwise returns at least one byte.
Example:
Dim buffer(1024) As byte
count = InputStream1.ReadBytes(buffer, 0, buffer.length)

OutputStream

A stream that you can write to. Usually you will pass the stream to a "higher level" object like TextWriter which will handle the writing.
Use File.OpenOutput to get a file output stream.

Events:

None

Members:


  Close

  Flush

  InitializeToBytesArray (StartSize As Int)

  IsInitialized As Boolean

  ToBytesArray As Byte()

  WriteBytes (Buffer() As Byte, StartOffset As Int, Length As Int)

Members description:

Close
Closes the stream.
Flush
Flushes any buffered data.
InitializeToBytesArray (StartSize As Int)
Use File.OpenOutput to get a file output stream.
This method should be used to write data to a bytes array.
StartSize - The starting size of the internal bytes array. The size will increase if needed.
IsInitialized As Boolean
ToBytesArray As Byte()
Returns a copy of the internal bytes array. Can only be used when the output stream was initialized with InitializeToBytesArray.
WriteBytes (Buffer() As Byte, StartOffset As Int, Length As Int)
Writes the buffer to the stream. The first byte to be written is Buffer(StartOffset),
and the last is Buffer(StartOffset + Length - 1).

TextReader

Reads text from the underlying stream.

Events:

None

Members:


  Close

  Initialize (InputStream As java.io.InputStream)

  Initialize2 (InputStream As java.io.InputStream, Encoding As String)

  IsInitialized As Boolean

  Read (Buffer() As Char, StartOffset As Int, Length As Int) As Int

  ReadAll As String

  ReadLine As String

  ReadList As List

  Ready As Boolean

  Skip (NumberOfCharaceters As Int) As Int

Members description:

Close
Closes the stream.
Initialize (InputStream As java.io.InputStream)
Initializes this object by wrapping the given InputStream using the UTF8 encoding.
Initialize2 (InputStream As java.io.InputStream, Encoding As String)
Initializes this object by wrapping the given InputStream using the specified encoding.
IsInitialized As Boolean
Read (Buffer() As Char, StartOffset As Int, Length As Int) As Int
Reads characters from the stream and into the Buffer.
Reads up to Length characters and puts them in the Buffer starting as StartOffset.
Returns the actual number of characters read from the stream.
Returns -1 if there are no more characters available.
ReadAll As String
Reads all of the remaining text and closes the stream.
ReadLine As String
Reads the next line from the stream. The new line characters are not returned.
Returns Null if there are no more characters to read.

Example:
  Dim Reader As TextReader
  Reader.Initialize(File.OpenInput(File.InternalDir, "1.txt"))
  Dim line As String
  line = Reader.ReadLine
  Do While line <> Null
    Log(line)
    line = Reader.ReadLine
  Loop
  Reader.Close
ReadList As List
Reads the remaining text and returns a List object filled with the lines.
Closes the stream when done.
Ready As Boolean
Tests whether there is at least one character ready for reading without blocking.
Skip (NumberOfCharaceters As Int) As Int
Skips the specified number of characters.
Returns the actual number of characters that were skipped (which may be less than the specified value).

TextWriter

Writes text to the underlying stream.


Example:
Dim Writer As TextWriter
Writer.Initialize(File.OpenOutput(File.DirDefaultExternal, "1.txt", False))
Writer.WriteLine("This is the first line.")
Writer.WriteLine("This is the second line.")
Writer.Close

Events:

None

Members:


  Close

  Flush

  Initialize (OutputStream As java.io.OutputStream)

  Initialize2 (OutputStream As java.io.OutputStream, Encoding As String)

  IsInitialized As Boolean

  Write (Text As String)

  WriteLine (Text As String)

  WriteList (List As List)

Members description:

Close
Closes the stream.
Flush
Flushes any buffered data.
Initialize (OutputStream As java.io.OutputStream)
Initializes this object by wrapping the given OutputStream using the UTF8 encoding.
Initialize2 (OutputStream As java.io.OutputStream, Encoding As String)
Initializes this object by wrapping the given OutputStream using the specified encoding.
IsInitialized As Boolean
Write (Text As String)
Writes the given Text to the stream.
WriteLine (Text As String)
Writes the given Text to the stream followed by a new line character.
Example:
  Dim Writer As TextWriter
  Writer.Initialize(File.OpenOutput(File.DirDefaultExternal, "1.txt", False))
  Writer.WriteLine("This is the first line.")
  Writer.WriteLine("This is the second line.")
  Writer.Close
WriteList (List As List)
Writes each item in the list as a single line.
Note that a value containing CRLF will be saved as two lines (which will return two item when read with ReadList).
All values will be converted to strings.
Top