我在数据库中有一个包含5列的表。在CSV文件中,我有如下数据。如何通过删除空列将其处理到Oracle数据库中(因为CSV文件包含6列,它将在具有5列的数据库列中不匹配)。
111, ,John,2000, ,US
222, ,Alle,3000, ,China
333, ,Kite,4000,LCD,IND
444, ,King,5000,LED,Aust
package com.java;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.Vector;
public class Test3
{
public static void main(String[] args)
{
try
{
int commas = 0;
List data = new Vector();
List columnCount = new Vector();
String[] cols = null;
String[] strArray = null;
String file = "D:/temp/CSV/data.csv";
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
do
{
commas = 0;
cols = line.split(",");
strArray = new String[cols.length];
int i=0;
for (String element : cols)
{
if (!isBlank(element))
{
strArray[i] = element;
i++;
}
}
int newFile = line.length();
for(int k = 0; k < newFile; k ++ )
{
char eachChar = line.charAt(k);
if(eachChar == ',')
{
commas ++;
}
}
data.add(strArray);
line = br.readLine();
}
while (line != null);
Vector columns = new Vector(commas + 1);
for(int i = 0; i < commas +1 ; i ++ )
{
columns.add("" + i);
}
} catch (Exception e) {
}
}
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0)
{
return true;
}
for (int i = 0; i < strLen; i++)
{
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
}
答案 0 :(得分:0)
您可以使用util方法检查它是否为空字符串。
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((Character.isWhitespace(str.charAt(i)) == false)) {
return false;
}
}
return true;
}
并在您的阅读器中使用它,如下所示
Scanner s = null;
try {
s = new Scanner(new File("D:/sri/Test.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (s.hasNextLine()) {
String[] line = s.nextLine().split(",");
for (String element : line) {
if (!isBlank(element))
System.out.println(element);
}
}