Libraries.Data.Formats.SeparatedValueFileReader Documentation
This class reads in text and separates it into lines. It loads the text from the file when the first ReadLine or ReadLines call is issued and not before.
Example Code
use Libraries.Data.Formats.SeparatedValueFileReader
use Libraries.Containers.Array
use Libraries.System.File
SeparatedValueFileReader reader
File file
file:SetPath("file.csv")
reader:OpenForRead(file)
Array<text> value = reader:ReadLines()
Inherits from: Libraries.System.FileReader, Libraries.System.Blueprints.FileReaderBlueprint, Libraries.Language.Object
Actions Documentation
Close()
When we open a file for reading, it is marked by the operating system as being in use by our application. To make it possible for other files to use this file at a later point in time, it is necessary to tell the operating system when we are done with this file. To do this, we use the Close() action. We should call Close() as soon as we know that a file is no longer needed. If no file is open, Close() will do nothing. After Close() is called, a FileReader object is considered invalid, and no actions should be called on it, other than OpenForRead(), to open the same (or different) file.
Example
use Libraries.System.File
use Libraries.System.FileReader
// Open a file, read all the contents and close it.
File f
f:SetPath("To Do List.txt")
FileReader reader
reader:OpenForRead(f)
output "Tim, your to do list says:"
output reader:Read()
// Close the file, as we no longer need it.
reader:Close()
Compare(Libraries.Language.Object object)
This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.
Parameters
- Libraries.Language.Object: The object to compare to.
Return
integer: The Compare result, Smaller, Equal, or Larger.
Example
Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)
Equals(Libraries.Language.Object object)
This action determines if two objects are equal based on their hash code values.
Parameters
- Libraries.Language.Object: The to be compared.
Return
boolean: True if the hash codes are equal and false if they are not equal.
Example
use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)
GetCharacterIndex()
If ReadLine is being called, this returns the current index being processed.
Return
integer:
GetCharacterSize()
This returns the number of characters that were read in the file.
Return
integer: the number of characters
GetHashCode()
This action gets the hash code for an object.
Return
integer: The integer hash code of the object.
Example
Object o
integer hash = o:GetHashCode()
GetStoreLines()
Returns true if we store lines on every call to ReadLine.
Return
boolean:
IsAtEndOfFile()
This action returns true if we have finished reading the file.
Return
boolean: true if we have finished reading.
OpenForRead(Libraries.System.File file)
Open a file for sequential reading. By "sequential reading," it is meant that it is impossible to go backwards in the file--we must make progress toward the end of the file with each successive read. This is extremely useful for reading plain text documents, but is much less useful for reading binary files. Binary files should use the FileRandomAccess class.
Parameters
Example
use Libraries.System.File
use Libraries.System.FileReader
// Open the file for reading.
File f
f:SetPath("homework.txt")
// Read the first two lines and then be done.
FileReader reader
reader:OpenForRead(f)
output reader:ReadLine()
output reader:ReadLine()
// Always close the file.
reader:Close()
Read(integer numberOfBytes)
This action reads the specified number of bytes after our current position. When we open a file, we start at the beginning--calling this action with a parameter of, for example, 2, would read the first two bytes of the file. If we perform some other reading action such as ReadLine(), and then call this action, the specified number of bytes will be read after our current position. If the number of bytes specified exceeds the end of the file, content up to the end of the file will be returned. If the end of the file has been reached before this action is called, an EndOfFileError will be raised.
Parameters
- integer numberOfBytes: the number of bytes we wish to read after our current location in the file. This must be a positive number; if it is not, an InvalidArgumentError is raised.
Return
text: the file contents we have read
Example
use Libraries.System.File
use Libraries.System.FileReader
// Open a file, output the first four bytes.
File f
f:SetPath("Settings.dat")
FileReader reader
reader:OpenForRead(f)
output "The first four bytes are " + reader:Read(4)
reader:Close()
Read()
This action reads all of the contents of our file from the current position. When we open a file, we start at the beginning--so calling this action immediately after opening a file will read the entire file. If we perform some other reading action, such as ReadLine(), and then call this action, we will receive all file contents after the point where we have read. If the end of file has already been reached before calling this action, an EndOfFileError will be raised.
Return
text: the file contents from our current position to the end of the file.
Example
use Libraries.System.File
use Libraries.System.FileReader
// Open a file, read all of its contents to a text value and then output the text value.
File f
f:SetPath("Settings.dat")
FileReader reader
reader:OpenForRead(f)
text settings = reader:Read()
output "The settings are:"
output settings
reader:Close()
ReadLine()
This action reads one line from the loaded text. By default, it stores the line in an array, in addition to returning it. This can be turned off by calling SetStoreLines(false)
Return
text:
Example
use Libraries.Data.Formats.SeparatedValueFileReader
use Libraries.Containers.Array
use Libraries.System.File
SeparatedValueFileReader reader
File file
file:SetPath("file.csv")
reader:OpenForRead(file)
output reader:ReadLine()
ReadLines()
This class reads the entire file into an array. If ReadLine has been called prior to this action being called, with SetStoreLines set to false, then this begins filling the array from this point. If called from the beginning, this action ignores the value of SetStoreLines, because otherwise this would be guaranteed to return an empty array.
Return
Libraries.Containers.Array: an array with all lines in the file.
Example
use Libraries.Data.Formats.SeparatedValueFileReader
use Libraries.Containers.Array
use Libraries.System.File
SeparatedValueFileReader reader
File file
file:SetPath("file.csv")
reader:OpenForRead(file)
Array<text> value = reader:ReadLines()
SetStoreLines(boolean storeLines)
Sets whether or not we store lines on every call to ReadLine.
Parameters
- boolean storeLines
Setup()
If ReadLine is being called, this returns the current index being processed.
On this page
Variables TableAction Documentation- Close()
- Compare(Libraries.Language.Object object)
- Equals(Libraries.Language.Object object)
- GetCharacterIndex()
- GetCharacterSize()
- GetHashCode()
- GetStoreLines()
- IsAtEndOfFile()
- OpenForRead(Libraries.System.File file)
- Read(integer numberOfBytes)
- Read()
- ReadLine()
- ReadLines()
- SetStoreLines(boolean storeLines)
- Setup()