ArcGIS for Developers

File QML Type

Provides an interface for reading from and writing to files. More...

Import Statement: import ArcGIS.AppFramework 1.0
Inherits:

AppFrameworkObject

Properties

Methods

Detailed Description

The File component provides tools to work with text files within an app. This includes returning what state the file has been opened in, reading data either in full or in part, and writing any form of data to the file.

Note that the File component only works directly with plaintext. This means that formatted text, or non-text content like images, will not read or display correctly, although they can still be written to the file.

The following code sample demonstrates usage of the File component, reading from and writing to a text file created by the FileFolder component. This functionality is not displayed within the app, but can be observed in the text file and through console logs.

Rectangle {
anchors.fill: parent

        Column {
                spacing : 10
                anchors.fill: parent

                Text {
                        id: sampleName
                        text: "File Component Sample"
                        font.pointSize: 24
                        color: "black"
                        wrapMode: Text.WrapAtWordBoundaryOrAnywhere
                }

                FileFolder {
                        id: fileFolder
                        path: AppFramework.userHomePath+"/Samples/File"

                        Component.onCompleted: {
                                fileFolder.makeFolder()
                                fileFolder.writeFile("FileSample.txt", "File component sample")
                        }
                }

                FileInfo {
                        id: fileInfo
                        filePath: fileFolder.path + "/FileSample.txt"
                }

                File {
                        id: file
                        path:fileInfo.filePath
                }

                Row {
                        spacing : 10

                        Button {
                                text: "read"
                                onClicked: {
                                        file.open(File.OpenModeReadWrite)
                                        console.log(file.readAll())
                                        file.close()
                                }
                        }

                        Button {
                                text: "write"
                                onClicked: {
                                        file.open(File.OpenModeReadWrite)
                                        file.writeLine("AppStudio is awesome")
                                        file.close()
                                }
                        }
                }
        }
}

Enumerations

TextEncoding enumeration

NameValue
File.TextEncodingUnknown-1
File.TextEncodingUtf80
File.TextEncodingLatin11
File.TextEncodingLocal8Bit2
File.TextEncodingUtf163

OpenMode enumeration

This enum describes the mode in which a file is opened. It's also returned by the openMode property.

NameValue
File.NotOpen0
File.OpenModeReadOnly1
File.OpenModeWriteOnly2
File.OpenModeReadWrite3
File.OpenModeAppend4
File.OpenModeTruncate8
File.OpenModeText16
File.OpenModeUnbuffered32

Error enumeration

This enum describes the various types of error that can occur, and can be returned by the error property.

NameValue
File.NoError0
File.ErrorRead1
File.ErrorWrite2
File.ErrorFatal3
File.ErrorResource4
File.ErrorOpen5
File.ErrorAbort6
File.ErrorTimeOut7
File.ErrorUnspecified8
File.ErrorRemove9
File.ErrorRename10
File.ErrorPosition11
File.ErrorResize12
File.ErrorPermissions13
File.ErrorCopy14

Property Documentation

[read-only] atEnd : bool

Returns true if the end of the file has been reached. Otherwise, returns false.


[read-only] error : int

Returns the file error status.


[read-only] errorString : string

Returns a human-readable description of the last error that occurred.


[read-only] exists : bool

Returns true if the specified file exists. Otherwise, returns false.


[read-only] isReadable : bool

Returns true if data can be read from the file. Otherwise, returns false.


[read-only] isWritable : bool

Returns true if data can be written to the file. Otherwise, returns false.


[read-only] openMode : int

Returns the state of the OpenMode enum dictating how the file is opened.


path : string

Returns the path to the file.


position : int

Returns the position data is to be written to or read from.


textEncoding : TextEncoding

Returns the type of encoding the given text is in.


textMode : bool

Returns true if the open file is declared to be a text file. Otherwise, returns false.


url : url

Returns the URL that links to the file.


Method Documentation

close()

Closes the file, and sets openMode property to NotOpen.


bool flush()

Flushes any buffered data to the file. Returns true if successful, otherwise, returns false.


bool open()

Opens the given file, returning true if successful. Otherwise, returns false.


bool open(openMode)

Opens the given file in the defined open mode, returning true if successful. Otherwise, returns false.

The openMode parameter

The mode the file will be opened in, e.g. read-only or write-only.


object read(length)

Reads the amount of characters defined by the command's length.

The length parameter

Maximum amount of characters from the file to read.


object readAll()

Reads all remaining data from the file, returning it as a JavaScript ArrayBuffer byte array.


string readLine()

Reads and returns a line of ASCII characters from the file.


bool reset()

Attempts to reset the file. Returns true if successful, otherwise returns false.


write(data)

Writes the given data into the file at the current position. After the content is written, the position property will be updated.

The data parameter

Data to write into the file. If this data is a string, this data will be encoded into binary using the format set by the textEncoding property. If this data is already binary, it will be directly written directly to the file as-is.


writeLine(text)

Writes the given string into the file at the current position. After the content is written to the file, the position property will be updated.

A UNIX line feed (\n) will be used to terminate the line. If you intend to use a different line ending, such as a Windows carriage return and line feed (\r\n), you will need to use the write method.

The text parameter

The text you want to write into the file. This line will be encoded into binary using the format set by the textEncoding property.