此代码抛出 -
java.lang.IndexOutOfBoundsException: No group 6 exception
当我尝试从大型CSV文件(800行记录)创建.sql
文件时。如果我使用小型CSV记录(5行记录),它会按预期工作,我可以创建.sql
文件。
1:INSERT INTO bofa_fund1(${keys}) VALUES(${values})
2:INSERT INTO bofa_fund1(Originator Loan Number,Servicer Loan Number,Pool_Num,Fund_Date,Originator,Current Servicer,Gross Rate,Original Balance,Cutoff Balance,Paid Thru Date,Next Due Date,First Pay Date) VALUES(${values})
java.lang.IndexOutOfBoundsException: No group 6
at java.util.regex.Matcher.group(Matcher.java:470)
at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
at java.util.regex.Matcher.replaceFirst(Matcher.java:861)
at java.lang.String.replaceFirst(String.java:2146)
at org.db.createSQLfromCSV.loadCSV(createSQLfromCSV.java:156)
at org.fileimport.CreateSQLServlet.doPost(CreateSQLServlet.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
你能帮我解决这个问题吗?
public class createSQLfromCSV {
public static final char DEFAULT_SEPARATOR = ',';
public static final String NEXT_LINE_STRING ="\n";
public static final String NEXT_TWO_LINE_STRING ="\n \n";
private static final String SQL_INSERT = "INSERT INTO ${table}(${keys}) VALUES(${values})";
private String CREATE_TABLE = "CREATE TABLE ${table}(${keys})";
private static final String TABLE_REGEX = "\\$\\{table\\}";
private static final String KEYS_REGEX = "\\$\\{keys\\}";
private static final String VALUES_REGEX = "\\$\\{values\\}";
private Properties prop = new Properties();
private File inputfilePath;
private File outputFilePath;
private File archiveFolder;
public void loadPropertyfile(){
InputStream in = this.getClass().getClassLoader().getResourceAsStream("org/properties/config.properties");
try {
prop.load(in);
inputfilePath = new File (prop.getProperty("sourcedir"));
outputFilePath = new File (prop.getProperty("outputdir"));
archiveFolder = new File (prop.getProperty("archivedir"));
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* Parse CSV file using OpenCSV library and load
*/
public void loadCSV() throws Exception {
loadPropertyfile();
System.out.println("inputfilePath:" + inputfilePath);
if( inputfilePath.isDirectory() && inputfilePath.list().length == 0)
{
throw new Exception("No file found on the server");
}
else if(inputfilePath.isDirectory() && inputfilePath.list().length == 1 )
{
for (final File fileEntry : inputfilePath.listFiles())
{
if (fileEntry.isFile() && !fileEntry.getName().endsWith(".csv"))
{
System.out.println("The File" + fileEntry.getName() + " is not a CSV format file");
}
if (fileEntry.isFile() && fileEntry.getName().endsWith(".csv"))
{
String inputFileName = fileEntry.getName();
String csvFilename = inputfilePath + "\\" + fileEntry.getName();
String OutputfileName = inputFileName.replace(".csv", "")+"_"+getDateTime()+ ".sql";
File outputfile = new File (outputFilePath, OutputfileName );
CSVReader csvReader = null;
BufferedWriter writer = new BufferedWriter( new FileWriter( outputfile));
try {
csvReader = new CSVReader(new FileReader(csvFilename), DEFAULT_SEPARATOR);
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Error occured while executing file. "
+ e.getMessage());
}
String[] headerRow = csvReader.readNext();
if (headerRow == null) {
throw new FileNotFoundException(
"No columns defined in given CSV file." +
"Please check the CSV file format.");
}
String[] nextLine;
String tableName = inputFileName.replace(".csv", "");
String dbVarType = "VARCHAR2(255 BYTE)";
String createTablequery = CREATE_TABLE.replaceFirst(TABLE_REGEX, tableName);
createTablequery = createTablequery
.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, " "+dbVarType+","));
System.out.println("createTablequery: " + createTablequery);
writer.write(createTablequery);
writer.write(NEXT_TWO_LINE_STRING);
writer.write("LOGGING " + NEXT_LINE_STRING);
writer.write("TABLESPACE"+" "+ '"' + "BIC_DATA" + '"'+" "+NEXT_LINE_STRING);
writer.write("PCTFREE 10 " +NEXT_LINE_STRING);
writer.write("INITRANS 1 " +NEXT_LINE_STRING);
writer.write("STORAGE " +NEXT_LINE_STRING);
writer.write("(" +NEXT_LINE_STRING);
writer.write ("INITIAL 81920 "+NEXT_LINE_STRING);
writer.write("NEXT 1048576 "+NEXT_LINE_STRING);
writer.write ("MINEXTENTS 1 "+ NEXT_LINE_STRING);
writer.write ("MAXEXTENTS 2147483645 "+ NEXT_LINE_STRING);
writer.write ("BUFFER_POOL DEFAULT "+NEXT_LINE_STRING);
writer.write(")" +NEXT_LINE_STRING);
writer.write("SET DEFINE OFF"+ NEXT_LINE_STRING);
while ((nextLine = csvReader.readNext()) != null)
{
String insertquery = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
System.out.println("1:" + insertquery);
insertquery = insertquery
.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
System.out.println("2:" + insertquery);
insertquery = insertquery.replaceFirst(VALUES_REGEX,StringUtils.join(nextLine, ",") ) +";"+NEXT_LINE_STRING;
System.out.println("3:" + insertquery);
writer.write(insertquery);
}
writer.close();
csvReader.close();
MoveFiles.moveTheFile(inputfilePath,archiveFolder);
}
}
}
else
{
throw new Exception("Multiple files found on the server");
}
}
private final static String getDateTime()
{
DateFormat df = new SimpleDateFormat("yyyyMMdd_hhmmss");
return df.format(new Date());
}
}
答案 0 :(得分:7)
其中一行CSV行中包含$6
。当您尝试进行正则表达式替换时,java将其解释为对捕获组的引用,并在正则表达式匹配中查找该组。在这种情况下它不存在。
尝试逃避替换:
insertquery = insertquery.replaceFirst(VALUES_REGEX, Matcher.quoteReplacement(StringUtils.join(nextLine, ","))) + ";" + NEXT_LINE_STRING;