📄 csvreader.java
字号:
}
}
/**
*
* Parse csv line with columnTypes.
*
* @param line
* @return array with values or column names.
* @throws SQLException
*/
protected String[] parseCsvLine(String line) throws SQLException
{
ArrayList values = new ArrayList();
boolean inQuotedString = false;
String value = "";
String orgLine = line;
int currentPos = 0;
int fullLine = 0;
int currentColumn = 0;
int indexOfBinaryObject = 0;
char currentChar;
line += separator;
long lineLength = line.length();
while (fullLine == 0) {
currentPos = 0;
while (currentPos < lineLength) {
//handle BINARY columns
if( !(this.columnTypes.length <= currentColumn ) ) {
if (this.columnTypes[currentColumn].equals(CsvDriver.BINARY_TYPE)) {
String binaryValue = "";
currentChar = line.charAt(currentPos);
if (currentChar == ',') {
values.add(binaryValue); //binary value is null;
currentPos ++;
}
else if (currentChar == '"') {
if (line.charAt(currentPos + 1) == '"') {
values.add(binaryValue); //binary value is null
currentPos = currentPos + 3;
}
else {
// take all until next separator, and that is value
// do not insert BinaryObject+index into line, just set right currentPos
// and insert value into vector
// binary value is always beteween quotes (")
binaryValue = line.substring(currentPos);
binaryValue = binaryValue.substring(1,
binaryValue.indexOf(separator) -
1);
values.add(binaryValue);
currentPos += binaryValue.length() + 3;
}
}
//set currentColumn++
currentColumn++;
continue;
}
} else {
throw new SQLException("Invalid csv format : file = "+new File(fileName).getAbsolutePath()+", line = "+line);
}
//parse one by one character
currentChar = line.charAt(currentPos);
if (value.length() == 0 && currentChar == '"' && !inQuotedString) {
//enter here if we are at start of column value
currentPos++;
inQuotedString = true;
continue;
}
if (currentChar == '"') {
//get next character
char nextChar = line.charAt(currentPos + 1);
//if we have "", consider it as ", and add it to value
if (nextChar == '"') {
value += currentChar;
currentPos++;
}
else {
//enter here if we are at end of column value
if (!inQuotedString) {
throw new SQLException("Unexpected '\"' in position " +
currentPos + ". Line=" + orgLine);
}
if (inQuotedString && nextChar != separator) {
throw new SQLException("Expecting " + separator +
" in position " + (currentPos + 1) +
". Line=" + orgLine);
}
//set currentPos to comma after value
currentPos++;
//if value is empty string between double quotes consider it as empty string
//else if value is empty string between commas consider it as null value
values.add(value);
currentColumn++;
value = "";
inQuotedString = false;
}
}
else {
//when we are at end of column value, and value is not inside of double quotes
if (currentChar == separator) {
//when have separator in data
if (inQuotedString) {
value += currentChar;
}
else {
//if value is empty string between double quotes consider it as empty string
//else if value is empty string between commas consider it as null value
if( value.equals("") )
value = null;
values.add(value);
currentColumn++;
value = "";
}
}
else {
value += currentChar;
}
}
currentPos++;
} //end while
if (inQuotedString) {
// Remove extra , added at start
value = value.substring(0, value.length() - 1);
try {
line = input.readLine();
}
catch (IOException e) {
throw new SQLException(e.toString());
}
}
else {
fullLine = 1;
}
}// end while( fullLine == 0 )
String[] retVal = new String[values.size()];
values.toArray(retVal);
return retVal;
}
/**
*
* Parse csv line, whithout columnTypes.
*
* @param line
* @return array with values or column names.
* @throws SQLException
*/
protected String[] parseCsvLineAsHeader(String line) throws SQLException
{
Vector values = new Vector();
ArrayList columnTypesList = new ArrayList();
boolean inQuotedString = false;
String value = "";
String orgLine = line;
int currentPos = 0;
int fullLine = 0;
while (fullLine == 0) {
currentPos = 0;
line += separator;
while (currentPos < line.length()) {
char currentChar = line.charAt(currentPos);
if (value.length() == 0 && currentChar == '"' && !inQuotedString) {
currentPos++;
inQuotedString = true;
continue;
}
if (currentChar == '"') {
char nextChar = line.charAt(currentPos + 1);
if (nextChar == '"') {
value += currentChar;
currentPos++;
}
else {
if (!inQuotedString) {
throw new SQLException("Unexpected '\"' in position " +
currentPos + ". Line=" + orgLine);
}
if (inQuotedString && nextChar != separator) {
throw new SQLException("Expecting " + separator + " in position " +
(currentPos + 1) + ". Line=" + orgLine);
}
if (value.endsWith("-"+CsvDriver.BINARY_TYPE)) {
columnTypesList.add(CsvDriver.BINARY_TYPE);
value = value.substring(0,value.indexOf("-"+CsvDriver.BINARY_TYPE));
}
else
columnTypesList.add(CsvDriver.VARCHAR_TYPE);
values.add(value);
value = "";
inQuotedString = false;
currentPos++;
}
}
else {
if (currentChar == separator) {
if (inQuotedString) {
value += currentChar;
}
else {
if (value.endsWith("-"+CsvDriver.BINARY_TYPE)) {
columnTypesList.add(CsvDriver.BINARY_TYPE);
value = value.substring(0,
value.indexOf("-"+CsvDriver.BINARY_TYPE));
}
else
columnTypesList.add(CsvDriver.VARCHAR_TYPE);
values.add(value);
value = "";
}
}
else {
value += currentChar;
}
}
currentPos++;
}
if (inQuotedString) {
value = value.substring(0, value.length() - 1);
try {
line = input.readLine();
}
catch (IOException e) {
throw new SQLException(e.toString());
}
}
else {
fullLine = 1;
}
}
String[] retVal = new String[values.size()];
values.copyInto(retVal);
this.columnTypes = new String[columnTypesList.size()];
columnTypesList.toArray(columnTypes);
return retVal;
}
private String formatString(String str) throws SQLException {
String retValue = str;
try {
//replace spec. characters
retValue = Utils.replaceAll(retValue,this.lineBreakEscape, "\n");
retValue = Utils.replaceAll(retValue,this.carriageReturnEscape, "\r");
// retValue = CsvSqlParser.replaceAll(retValue,this.doubleQuoteEscape, "\"");
}catch(Exception e) {
throw new SQLException("Error while reformat string ! : "+str);
}
return retValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -