Package org.sm.smtools.util
Class TextFileWriter
java.lang.Object
org.sm.smtools.util.TextFileWriter
public final class TextFileWriter
extends java.lang.Object
The
TextFileWriter
class allows easy writing to text files.
In the resulting text file, each line will contain at most one value.
The available datatypes to write are:
int
double
String
(unquoted)boolean
(true and false)Date
(dd/mm/yyyy)Time
(hh:mm:ss.ms)- comma-separated values (CSV)
Note that this class cannot be subclassed!
- Version:
- 02/08/2019
- Author:
- Sven Maerivoet
-
Constructor Summary
Constructors Constructor Description TextFileWriter(java.lang.String filename)
Sets up a text file writer for the specified file.TextFileWriter(java.lang.String filename, boolean append)
Sets up a text file writer for the specified file. -
Method Summary
Modifier and Type Method Description void
close()
Closes the file.void
writeBoolean(boolean b)
Writes a string representation of aboolean
to the file.void
writeCSV(java.lang.String[] csvValues)
Write comma-separated values (CSV) to the file.void
writeDate(DateStamp dateStamp)
Writes a string representation of aDateStamp
object to the file.void
writeDateTimeStamp(DateStamp dateStamp, TimeStamp timeStamp)
Writes a string representation of both aDateStamp
and aTimeStamp
object to the file.void
writeDouble(double d)
Writes a string representation of adouble
to the file.void
writeInteger(int i)
Writes a string representation of anint
to the file.void
writeLn()
Writes an empty line to the file.void
writeString(java.lang.String s)
Writes a string to the file.void
writeTimeStamp(TimeStamp timeStamp)
Writes a string representation of aTimeStamp
object to the file.Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
TextFileWriter
Sets up a text file writer for the specified file.- Parameters:
filename
- the name of the file to create- Throws:
FileCantBeCreatedException
- if the file cannot be created
-
TextFileWriter
Sets up a text file writer for the specified file.- Parameters:
filename
- the name of the file to createappend
- whether or not the (existing) file should be appended- Throws:
FileCantBeCreatedException
- if the file cannot be created
-
-
Method Details
-
close
public final void close()Closes the file. -
writeLn
Writes an empty line to the file.- Throws:
FileWriteException
- if an error during the writing occurred
-
writeInteger
Writes a string representation of anint
to the file.- Parameters:
i
- theint
to write to the file- Throws:
FileWriteException
- if theint
could not be written to the file
-
writeDouble
Writes a string representation of adouble
to the file.- Parameters:
d
- thedouble
to write to the file- Throws:
FileWriteException
- if thedouble
could not be written to the file
-
writeString
Writes a string to the file.- Parameters:
s
- theString
to write to the file- Throws:
FileWriteException
- if theString
could not be written to the file
-
writeBoolean
Writes a string representation of aboolean
to the file.The string representation is either TRUE or FALSE.
- Parameters:
b
- theboolean
to write to the file- Throws:
FileWriteException
- if theboolean
could not be written to the file
-
writeDate
Writes a string representation of aDateStamp
object to the file.The string representation of the
DateStamp
object is dd/mm/yyyy, e.g., "11/04/1976".- Parameters:
dateStamp
- theDateStamp
object to write to the file- Throws:
FileWriteException
- if theDateStamp
object could not be written to the file
-
writeTimeStamp
Writes a string representation of aTimeStamp
object to the file.The string representation of the
TimeStamp
object is hh:mm:ss.ms, e.g., "12:45:16.154".- Parameters:
timeStamp
- theTimeStamp
object to write to the file- Throws:
FileWriteException
- if theTimeStamp
object could not be written to the file
-
writeDateTimeStamp
public final void writeDateTimeStamp(DateStamp dateStamp, TimeStamp timeStamp) throws FileWriteExceptionWrites a string representation of both aDateStamp
and aTimeStamp
object to the file.The string representation of the
TimeStamp
object is yyyy-MM-ddT:HH:mm:ssZ.- Parameters:
dateStamp
- theDateStamp
object to write to the filetimeStamp
- theTimeStamp
object to write to the file- Throws:
FileWriteException
- if theTimeStamp
object could not be written to the file
-
writeCSV
Write comma-separated values (CSV) to the file.Note that all strings are automatically quoted.
- Parameters:
csvValues
- the comma-separated values to write to the file- Throws:
FileWriteException
- if theTimeStamp
object could not be written to the file
-