日期:2014-05-16  浏览次数:20339 次

定时读取一个目录下文件数据存入oracle数据库(原创)
虽说很简单,但俺刚开始思路绕了,以此记之

package com.bj.save;

import java.io.File;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.bj.util.DBUtil;

public class BackupData {
	
	
	
	/**
	 * 将读取出来的数据插入到数据库
	 * 
	 * @throws SQLException
	 */
	public void saveData(String localdir) throws SQLException {

		double begin = System.currentTimeMillis();
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = DBUtil.getConnection();
			DBUtil.setAutoCommit(conn, false);// 设置自动提交
			File file = new File(localdir);
			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);
						/**
						 * StringBuffer s = new StringBuffer( substring);
						 * //文件名里面包含有小时s.insert(10, ": ");
						 */
						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 = ReadFile.getData(latFile.getPath());
					List<String> Ylist = ReadFile.getData(lonFile.getPath());
					List<String> WDlist = ReadFile.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();

						if (i % 500 == 0 || i == Xlist.size() - 1) {
							conn.commit();
							ps.clearBatch();
						}
					}
					ps.close();
				}

				double end = System.currentTimeMillis();
				System.out.println("导入日常预测数据表" + m + "条记录用时" + (end - begin)
						+ "毫秒,平均存入每条记录用时" + ((end - begin) / m) + "毫秒");
			}
		} catch (Exception e) {
			conn.rollback();
			e.printStackTrace();
		} finally {
			conn.close();
		}
	}
}


	// 读取文件并获得文件中数据(我这里是netCDF格式数据集文件)
	public static List<String> getData(String readPath) throws IOException {

		File file = new File(readPath);
		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(";","").replaceAll("\\s*", "").split(",");// 与文件中的分隔符要一致

					for (String data : strArr) {
						data.trim();
						list.add(data);
					}
				}
			}

		}
		br.close();
		return list;
	}


至于定时读取,用Quartz很简单的 0_0