Provides an interface for reading from and writing to files. More...
Import Statement: | import ArcGIS.AppFramework 1.0 |
Inherits: |
Properties
- atEnd : bool
- error : int
- errorString : string
- exists : bool
- isReadable : bool
- isWritable : bool
- openMode : int
- path : string
- position : int
- textEncoding : TextEncoding
- textMode : bool
- url : url
Methods
- close()
- bool flush()
- bool open()
- bool open(int openMode)
- object read(int length)
- object readAll()
- string readLine()
- bool reset()
- write(var data)
- writeLine(string text)
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
Name | Value |
---|---|
File.TextEncodingUnknown | -1 |
File.TextEncodingUtf8 | 0 |
File.TextEncodingLatin1 | 1 |
File.TextEncodingLocal8Bit | 2 |
File.TextEncodingUtf16 | 3 |
OpenMode enumeration
This enum describes the mode in which a file is opened. It's also returned by the openMode property.
Name | Value |
---|---|
File.NotOpen | 0 |
File.OpenModeReadOnly | 1 |
File.OpenModeWriteOnly | 2 |
File.OpenModeReadWrite | 3 |
File.OpenModeAppend | 4 |
File.OpenModeTruncate | 8 |
File.OpenModeText | 16 |
File.OpenModeUnbuffered | 32 |
Error enumeration
This enum describes the various types of error that can occur, and can be returned by the error property.
Name | Value |
---|---|
File.NoError | 0 |
File.ErrorRead | 1 |
File.ErrorWrite | 2 |
File.ErrorFatal | 3 |
File.ErrorResource | 4 |
File.ErrorOpen | 5 |
File.ErrorAbort | 6 |
File.ErrorTimeOut | 7 |
File.ErrorUnspecified | 8 |
File.ErrorRemove | 9 |
File.ErrorRename | 10 |
File.ErrorPosition | 11 |
File.ErrorResize | 12 |
File.ErrorPermissions | 13 |
File.ErrorCopy | 14 |
Property Documentation
Returns true if the end of the file has been reached. Otherwise, returns false.
Returns a human-readable description of the last error that occurred.
Returns true if data can be read from the file. Otherwise, returns false.
Returns true if data can be written to the file. Otherwise, returns false.
Returns true if the open file is declared to be a text file. Otherwise, returns false.
Method Documentation
Closes the file, and sets openMode property to NotOpen.
Flushes any buffered data to the file. Returns true if successful, otherwise, returns false.
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.
Reads the amount of characters defined by the command's length.
The length parameter
Maximum amount of characters from the file to read.
Reads all remaining data from the file, returning it as a JavaScript ArrayBuffer byte array.
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.
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.