Class FileData
java.lang.Object
net.simplace.sim.io.FileData
One file (or a string with file content) used by a file interface. For reading, the content is read once and
cached, either as table (CSV,
getDataCache(Integer[], Integer), see CSVTable) or as XML root element
(getRootElement()); the cache can be shared with an equal file of another session. For writing, lines are
appended to the file (writeToFile(String)). Paths starting with // are converted to UNC
paths.- Author:
- Andreas Enders
-
Constructor Summary
ConstructorsConstructorDescriptionFileData(String aPathname, String aContent, FWSimSession aActiveInSession) Creates the file object without divider (e.g.FileData(String aPathname, String aContent, FWSimSession aActiveInSession, String aDivider, String aProjectID) Creates the file object. -
Method Summary
Modifier and TypeMethodDescriptionvoidCloses the file after writing or reading: closes the output stream and discards the caches.booleanTwo file objects are equal if they have the same path (content objects are never equal).getDataCache(Integer[] aLinesToRead, Integer aSkipHeaderLines) Returns the content as table, split by the divider.getFile()org.jdom2.ElementReturns the XML root element; the file (or content) is parsed on the first call.inthashCode()Consistent withequals(Object): based on the file name.booleanvoidShares the cache of an equal file (e.g.voidsetUpdated(boolean aIsUpdated) toString()Returns the absolute path, or the beginning of the content.voidwriteObjectsToFile(Collection<Object> aCollection, LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables, CSVInterface.DATA_MODE aDataMode) Appends an output line, separated by the divider.voidwriteStringsToFile(Collection<String> aCollection) Appends the strings as one line, separated by the divider;nullis written asNULL.voidwriteToFile(String aLine) Appends a line (with CR LF, platform default charset - the same charset the resources are read with, seeIOHelper.readLinesOfStream(java.io.InputStream, Integer[], String)); the output stream is opened on the first call.
-
Constructor Details
-
FileData
public FileData(String aPathname, String aContent, FWSimSession aActiveInSession, String aDivider, String aProjectID) Creates the file object.- Parameters:
aPathname- path of the file, ornullif the content is givenaContent- the data as string (used only if the path isnull)aActiveInSession- the sessionaDivider- column divider for CSV files, may benullaProjectID- id of the current project (not used)
-
FileData
Creates the file object without divider (e.g. for XML files).- Parameters:
aPathname- path of the file, ornullif the content is givenaContent- the data as string (used only if the path isnull)aActiveInSession- the session
-
-
Method Details
-
setCache
Shares the cache of an equal file (e.g. of another session) instead of reading the file again.- Parameters:
aFileData- the file whose cache is usedaWasUpdated- if the file counts as updated
-
getDataCache
Returns the content as table, split by the divider. The file is read on the first call and again if the lines to read or header lines change; the table of the shared file is used only if it was read with the same divider, lines to read and header lines. Content given as string is cached the same way. The table is shared: the resources convert its columns to their data types in place (seeCSVTable).- Parameters:
aLinesToRead- lines to read, ornullfor allaSkipHeaderLines- number of lines to skip at the start, may benull- Returns:
- the table; line 0 is the header
- Throws:
IOException- if the file cannot be read or is empty
-
getRootElement
Returns the XML root element; the file (or content) is parsed on the first call.- Returns:
- the root element, or
nullwithout file name and content - Throws:
IOException- if the file cannot be read or parsed
-
writeStringsToFile
Appends the strings as one line, separated by the divider;nullis written asNULL.- Parameters:
aCollection- the values of the line (e.g. the header)
-
writeObjectsToFile
public void writeObjectsToFile(Collection<Object> aCollection, LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables, CSVInterface.DATA_MODE aDataMode) Appends an output line, separated by the divider. Values are formatted by theformatof their header variable, e.g.<out id="CURRENT_DAY" datatype="DATE" format="yyyy-MM-dd" .../>or<out id="decimal_num" datatype="DOUBLE" format="%.2f" .../>(12.2363452 → 12.24); booleans with a format are written as 1/0,nullasNULL.Arrays: in COL mode all elements are written in one line (separated by the divider, or by
|if the variable has nocount); in ROW mode one line per element of the longest array is written, shorter arrays are filled withNULL.- Parameters:
aCollection- the values of the line, in the order of the header variablesaHeaderVariables- the header variables (formats and data types)aDataMode- how arrays are written
-
writeToFile
Appends a line (with CR LF, platform default charset - the same charset the resources are read with, seeIOHelper.readLinesOfStream(java.io.InputStream, Integer[], String)); the output stream is opened on the first call. Errors are logged.- Parameters:
aLine- the line without line break
-
getSession
- Returns:
- the session the file is used in
-
closeFile
public void closeFile()Closes the file after writing or reading: closes the output stream and discards the caches. Note: the file name is discarded as well, so the object is no longer equal to other objects for the same file. -
setUpdated
public void setUpdated(boolean aIsUpdated) - Parameters:
aIsUpdated- if the file is new or changed and has to be read again
-
isUpdated
public boolean isUpdated()- Returns:
trueif the file is new or changed and has to be read again (initiallytrue)
-
getFile
- Returns:
- the file, or
nullfor content
-
getUniqueID
- Returns:
- an id of the source data: the path for files, the hash code of the content otherwise
-
equals
-
hashCode
public int hashCode()Consistent withequals(Object): based on the file name. -
toString
-