public class TextFile
extends java.lang.Object
implements java.io.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.
println()
- prints line-separator to the file
println(value)
- prints given value, followed by
line-separator, to the file: println(double)
,
println(String)
, etc.
printf(String, Object...)
and
printf(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.
READ
mode) will start reading file from its
beginning
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
Modifier and Type | Class and Description |
---|---|
static class |
TextFile.Mode
File operations mode constants
|
Modifier and Type | Field and Description |
---|---|
static TextFile.Mode |
READ
'read' mode
|
static TextFile.Mode |
WRITE
'write' mode
|
static TextFile.Mode |
WRITE_APPEND
'write/append' mode
|
Constructor and Description |
---|
TextFile(Presentable owner,
java.lang.String packagePrefix,
TextFile.Mode mode,
java.lang.String fileName,
java.lang.String charsetName,
char[] separatorsForReading)
Creates new TextFile object based on given name of file
|
TextFile(Presentable owner,
java.lang.String packagePrefix,
java.net.URL url,
java.lang.String charsetName,
char[] separatorsForReading)
Creates new TextFile object in
READ mode, based on given URL |
Modifier and Type | Method and Description |
---|---|
boolean |
canReadMore()
Returns
true if there is available content in the file at
the reading position. |
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.
|
java.lang.String |
getLocation()
Returns the location (path) of the file.
|
void |
print(boolean b)
Prints a boolean value.
|
void |
print(char c)
Prints a character.
|
void |
print(char[] s)
Prints an array of characters.
|
void |
print(double d)
Prints a double-precision floating-point number.
|
void |
print(float f)
Prints a floating-point number.
|
void |
print(int i)
Prints an integer.
|
void |
print(long l)
Prints a long integer.
|
void |
print(java.lang.Object obj)
Prints an object.
|
void |
print(java.lang.String s)
Prints a string.
|
void |
printf(java.util.Locale l,
java.lang.String format,
java.lang.Object... args)
A convenient method to write a formatted string to the text file using
the specified format string and arguments.
|
void |
printf(java.lang.String format,
java.lang.Object... args)
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(boolean x)
Prints a boolean value and then terminates the line.
|
void |
println(char x)
Prints a character and then terminates the line.
|
void |
println(char[] x)
Prints an array of characters and then terminates the line.
|
void |
println(double x)
Prints a double-precision floating-point number and then terminates the
line.
|
void |
println(float x)
Prints a floating-point number and then terminates the line.
|
void |
println(int x)
Prints an integer and then terminates the line.
|
void |
println(long x)
Prints a long integer and then terminates the line.
|
void |
println(java.lang.Object x)
Prints an Object and then terminates the line.
|
void |
println(java.lang.String x)
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.
|
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()
Reads and returns an
int value. |
java.lang.String |
readLine()
Read a line of text.
|
long |
readLong()
Reads and returns a
long value. |
short |
readShort()
Reads and returns a
short value. |
java.lang.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(java.lang.Object owner)
Deprecated.
|
void |
setFile(java.lang.String fileName,
TextFile.Mode 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 |
void |
setMode(TextFile.Mode 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 |
void |
setURL(java.net.URL url)
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(long n)
Skip characters (as well as separator characters).
|
int |
skipTokens(int n)
Skips tokens (texts between separators declared in the constructor)
|
java.lang.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, java.lang.String packagePrefix, TextFile.Mode mode, java.lang.String fileName, java.lang.String charsetName, char[] separatorsForReading)
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
- owner
's class (if included into the class-path
and lies in the owner
's package) -
only READ
mode
charsetName
- The name of a supported charsetCharset.availableCharsets()
.keySet()
null
for the default system charsetseparatorsForReading
- an array (may be null
) 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 lines
For example, read tab-separated file can be processed using
separatorsForReading
=
new char[]{ '\n', '\t' }
public TextFile(Presentable owner, java.lang.String packagePrefix, java.net.URL url, java.lang.String charsetName, char[] separatorsForReading)
READ
mode, based on given URLowner
- 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 charsetCharset.availableCharsets()
.keySet()
null
for the default system charsetseparatorsForReading
- an array (may be null
) 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 lines
For example, read tab-separated file can be processed using
separatorsForReading
=
new char[]{ '\n', '\t' }
@AnyLogicInternalCodegenAPI @Deprecated public void restoreOwner(java.lang.Object owner)
restoreOwner
in interface com.anylogic.engine.internal.Child
owner
- owner of this object, usually Agent
,
Experiment
or
ShapeGroup
public void close()
public void setFile(java.lang.String fileName, TextFile.Mode mode)
READ
fileName
- owner
's class (if included into the class-path
and lies in the owner
's package) -
only READ
mode
mode
- possible values:
public java.lang.String getLocation()
null
if the file location hasn't been set.setFile(String, Mode)
,
setURL(URL)
public void setURL(java.net.URL url)
READ
modeurl
- the url to contents which should be readpublic void setMode(TextFile.Mode mode)
READ
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.b
- The boolean
to be printedpublic void print(char c)
c
- The char
to be printedpublic void print(char[] s)
s
- The array of chars to be printedjava.lang.NullPointerException
- If s
is null
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.d
- The double
to be printedDouble.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.f
- The float
to be printedFloat.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.i
- The int
to be printedInteger.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.l
- The long
to be printedLong.toString(long)
public void print(java.lang.Object obj)
String.valueOf(Object)
method is translated into bytes according to the chosen character
encoding, and these bytes are written to the file.obj
- The Object
to be printedObject.toString()
public void print(java.lang.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.s
- The String
to be printedpublic void println()
line.separator
, and is not necessarily a single newline
character ('\n'
).public void println(boolean x)
print(boolean)
and then
println()
.x
- the boolean
value to be printedpublic void println(char x)
print(char)
and then
println()
.x
- the char
value to be printedpublic void println(char[] x)
print(char[])
and
then println()
.x
- the array of char
values to be printedpublic void println(double x)
print(double)
and then println()
.x
- the double
value to be printedpublic void println(float x)
print(float)
and
then println()
.x
- the float
value to be printedpublic void println(int x)
print(int)
and then
println()
.x
- the int
value to be printedpublic void println(long x)
print(long)
and then
println()
.x
- the long
value to be printedpublic void println(java.lang.Object x)
print(String)
and then
println()
.x
- The Object
to be printed.public void println(java.lang.String x)
print(String)
and then
println()
.x
- the String
value to be printedpublic void printf(java.util.Locale l, java.lang.String format, java.lang.Object... args)
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. 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 a
null
argument depends on the conversion of
Formatter
.java.util.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 the
Formatter
class specification.java.lang.NullPointerException
- If the format
is null
public void printf(java.lang.String format, java.lang.Object... args)
format
- A format string as described in Formatter
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 a
null
argument depends on the conversion of
Formatter
.java.util.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 the
Formatter
class specification.java.lang.NullPointerException
- If the format
is null
public int getLineNumber()
READ
mode0
if the file hasn't been opened yet (no read- and
skip-methods haven't been called as well as canReadMore()
)
-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.READ
modetrue
if there is available content in the file at
the reading position, false
if the end of file has been reachedpublic java.lang.String readLine()
readInt()
was called once), the rest part
of line is returned (in the given example it is "Car" - if this TextFile
has separator ';')null
if the end of the
stream has been reachedpublic boolean readBoolean()
"true"
or "false"
(with or without quotation marks)boolean
value read.public byte readByte()
Byte.MIN_VALUE
... Byte.MAX_VALUE
, inclusive.byte
number value read.public short readShort()
short
value.Short.MIN_VALUE
...
Short.MAX_VALUE
, inclusiveshort
value read.public char readChar()
char
value of character read.public int readInt()
int
value.Integer.MIN_VALUE
...
Integer.MAX_VALUE
, inclusiveint
value read.public long readLong()
long
value.Long.MIN_VALUE
...
Long.MAX_VALUE
, inclusivelong
value read.public float readFloat()
float
value (number with
floating-point).float
value read.public double readDouble()
double
value (number with
floating-point and double-precision).double
value read.public java.lang.String readString()
String
which contains text from
current reading position (which is after previously read separator),
inclusive, to the next separator character position, exclusivedouble
value read.public long skipChars(long n)
n
- the number of characters to skipjava.lang.IllegalArgumentException
- If n
is negativepublic int skipTokens(int n)
n
- the number of tokens to skipjava.lang.IllegalArgumentException
- If n
is negativepublic java.lang.String toString()
toString
in class java.lang.Object
Copyright © AnyLogic North America, LLC. All Rights Reserved.