- Nested Class Summary
- Field Summary
- Constructor Summary
- Method Summary
- Field Details
- Constructor Details
- Method Details
- restoreOwner
- close
- setFile
- getLocation
- setURL
- setMode
- println
- println
- println
- println
- println
- println
- println
- println
- println
- println
- printf
- printf
- getLineNumber
- canReadMore
- readLine
- readBoolean
- readByte
- readShort
- readChar
- readInt
- readLong
- readFloat
- readDouble
- readString
- skipChars
- skipTokens
- toString
- java.lang.Object
- com.anylogic.engine.connectivity.TextFile
- All Implemented Interfaces:
com.anylogic.engine.internal.Child
,Serializable
public class TextFile extends Object implements Serializable, com.anylogic.engine.internal.Child
Write mode description:
This object has following methods for writing to the file:
print(value)
- prints given value to the file:print(double)
,print(String)
, etc.
- prints line-separator to the fileprintln()
println(value)
- prints given value, followed by line-separator, to the file:println(double)
,println(String)
, etc.printf(String, Object...)
andprintf(Locale, String, Object...)
- convenience methods to write a formatted string to the text file using the specified format string and arguments
Read mode description:
This object reads text file line by line. Class defines method
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 method 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.
Common information:
Initially, TextFile is in 'not open' state: any
further accessor-method call (e.g. print(double)
) will open file,
i.e.
- next reading (if in
READ
mode) will start reading file from its beginning - next writing (if in
WRITE
mode) will start rewriting or appending (depends on mode)
TextFile has skipping methods skipChars(long)
and
skipTokens(int)
which may be used to skip (preliminarily known)
number of characters/tokens in the file
- Author:
- AnyLogic North America, LLC https://anylogic.com
- See Also:
- Serialized Form
Modifier and Type | Class | Description |
---|---|---|
static enum | TextFile.Mode |
File operations mode constants
|
Modifier and Type | Field | Description |
---|---|---|
static final TextFile.Mode | READ |
'read' mode
|
static final TextFile.Mode | WRITE |
'write' mode
|
static final TextFile.Mode | WRITE_APPEND |
'write/append' mode
|
Constructor | Description |
---|---|
TextFile |
Creates new TextFile object based on given name of file
|
TextFile |
Creates new TextFile object in
READ mode, based on given URL |
Modifier and Type | Method | Description |
---|---|---|
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. This method is only available in READ mode |
void | close() |
Closes the read or write stream of this TextFile and releases any system
resources associated with it.
|
int | getLineNumber() |
Get the current line number.
This method is only available in READ mode |
String | getLocation() |
Returns the location (path) of the file.
Returns null if the file location hasn't been set. |
void | print |
Prints a boolean value.
|
void | print |
Prints a character.
|
void | print |
Prints an array of characters.
|
void | print |
Prints a double-precision floating-point number.
|
void | print |
Prints a floating-point number.
|
void | print |
Prints an integer.
|
void | print |
Prints a long integer.
|
void | print |
Prints an object.
|
void | print |
Prints a string.
|
void | printf |
A convenient method to write a formatted string to the text file using
the specified format string and arguments.
|
void | printf |
A convenient method to write a formatted string to the text file using
the specified format string and arguments.
|
void | println() |
Terminates the current line by writing the line separator string.
|
void | println |
Prints a boolean value and then terminates the line.
|
void | println |
Prints a character and then terminates the line.
|
void | println |
Prints an array of characters and then terminates the line.
|
void | println |
Prints a double-precision floating-point number and then terminates the
line.
|
void | println |
Prints a floating-point number and then terminates the line.
|
void | println |
Prints an integer and then terminates the line.
|
void | println |
Prints a long integer and then terminates the line.
|
void | println |
Prints an Object and then terminates the line.
|
void | println |
Prints a String and then terminates the line.
|
boolean | readBoolean() |
Reads boolean string
"true" or "false"
(with or without quotation marks) |
byte | readByte() |
Reads and returns number as byte.
|
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 |
double | readDouble() |
Reads and returns a
double value (number with
floating-point and double-precision). |
float | readFloat() |
Reads and returns a
float value (number with
floating-point). |
int | readInt() | |
String | readLine() |
Read a line of text.
|
long | readLong() | |
short | readShort() | |
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 |
void | restoreOwner |
Deprecated.
|
void | setFile |
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 |
void | setMode |
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 |
void | setURL |
Sets the new url to be used with this TextFile object
This method switches TextFile to the READ modeIf specified url differs from previously used and the stream of latter is not closed, it will be closed |
long | skipChars |
Skip characters (as well as separator characters).
|
int | skipTokens |
Skips tokens (texts between separators declared in the constructor)
|
String | toString() |
public static final TextFile.Mode READ
public static final TextFile.Mode WRITE
public static final TextFile.Mode WRITE_APPEND
public TextFile(Presentable owner, String packagePrefix, TextFile.Mode mode, String fileName, String charsetName, char[] separatorsForReading)
- Parameters:
owner
- this object's owner (agent or experiment)packagePrefix
- the package name of original agent where this image is defined, formatted using '/' characters; with '/' character at the beginning and at the endmode
- possible values:fileName
-- absolute path to the file
- path to file relative to current working directory
- name of file which is among resources of
owner
's class (if included into the class-path and lies in theowner
's package) - onlyREAD
mode
charsetName
- The name of a supported charset
Available charset names can be obtained callingCharset.availableCharsets()
.keySet()
usenull
for the default system charsetseparatorsForReading
- an array (may benull
) of characters which should be assumed as token-separators. To define line-separator, use '\n' character. It is also important whether this array includes line-separator ('\n') or not. If doesn't then e.g.readString()
may return string with multiple linesFor example, read tab-separated file can be processed using
separatorsForReading
=new char[]{ '\n', '\t' }
public TextFile(Presentable owner, String packagePrefix, URL url, String charsetName, char[] separatorsForReading)
READ
mode, based on given URL- Parameters:
owner
- this object's owner (agent or experiment)packagePrefix
- the package name of original agent where this image is defined, formatted using '/' characters; with '/' character at the beginning and at the endurl
- the url to contents which should be readcharsetName
- The name of a supported charset
Available charset names can be obtained callingCharset.availableCharsets()
.keySet()
usenull
for the default system charsetseparatorsForReading
- an array (may benull
) of characters which should be assumed as token-separators. To define line-separator, use '\n' character. It is also important whether this array includes line-separator ('\n') or not. If doesn't then e.g.readString()
may return string with multiple linesFor example, read tab-separated file can be processed using
separatorsForReading
=new char[]{ '\n', '\t' }
@AnyLogicInternalCodegenAPI @Deprecated public void restoreOwner(Object owner)
This method restores owner of this object
The method is used in snapshot saving/loading
- Specified by:
restoreOwner
in interfacecom.anylogic.engine.internal.Child
- Parameters:
owner
- owner of this object, usuallyAgent
,Experiment
orShapeGroup
public void close()
Closing a previously closed TextFile has no effect.
public void setFile(String fileName, TextFile.Mode mode)
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
- Parameters:
fileName
-- absolute path to the file
- path to file relative to current working directory
- name of file which is among resources of
owner
's class (if included into the class-path and lies in theowner
's package) - onlyREAD
mode
mode
- possible values:
public String getLocation()
Returns
null
if the file location hasn't been set.- Returns:
- the location of the file.
- See Also:
public void setURL(URL url)
This method switches TextFile to the
READ
modeIf specified url differs from previously used and the stream of latter is not closed, it will be closed
- Parameters:
url
- the url to contents which should be read
public void setMode(TextFile.Mode mode)
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
- Parameters:
mode
- possible values:
public void print(boolean b)
String.valueOf(boolean)
is translated into bytes according to the chosen character encoding, and
these bytes are written to the file.- Parameters:
b
- Theboolean
to be printed
public void print(char c)
- Parameters:
c
- Thechar
to be printed
public void print(char[] s)
- Parameters:
s
- The array of chars to be printed- Throws:
NullPointerException
- Ifs
isnull
public void print(double d)
String.valueOf(double)
is translated
into bytes according to the chosen character encoding, and these bytes
are written to the file.- Parameters:
d
- Thedouble
to be printed- See Also:
-
Double.toString(double)
public void print(float f)
String.valueOf(float)
is translated into bytes according to the chosen character encoding, and
these bytes are written to the file.- Parameters:
f
- Thefloat
to be printed- See Also:
-
Float.toString(float)
public void print(int i)
String.valueOf(int)
is translated into bytes according to the chosen character encoding, and
these bytes are written to the file.- Parameters:
i
- Theint
to be printed- See Also:
-
Integer.toString(int)
public void print(long l)
String.valueOf(long)
is translated into bytes according to the chosen character encoding, and
these bytes are written to the file.- Parameters:
l
- Thelong
to be printed- See Also:
-
Long.toString(long)
public void print(Object obj)
String.valueOf(Object)
method is translated into bytes according to the chosen character
encoding, and these bytes are written to the file.- Parameters:
obj
- TheObject
to be printed- See Also:
-
Object.toString()
public void print(String s)
null
then the string
"null"
is printed. Otherwise, the string's characters are
converted into bytes according to the chosen character encoding, and
these bytes are written to the file.- Parameters:
s
- TheString
to be printed
public void println()
line.separator
, and is not necessarily a single newline
character ('\n'
).public void println(boolean x)
print(boolean)
and then
println()
.- Parameters:
x
- theboolean
value to be printed
public void println(char x)
print(char)
and then
println()
.- Parameters:
x
- thechar
value to be printed
public void println(char[] x)
print(char[])
and
then println()
.- Parameters:
x
- the array ofchar
values to be printed
public void println(double x)
print(double)
and then println()
.- Parameters:
x
- thedouble
value to be printed
public void println(float x)
print(float)
and
then println()
.- Parameters:
x
- thefloat
value to be printed
public void println(int x)
print(int)
and then
println()
.- Parameters:
x
- theint
value to be printed
public void println(long x)
print(long)
and then
println()
.- Parameters:
x
- thelong
value to be printed
public void println(Object x)
print(String)
and then
println()
.- Parameters:
x
- TheObject
to be printed.
public void println(String x)
print(String)
and then
println()
.- Parameters:
x
- theString
value to be printed
public void printf(Locale l, String format, Object... args)
- Parameters:
l
- The locale to apply during formatting. Ifl
isnull
then no localization is applied.format
- A format string as described inFormatter
class specification.args
- Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behavior on anull
argument depends on the conversion ofFormatter
.- Throws:
IllegalFormatException
- If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of theFormatter
class specification.NullPointerException
- If theformat
isnull
public void printf(String format, Object... args)
- Parameters:
format
- A format string as described inFormatter
class specification.args
- Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behavior on anull
argument depends on the conversion ofFormatter
.- Throws:
IllegalFormatException
- If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of theFormatter
class specification.NullPointerException
- If theformat
isnull
public int getLineNumber()
This method is only available in
READ
mode- Returns:
- the current line number (1-based)
0
if the file hasn't been opened yet (no read- and skip-methods haven't been called as well ascanReadMore()
)-1
if the end of the file has been reached
public boolean canReadMore()
true
if there is available content in the file at
the reading position.This method opens file for reading if it is not open.
This method is only available in
READ
mode- Returns:
true
if there is available content in the file at the reading position,false
if the end of file has been reached
public String readLine()
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 ';')- 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
public boolean readBoolean()
"true"
or "false"
(with or without quotation marks)- Returns:
- the
boolean
value read.
public byte readByte()
Byte.MIN_VALUE
... Byte.MAX_VALUE
, inclusive.- Returns:
- the
byte
number value read.
public short readShort()
short
value.Short value is a number in the range
Short.MIN_VALUE
...
Short.MAX_VALUE
, inclusive- Returns:
- the
short
value read.
public char readChar()
Throws exception if there is separator in the current reading position or current text before next separator has more than one character
- Returns:
- the
char
value of character read.
public int readInt()
int
value.Int value is a number in the range
Integer.MIN_VALUE
...
Integer.MAX_VALUE
, inclusive- Returns:
- the
int
value read.
public long readLong()
long
value.Long value is a number in the range
Long.MIN_VALUE
...
Long.MAX_VALUE
, inclusive- Returns:
- the
long
value read.
public float readFloat()
float
value (number with
floating-point).- Returns:
- the
float
value read.
public double readDouble()
double
value (number with
floating-point and double-precision).- Returns:
- the
double
value read.
public String readString()
String
which contains text from
current reading position (which is after previously read separator),
inclusive, to the next separator character position, exclusive- Returns:
- the
double
value read.
public long skipChars(long n)
- Parameters:
n
- the number of characters to skip- Returns:
- the number of characters actually skipped
- Throws:
IllegalArgumentException
- Ifn
is negative
public int skipTokens(int n)
- Parameters:
n
- the number of tokens to skip- Returns:
- the number of tokens actually skipped
- Throws:
IllegalArgumentException
- Ifn
is negative
public String toString()