虽说很简单,但俺刚开始思路绕了,以此记之
public static void saveData() throws Exception {
double begin = System.currentTimeMillis();
Connection conn = null;// 得到数据库连接
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
conn.setAutoCommit(false);// 设置自动提交
String dir = "D:/dir/";
File file = new File(dir);
if (file.isDirectory()) {
System.out.println("读取文件夹中文件信息");
int m = 0;
File[] listFiles = file.listFiles();
Map<String, File> latHashMap = new HashMap<String, File>();
Map<String, File> t2HashMap = new HashMap<String, File>();
Map<String, File> lonHashMap = new HashMap<String, File>();
for (File file2 : listFiles) {
// 把文件夹中所有的文件分类
String name = file2.getName();
if (name.contains("lat")) {
String substring = name.substring(3, name.length() - 4);
latHashMap.put(substring, file2);
} else if (name.contains("t2")) {
String substring = name.substring(2, name.length() - 4);
t2HashMap.put(substring, file2);
} else if (name.contains("lon")) {
String substring = name.substring(3, name.length() - 4);
lonHashMap.put(substring, file2);
}
}
// 操作文件,把同日期的不同文件内容填入到对应的
数据库表的字段中
Set<Entry<String, File>> latEntrySet = latHashMap.entrySet();
System.out.println("开始存入数据库");
String dataKey = null;
for (Entry<String, File> entry : latEntrySet) {
dataKey = entry.getKey();
File latFile = entry.getValue();
File t2File = t2HashMap.get(dataKey);
File lonFile = lonHashMap.get(dataKey);
List<String> Xlist = getData(latFile.getPath());
List<String> Ylist = getData(lonFile.getPath());
List<String> WDlist = getData(t2File.getPath());
for (int i = 0; i < (Xlist.size()); i++) {
m++;
ps = conn
.prepareStatement("insert into Test(id,sb,x,y,wd) values(Test_ID.nextval,?,?,?,?)");
ps.setString(1, dataKey);
ps.setObject(2, Xlist.get(i));
ps.setObject(3, Ylist.get(i));
ps.setObject(4, WDlist.get(i));
ps.addBatch();
ps.executeBatch();
ps.clearBatch();
ps.close();
}
conn.commit();
}
double end = System.currentTimeMillis();
System.out.println("
导入数据表" + m + "条记录用时" + (end - begin)
+ "毫秒,平均存入每条记录用时" + ((end - begin) / m) + "毫秒");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
}
// 读取文件并获得文件中数据(我这里是netCDF格式数据集文集)
public static List<String> getData(String readPath) throws IOException {
File file = new File(readPath);
if (!file.exists()) {
System.out.println("File not exist!");
System.exit(0);
}
BufferedReader br;
br = new BufferedReader(new FileReader(readPath));
String line;
String[] strArr = null;
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile("^\\s*\\d.*");
if (file.exists()) {
int n = 1;
while ((line = br.readLine()) != null) {
n++;
if (p.matcher(line).matches()) {
strArr = line.replace(";","").trim().split(",");// 与文件中的分隔符要一致
for (String data : strArr) {
data.trim();
list.add(data);
}
}
}
}
br.close();
return list;
}