循环读取一个目录下文本文件数据存入oracle数据库_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 循环读取一个目录下文本文件数据存入oracle数据库

循环读取一个目录下文本文件数据存入oracle数据库

 2011/12/8 8:10:52  ljjclub  http://ljjclub.iteye.com  我要评论(0)
  • 摘要:虽说很简单,但俺刚开始思路绕了,以此记之publicstaticvoidsaveData()throwsException{doublebegin=System.currentTimeMillis();Connectionconn=null;//得到数据库连接PreparedStatementps=null;try{conn=DBUtil.getConnection();conn.setAutoCommit(false);//设置自动提交Stringdir="D:/dir/"
  • 标签:目录 文件 ORA 一个 数据库 数据 Oracle Oracle数据库
虽说很简单,但俺刚开始思路绕了,以此记之

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;
}
发表评论
用户名: 匿名