The Text File element is a tool for accessing text files. It allows you to write to and read from a text file.
The element has the following modes:
- Reading mode Demo model: Reading Agent Parameters from a CSV File Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files). Demo model: Reading Table Function from a Text File Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
- Writing mode Demo model: Using Text File as a Log Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
- Writing/append mode
To add a text file element
- Drag the Text File element from the Connectivity palette onto the graphical diagram. You can also drag the text files from other applications directly onto the AnyLogic graphical diagram. In this case, AnyLogic automatically adds a new Text File element on the graphical diagram containing the added text file.
- Open the Properties of the newly added element.
- Modify the Name of the element. This name will be used to access this element.
- Select the work mode of the object: Read, Write, or Write/Append.
- Select the Character set that will be used to translate bytes to and from strings.
- Specify the text file this object will work with. If you want to modify the file located on the local computer, choose File from the Resource group of buttons and browse for the file using the Browse button. Once selected, the source file will automatically appear in the model’s Resource folder in the Projects view. This allows you to track the current state of the source file, switch between the absolute and relative file paths, and so on.
- If you want to read the file from an URL, select URL from the File Type group of buttons and enter the required URL in the URL edit box.
-
If you are reading the file, specify the separators that are used in the text file to separate values, by selecting the appropriate check boxes in the Separators group of controls.
If some custom separators are used, specify them in the Custom edit box below.
To work with text files, use the API of the TextFile object.
The Text file element reads text files line by line. The class defines a getLineNumber() function to get the current line number. By default, line numbering starts at 0. This number is incremented at each line terminator as the data is read.
A line is considered to be terminated by either a line feed ('n'), a carriage return ('r'), or a carriage return followed immediately by a line feed.
In the Read mode, on each read function call (for example, readDouble()), the Text file element advances the read position to the next value that can be read, that is, it reads requested data and skips all trailing delimiters specified in the constructor.
Initially, the Text file element is in the “not open” state: any further accessor function call (for example, print(double)) will open the file. That is, the next read (if in the Read mode) will start reading from the beginning of the file, while the next write (if in the Write mode) will start rewriting or appending (depending on the mode).
The text file element has skipping functions: skipChars(long) and skipTokens(int). Use them to skip a known number of characters or tokens in the file.
- General
-
Name — The name of the element. It is used to identify and access it from the code.
Show name — If selected, the name of the element is displayed in the presentation diagram.
Ignore — If selected, the element is excluded from the model.
Resource — The source of the text file, either File or URL.
File [Visible if Resource: File] — The file from which this Text file element originates. Click in the field to select a file from those already attached to the model, or click the Browse icon to the right to upload a new file from your computer.
URL [Visible if Resource: URL] — The URL pointing to the file from which this Text file element originates.
Mode — The mode of interaction with the original text file:
Read — Read-only mode. This is the only mode available for files that originate from URLs and are inside Java packages.
Write — Allows you to write to the files while overwriting them.
Write/Append — Allows you to append information to existing content within files.Character set — The character set (encoding) you want to use when interacting with the file.
Separators [Visible if Mode: Read] — The line separators (delimiters) used within the source file. You can select more than one. For details, see above.
- Expert
-
Visible — The visibility of the element. When selected, the element is visible during model runtime.
- Writing to the text file
-
This object has the following functions for writing to the text file:
Function Description void println() Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('n'). void print(<Type> value) Prints given value to the file. When printing a numeric value, the String produced by String.valueOf(value) is translated into bytes according to the selected character encoding, and those bytes are written to the file. If the argument is null then the string null is printed.
value — The value to print.void println(<Type> value) Prints the given value to the file and then terminates the line. This function behaves as if it called print(value) and then println().
value — The value to print.void printf(java.util.Locale l, String format, Object... args) A convenient function that writes a formatted string to the text file using the specified format string and arguments.
The number of arguments is variable and can be zero.
l — The locale to apply during formatting. If l is null, no locale is applied.
format — A format string as described in the Formatter class specification.
args — Arguments referenced by the format specifiers in the format string.void printf(String format, Object... args) A convenient function that writes write a formatted string to the text file using the specified format string and arguments.
The number of arguments is variable and can be zero.
format — A format string as described in the Formatter class specification.
args — Arguments referenced by the format specifiers in the format string.Both print() and println() functions can take values of all used types: boolean, char, char[], double, double[], float, int, int[], long. Object, String, String[]. They also support printing two-dimensional arrays.
- Reading the text file
-
This object has following functions for reading the file (all these functions are only available in Read mode):
Function Description boolean canReadMore() Returns true if there is available content in the file at the read position. This function opens the file for reading if it is not open. int getLineNumber() The function returns the current line number (1-based).
0 is returned if the file has not yet been opened (no read- and skip-functions have been called, as well as canReadMore()).
-1 is returned when the end of the file is reached.String readLine() Reads a line of text.
Whenever a line terminator is read, the current line number is incremented. If the current read position is in the middle of the line (for example, the current line is 33;Car and readInt() has been called once), the remaining part of the line is returned (in the given example, it is Car, if this text file has ; as the separator; ;Car otherwise).
The function returns a String containing the contents of the line (from the current read position), not including any line terminators, or null if the end of the stream has been reached.String readString() Reads and returns a String containing text from the current read position (which is after the previously read separator), inclusive, to the next separator position, exclusive. boolean readBoolean() Reads the Boolean string: "true" or "false" (with or without quotes). byte readByte() Reads and returns the number as byte. short readShort() Reads and returns a short value. char readChar() Reads and returns a single character. Throws an exception if there is a delimiter at the current read position, or if the current text before the next delimiter has more than one character. int readInt() Reads and returns an int value. double readDouble() Reads and returns a double value (floating point number with double precision). long readLong() Reads and returns a long value. float readFloat() Reads and returns a float value. - Auxiliary functions
-
Function Description long skipChars(long n) Skips characters (as well as separators). The function returns the number of characters actually skipped.
n — the number of characters to skip.int skipTokens(int n) Skips tokens (text between separators declared in the constructor). The function returns the number of tokens actually skipped.
n — the number of characters to skip.void close() Closes the read or write stream of this text file and releases any system resources associated with it. Returns this object to the “not open” state. void setFile(String fileName, int mode) Sets the new file to use with this Text file element. If the specified file is different from the previously used file and the latter is not closed, it will be closed. For a text file based on the URL or on the file among class resources, the only possible mode is Read.
fileName — the absolute path to the file, or
the path to the file relative to the current working directory, or
the name of the file which is among resources of the owner’s class (if included in the class-path and located in the owner’s package) — Read (TextFile.READ) mode only.
Possible values of mode:
TextFile.READ
TextFile.WRITE
TextFile.WRITE_APPENDvoid setURL(java.net.URL url) Sets the new URL to use with this Text file element. This function switches the text file to the Read mode. If the specified URL is different from the previously used URL and the stream of the latter is not closed, it will be closed. String getLocation() Returns the location (path) of the file. If the file location has not been set, returns null. void setMode(int mode) Sets the new mode of this Text file element. If the specified mode is different from the previously used mode and the file is not closed, it will be closed. For a text file based on the URL or on the file among class resources, the only possible mode is Read.
Possible values of mode:
TextFile.READ
TextFile.WRITE
TextFile.WRITE_APPEND
-
How can we improve this article?
-