Text File

Text file access tool. The object allows writing to text file and reading from it. 

The object work in one of the following modes:

Creating text file access tool

 To add text file object

  1. 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.
  2. Open the text file's Properties.
  3. Modify the Name of the element. This name will be used to access this element. 
  1. Select the object work mode: Read, Write, or Write/Append.
  2. Choose the Character set, that will be used to translate bytes to/from strings.
  3. 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 File Type group of buttons and browse for the file using the Browse button. The path to the file will be shown in the File field. The  button to the right allows to switch between the absolute and relative file path.
  4. Otherwise, if you want to read the file from some URL, choose URL from the File Type group of buttons and type the required URL in the URL edit box.
  5. In the case you will read the file, specify separators that are used in the text file to separate values. Select the corresponding check boxes in the Separators group of controls. If some custom separators are used, specify them in the Custom edit box below.
You work with text files using the corresponding API of the  TextFile object.

This object reads text file line by line. Class defines function getLineNumber() for getting the current line number. By default, line numbering begins at 0. This number increments at every line terminator as the data is read.

A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a line feed.

In the reading mode, on each reading function call (e.g. readDouble()), TextFile advances reading position to the next value that can be read. I.e. it reads requested data and skips all trailing separatorsForReading that were specified in the constructor.

Initially, TextFile is in 'not open' state: any further accessor-function call (e.g. print(double)) will open file, i.e. next reading (if in READ mode) will start reading file from its beginning, while next writing (if in WRITE mode) will start rewriting or appending (depends on mode)

TextFile has skipping functions skipChars(long) and skipTokens(int) which may be used to skip (preliminarily known) number of characters/tokens in the file

Writing to text file

This object has following methods for writing to the file:

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. If numeric value is printed, the String produced by String.valueOf(value) is translated into bytes according to the chosen character encoding, and these bytes are written to the file. If the argument is null then the string "null" is printed. 

void println(<type> value) - prints given value to the file and then terminates the line. This method behaves as though it invokes print(value) and then println().

void printf(java.util.Locale l, String format, Object... args)  - Convenient method to write a formatted string to the text file using the specified format string and arguments. The number of arguments is variable and may be zero. 
l - The locale to apply during formatting. If l is null then no localization is applied.
format - A format string as described in Formatter class specification.
args - Arguments referenced by the format specifiers in the format string. 

void printf(String format, Object... args)  - Convenient method to write a formatted string to the text file using the specified format string and arguments. The number of arguments is variable and may be zero.
format - A format string as described in Formatter class specification.
args - Arguments referenced by the format specifiers in the format string. 

void printf(String, Object...) and printf(Locale, String, Object...) - convenient method to write a formatted string to the text file using the specified format string and arguments.

Both print() and println() methods can take values of all used types: boolean, char, char[], double, float, int, long. Object, String.

Reading text file

This object has following methods for reading the file (all these methods are only available in READ mode):

boolean canReadMore() - Returns true if there is available content in the file at the reading position. This method opens 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 been opened yet (no read- and skip-functions haven't been called as well as canReadMore()).
-1 is returned if 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 reading position is in the middle of line (e.g. current line is "33;Car" and readInt() was called once), the rest part of line is returned (in the given example it is "Car" - if this TextFile has separator ';', and ";Car" otherwise).
The function returns a String containing the contents of the line (from current reading position), not including any line termination characters, or null if the end of the stream has been reached

String readString() - Reads and returns a String which contains text from current reading position (which is after previously read separator), inclusive, to the next separator character position, exclusive 

boolean readBoolean() - Reads boolean string "true" or "false" (with or without quotation marks). 

byte readByte() - Reads and returns number as byte

short readShort() - Reads and returns a short value.

char readChar() - Reads and returns one character. Throws exception if there is separator in the current reading position or current text before next separator has more than one character 

int readInt() - Reads and returns an int value. 

double readDouble() - Reads and returns a double value (number with floating-point and double-precision). 

long readLong() - Reads and returns a long value.

float readFloat() - Reads and returns a float value (number with floating-point). 

Auxiliary methods

long skipChars(long n) - Skip characters (as well as separator characters). The method 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 method returns the number of tokens actually skipped.
n - the number of tokens to skip.

void close() - Closes the read or write stream of this TextFile and releases any system resources associated with it. Returns this object to 'not open' state.

void setFile(String fileName, int mode) - Sets the new file to be used with this TextFile object. If specified file differs from previously used and the latter is not closed, it will be closed. For TextFile based on URL or on file among class resources, the only possible mode is READ.
    fileName -
    absolute path to the file, or
    path to file relative to current working directory, or
    name of file which is among resources of owner's class (if included into the class-path and lies in the owner's package) - only READ mode
    mode - possible values:
READ
WRITE
WRITE_APPEND
 
void setURL(java.net.URL url) - Sets the new url to be used with this TextFile object. This method switches TextFile to the READ mode. If specified url differs from previously used and the stream of latter is not closed, it will be closed.

String getLocation() - Returns the location (path) of the file. If the file location hasn't been set, returns null

void setMode(int mode) - Sets the new mode of this TextFile object. If specified mode differs from previously used and the file is not closed, it will be closed. For TextFile based on URL or on file among class resources, the only possible mode is READ.  
    mode - possible values:
READ
WRITE
WRITE_APPEND