public class CSVFile
extends Object
Modifier and Type | Field and Description |
---|---|
protected String |
dataDelimiter |
protected String |
encoding
the encoding type of the file
|
protected String |
headerDelimiter |
protected Hashtable |
headers
the data headers contained in the file.
|
protected String |
lineDelimiter |
protected String |
name
the name of the file (including any file extension)
|
protected String |
path
the path of the file to read/write data
|
protected String |
rawFileText
the raw text that is found in the file when reading/writing
|
Constructor and Description |
---|
CSVFile(String path)
Constructs a new file that is ascii encoded
|
CSVFile(String path,
String encoding)
Constructs a new CSVFile
|
Modifier and Type | Method and Description |
---|---|
void |
addHeader(CSVDataHeader header) |
protected CSVDataHeader |
createNewHeader(String rawText)
Create new header from given text (should be two lines) each beginning with the headerDelimiter.
|
protected String |
createRawText()
Traverse through the list of headers and create the raw text for a file
based on the data contained in the headers.
|
String |
getDataDelimiter() |
String |
getFileName() |
CSVDataHeader |
getHeader(String title) |
String |
getHeaderDelimiter() |
Hashtable |
getHeaders() |
String |
getLineDelimiter() |
String |
getPath() |
void |
parseFile(String rawInput)
Parse through the given text saving the given data into CSVDataHeaders
|
protected void |
parseFileMetaData(String text)
Parse the given text to determine the delimiters for parsing the file
|
protected CSVDataHeader |
parseHeaderData(CSVDataHeader header,
String rawText)
Parse the given text and append it to the appropriate columns
in the given CSVDataHeader.
|
protected String |
readFileData()
Reads the text contained in a file based on the file's encoding.
|
void |
removeHeader(String title) |
void |
save()
Saves raw text to the file based on the data contained in the headers list.
|
void |
setDataDelimiter(String d)
Sets the delimiter to use when parsing a row for data
* @param d the new delimiter to use
|
void |
setFileName(String newName) |
void |
setHeaderDelimiter(String d)
Sets the delimiter to use when parsing a header of data
|
void |
setHeaderList(Hashtable newHeaders)
Sets the entire list of headers to the given list
CAUTION:This removes any previously contained data that the data headers of this file contain |
void |
setLineDelimiter(String d)
Sets the delimiter to use when parsing for a row of data
|
void |
setPath(String pth) |
protected void |
writeFileData(String data)
Writes data to the file.
|
protected String encoding
OutputStreamWriter
protected String path
if path
points to an invalid file
or the file does not exist the file is created
instead and read/written to. BE CAREFUL
protected String name
protected Hashtable headers
CSVDataHeader
protected String rawFileText
protected String headerDelimiter
protected String lineDelimiter
protected String dataDelimiter
public CSVFile(String path)
path
- the path of the file to createCSVFile(String path,String encoding)
public CSVFile(String path, String encoding)
path
- the path of the file to createencoding
- the encoding of the text in the filepublic void setLineDelimiter(String d)
d
- the new delimiter to usepublic void setDataDelimiter(String d)
public void setHeaderDelimiter(String d)
d
- the new delimiter to usepublic String getLineDelimiter()
public String getHeaderDelimiter()
public String getDataDelimiter()
public Hashtable getHeaders()
public void setHeaderList(Hashtable newHeaders)
newHeaders
- the new set of headers to usepublic String getPath()
public void setPath(String pth)
pth
- the new path to use for this filepublic String getFileName()
public void setFileName(String newName)
newName
- the new name of this fileprotected String readFileData()
for details on where to read the data from
protected void writeFileData(String data)
data
- the String of data to write to the filepublic void save()
createRawText()
,
writeFileData(String)
public void parseFile(String rawInput)
This is done by reading through each line in the text (denoted by the value "\n")
and parsing the data according to the first character in the line. If the character
is one of the three delimiters then parse it accordingly. If it not skip it and
move on.
rawInput
- the raw text to parse - generally the value returned by readFileData().readFileData()
protected CSVDataHeader parseHeaderData(CSVDataHeader header, String rawText)
header
- the header to append the data torawText
- the raw text to parse
Should be a line of raw text from the file in proper JavaCSVReader format see README.txt
Vector2D.appendToColumn(Object,int)
protected void parseFileMetaData(String text)
view the README.txt for more details on file and text formatting
text
- the raw text to use to parse for the delimiters.
This is normally the FIRST line in a file
protected CSVDataHeader createNewHeader(String rawText)
view the README.txt for more details on file and text formatting
rawText
- the rawText to parse for creating the new CSVDataHeaderpublic void addHeader(CSVDataHeader header)
header
- the new header to add to the list of headers this file containspublic void removeHeader(String title)
title
- the title of the header to remove from the list of headers this file containspublic CSVDataHeader getHeader(String title)
title
- the title of the header to getprotected String createRawText()
save()