Android 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.
The Text files tutorial covers most of the File methods and concepts.

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)

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

  DirAssets As String [read only]

  DirDefaultExternal As String [read only]

  DirInternal As String [read only]

  DirInternalCache As String [read only]

  DirRootExternal As String [read only]

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

  ExternalReadable As Boolean [read only]

  ExternalWritable As Boolean [read only]

  GetText (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

  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

  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

  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 file name.
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.
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.
DirAssets As String [read only]
Returns a reference to the files added to the Files tab. These files are read-only.
DirDefaultExternal As String [read only]
Returns the application default external folder which is based on the package name.
The folder is created if needed.
DirInternal As String [read only]
Returns the folder in the device internal storage that is used to save application private data.
DirInternalCache As String [read only]
Returns the folder in the device internal storage that is used to save application cache data.
This data will be deleted automatically when the device runs low on storage.
DirRootExternal As String [read only]
Returns the root folder of the external storage media.
Exists (Dir As String, FileName As String) As Boolean
Returns true if the specified FileName exists in the specified Dir.
Note that the Android file system is case sensitive.

Example:
If File.Exists(File.DirDefaultExternal, "MyFile.txt") Then ...
ExternalReadable As Boolean [read only]
Tests whether the external storage media can be read from.
ExternalWritable As Boolean [read only]
Tests whether the external storage media can be written to (and also read from).
GetText (Dir As String, FileName As String) As String
Reads the entire file and returns its text.
The file is assumed to be encoded with UTF8.
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.DirRootExternal, "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.
MakeDir (Parent As String, Dir As String)
Creates the given folder (creates all folders as needed).
Example:
File.MakeDir(File.DirInternal, "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 Android file system is 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.
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.DirDefaultExternal, "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.DirInternal, "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.DirRootExternal, "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.
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.DirInternal, "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.DirRootExternal, "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