Text file access tool. The object allows writing to text file and reading from it.
The object work in one of the following modes:
To add text file 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 filevoid 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 function behaves as though it invokes print(value) and then println().
void printf(java.util.Locale l, String format, Object... args)
- Convenient function 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 function 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 function to write a formatted string to the text file using the specified format string and arguments.
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.
boolean canReadMore() - Returns true if there is available content in the file at the reading position. This function opens file for reading if it is not open.
int getLineNumber() - The function returns the current line number (1-based).
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).