📄 csvreader.java
字号:
/**
Copyright (C) 2002-2003 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.relique.jdbc.csv;
import java.io.*;
import java.util.*;
import java.sql.SQLException;
/**
* This class is a helper class that handles the reading and parsing of data
* from a .csv file.
*
* @author Zoran Milakovic
*/
public class CsvReader
{
private BufferedReader input;
private String[] columnNames;
private String[] columnTypes;
private String[] columns;
private java.lang.String buf = null;
private char separator = CsvDriver.DEFAULT_SEPARATOR;
private long maxFileSize = CsvDriver.DEFAULT_FILE_MAXSIZE;
private String extension = CsvDriver.DEFAULT_EXTENSION;
private boolean suppressHeaders = false;
private String lineBreakEscape = CsvDriver.DEFAULT_LINE_BREAK_ESCAPE;
private String doubleQuoteEscape = CsvDriver.DEFAULT_DOUBLE_QUOTE_ESCAPE;
private String carriageReturnEscape = CsvDriver.DEFAULT_CARRIAGE_RETURN_ESCAPE;
private String tableName;
private String fileName;
private String charset = null;
/**
*
* @param fileName
* @param separator
* @param suppressHeaders
* @param charset
* @param extension
* @throws java.lang.Exception
*/
public CsvReader(
String fileName,
char separator,
boolean suppressHeaders,
String charset,
String extension,
String lineBreakEscape,
String carriageReturnEscape
)
throws java.lang.Exception
{
this.separator = separator;
this.suppressHeaders = suppressHeaders;
this.fileName = fileName;
this.charset = charset;
this.lineBreakEscape = lineBreakEscape;
this.carriageReturnEscape = carriageReturnEscape;
if( extension != null )
this.extension = extension;
if (charset != null) {
input = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),charset));
} else {
input = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
}
if (this.suppressHeaders)
{
// No column names available.
// Read first data line and determine number of colums.
buf = input.readLine();
String[] data = parseCsvLineAsHeader(buf);
columnNames = new String[data.length];
for (int i = 0; i < data.length; i++)
{
columnNames[i] = "COLUMN" + String.valueOf(i+1);
}
data = null;
}
else
{
String headerLine = input.readLine();
columnNames = parseCsvLineAsHeader(headerLine);
}
}
/**
* Gets the columnNames attribute of the CsvReader object
*
* @return The columnNames value
*/
public String[] getColumnNames()
{
return columnNames;
}
/**
*
* @return array with column types
*/
public String[] getColumnTypes()
{
return columnTypes;
}
public String getTableName() {
if(tableName != null)
return tableName;
int lastSlash = 0;
for(int i = fileName.length()-1; i >= 0; i--)
if(fileName.charAt(i) == '/' || fileName.charAt(i) == '\\') {
lastSlash = i;
break;
}
tableName = fileName.substring(lastSlash+1, fileName.length() - 4);
return tableName;
}
/**
* Get the value of the column at the specified index.
*
* @param columnIndex Description of Parameter
* @return The column value
* @since
*/
public String getColumn(int columnIndex) throws SQLException
{
if (columnIndex >= columns.length)
{
return null;
}
return formatString( columns[columnIndex] );
}
/**
* Get value from column at specified name.
* If the column name is not found, throw an error.
*
* @param columnName Description of Parameter
* @return The column value
* @exception SQLException Description of Exception
* @since
*/
public String getColumn(String columnName) throws SQLException
{
for (int loop = 0; loop < columnNames.length; loop++)
{
if (columnName.equalsIgnoreCase(columnNames[loop])
|| columnName.equalsIgnoreCase(getTableName() + "."
+ columnNames[loop]))
{
return getColumn(loop);
}
}
throw new SQLException("Column '" + columnName + "' not found.");
}
/**
*Description of the Method
*
* @return Description of the Returned Value
* @exception SQLException Description of Exception
* @since
*/
public boolean next() throws SQLException {
columns = new String[columnNames.length];
String dataLine = null;
try {
if (suppressHeaders && (buf != null)) {
// The buffer is not empty yet, so use this first.
dataLine = buf;
buf = null;
} else {
// read new line of data from input.
dataLine = input.readLine();
}
if (dataLine == null) {
String nextFileName = getNextFileName();
if (new File(nextFileName).exists()) {
this.fileName = nextFileName;
if (charset != null) {
input = new BufferedReader(new InputStreamReader(new
FileInputStream(fileName), charset));
}
else {
input = new BufferedReader(new InputStreamReader(new
FileInputStream(fileName)));
}
//skip header
dataLine = input.readLine();
dataLine = input.readLine();
}
else {
input.close();
return false;
}
}
} catch (IOException e) {
throw new SQLException(e.toString());
}
columns = parseCsvLine(dataLine);
return true;
}
private String getNextFileName() {
String currentFileName = this.fileName;
String newName = "";
String number = "";
//name without extension
String currentFileExtension = currentFileName.substring(currentFileName.lastIndexOf("."), currentFileName.length());
currentFileName = currentFileName.substring(0, currentFileName.lastIndexOf("."));
if( currentFileExtension.endsWith(CsvDriver.FILE_NAME_EXT) ) {
number += currentFileName.substring(currentFileName.length()-3, currentFileName.length());
long num = Long.valueOf(number).longValue()+1;
if( num >= 100 && num < 1000 )
number = String.valueOf( num );
else if ( num >= 10 && num < 100 )
number = "0"+String.valueOf( num );
else if ( num > 1 && num < 10 )
number = "00"+String.valueOf( num );
currentFileName = currentFileName.substring(0, currentFileName.length()-3);
newName = currentFileName + number + currentFileExtension;
} else {
newName = currentFileName.toUpperCase() + "001" + this.extension + CsvDriver.FILE_NAME_EXT;
}
return newName;
}
/**
*Description of the Method
*
* @since
*/
public void close()
{
try
{
input.close();
buf = null;
}
catch (Exception e)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -