⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readtxtdatabatch.java

📁 定时将.txt数据导入mssql数据库。TestAutoRun.java文件为第一个调用文件。
💻 JAVA
字号:
package myseverlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;


/**
 * 远程读源文件(.txt),并逐条将文件插入数据库
 * 
 * 
 * @author Administrator
 *
 */
public class ReadTxtDataBatch {

	private String todayDateTime = new Timestamp(System.currentTimeMillis())
			.toString();

	private String today = todayDateTime.substring(0, 10);

	private String logMsg = "";

	public void readRun() throws Exception {
		System.out.println("ReadTxtData readRun() is in");
		String yesterday = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000).toString();
		String hour = todayDateTime.substring(11, 13);
		String mi = todayDateTime.substring(14, 16);
		String fileName = "";

		// 获得fileName
		// 每天00点将昨天的数据读入flash2008_table,并删除昨天的nowflash_table
		//if (hour.equals("00") &&(mi.compareTo("30") < 0))
		if (hour.equals("00") && (mi.compareTo("30") < 0)) {
			System.out.println("删除昨天的nowflash_table");
			// 获得文件名称	
			//fileName = toFileName(yesterday, "23", "0").substring(0, 10) + ".txt";
			fileName = toFileName(yesterday, "23", "1");
			// 将昨天的23_1.txt内容插入数据库
			// 1:执行finally内容[删除nowflash表]
			inserTable(fileName, "1");
			
			// 填写log
			logMsg = fileName + " had been saved into nowflash_table" + "    "
					+ todayDateTime;
			LogPrint.logWrite("d:\\log\\" + today + ".txt", logMsg);

			// 填写log
			logMsg = "nowflash_table had been delete into MsSql" + "    "
					+ todayDateTime;
			LogPrint.logWrite("d:\\log\\" + today + ".txt", logMsg);
			System.out.println("零点删除昨天的nowflash_table");
		} else {
             String hourFlag = "0";
			// hour - 1
			if (mi.compareTo("30") < 0) {

				String beforHour = toBeforeHour(hour);
				// 去读前半小时内容
				hourFlag = "1";
				fileName = toFileName(today, beforHour, hourFlag);

				// 当前hour
			} else {
				// 去读前半小时内容
				hourFlag = "0";
				fileName = toFileName(today, hour, hourFlag);
			}

			// 从txt读源数据,存入MsSql
			// 0:不执行 finally内容[不删除nowflash表]
			inserTable(fileName, "0");
			// 填写log
			logMsg = fileName + " had been inserted into nowFlash_table"
					+ "    " + todayDateTime;
			LogPrint.logWrite("d:\\log\\nowflash" + today + ".txt", logMsg);
		}

		System.out.println("ReadTxtData readRun() is out");
	}

	/**
	 * 获得文件名称
	 * 
	 * @param date 日期(yyyy-mm-dd)
	 * @param hour 小时
	 * @param hourflag 标志(0:前半小时,1:后半小时)
	 * @return
	 */
	public String toFileName(String date, String hour, String hourflag) {
		System.out.println("ReadTxtData toFileName is in");
		String returnFileName = "";
		if ((date != null) && (date != null) && date.length() == 10) {
			String year = date.substring(0, 4);
			String mon = date.substring(5, 7);
			String day = date.substring(8, 10);
			returnFileName = year + "_" + mon + "_" + day + "_" + hour + "_"
					+ hourflag + ".txt";
			System.out.println("returnFileName is ======" + returnFileName);
		}
		System.out.println("ReadTxtData toFileName is out");
		return returnFileName;

	}

	/**
	 * 返回前一小时
	 * 
	 * @param hour
	 * @return
	 */
	public String toBeforeHour(String hour) {
		System.out.println("ReadTxtData toBeforeHour is in");
		System.out.println("String hour is " + hour);
		String returnHour = "";
		if ((hour != null) && (hour != "") && (hour.length() == 2)) {

			String temp1 = hour.substring(0, 1);
			if (temp1.equals("0")) {
				String temp2 = hour.substring(1, 2);
				int temphour = Integer.parseInt(temp2) - 1;
				returnHour = "0" + String.valueOf(temphour);
			} else if (hour.equals("10")) {
				returnHour = "09";
			} else {
				int temphour = Integer.parseInt(hour) - 1;
				returnHour = String.valueOf(temphour);
			}
		}

		System.out.println("returnHour =======" + returnHour);
		System.out.println("ReadTxtData toBeforeHour is out");
		return returnHour;
	}

	/**
	 * @param fileName 插入数据源名称
	 * 
	 * @param flag 标识位(0:不执行finally内容  1:执行finally内容, 3:插入指定一天的数据)
	 * 
	 * 从文本文件中读出源数据并插入到MsSql数据库中
	 * 
	 */
	public void inserTable(String fileName, String flag) {
		
		System.out.println("ReadTxtData inserMssql is in fileName=" + fileName);
		System.out.println("ReadTxtData inserMssql is in flag=" + flag);
		// 据库连接
		MssqlDbconn db = new MssqlDbconn();
		String nowFlashSql = "insert into  nowflash_table (latitude,longtitude,intens,slope,error,location,datetime,hour,minute,second,minisecond,province,district,country) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		String flash2008Sql = "insert into  flash2008_table (latitude,longtitude,intens,slope,error,location,datetime,hour,minute,second,minisecond,province,district,country) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement nowFlashPstmt = db.DBConn(nowFlashSql);
		PreparedStatement flash2008Pstmt = db.DBConn(flash2008Sql);
		try {
			// 将源文件拷贝到本地
			 copyFile(fileName, flag);
			
			// 读本地文件,flag="3"时读一天的文件,flag=其它时读半小时文件。
			String readir;
			if (flag.equals("3")) {
				readir = "D:\\project\\tmp\\oneday\\";
			} else {
				readir = "D:\\project\\tmp\\halfhour\\";
			}
			System.out.println("ReadTxtData inserMssql is in readir=" + readir);
			FileReader reader = new FileReader(readir + fileName);
			BufferedReader in = new BufferedReader(reader);
			String temp = "";
			
			// 逐行读.txt文件并对数据进行处理
			// 插入nowflash_table、flash2008_table
			while ((temp = in.readLine()) != null) {
				String dataString = new String(temp);
				// 将字符串中的所有连续空格替换成一个空格
				String newStr = dataString.replaceAll(" {2,}", " ");
				String[] ss = newStr.split(" ");

				// 准备插入的数据
				String date = ss[1];
				String time = ss[2];
				String tempDatatime = date + " " + time.substring(0, 8);
				Timestamp datetime = new Timestamp(0).valueOf(tempDatatime);
				float latitude = Float.valueOf(ss[3].substring(3)).floatValue();
				float longtitude = Float.valueOf(ss[4].substring(3))
						.floatValue();
				float intens = Float.valueOf(ss[5].substring(3)).floatValue();
				float slope = Float.valueOf(ss[6].substring(3)).floatValue();
				float error = Float.valueOf(ss[7].substring(3)).floatValue();
				String location = ss[8].substring(5);
				int hour = Integer.parseInt(time.substring(0, 2));
				int minute = Integer.parseInt(time.substring(3, 5));
				int second = Integer.parseInt(time.substring(6, 8));
				int minisecond = Integer.parseInt(time.substring(9));
				String province = "";
				String district = "";
				String country = "";
                
				// 当flag!=3时插入NowFlashTable表
				if (!flag.equals("3")){
					// insert NowFlashTable
					nowFlashPstmt.setFloat(1, latitude);
					nowFlashPstmt.setFloat(2, longtitude);
					nowFlashPstmt.setFloat(3, intens);
					nowFlashPstmt.setFloat(4, slope);
					nowFlashPstmt.setFloat(5, error);
					nowFlashPstmt.setString(6, location);
					nowFlashPstmt.setTimestamp(7, datetime);
					nowFlashPstmt.setInt(8, hour);
					nowFlashPstmt.setInt(9, minute);
					nowFlashPstmt.setInt(10, second);
					nowFlashPstmt.setInt(11, minisecond);
					nowFlashPstmt.setString(12, province);
					nowFlashPstmt.setString(13, district);
					nowFlashPstmt.setString(14, country);
					nowFlashPstmt.addBatch();
				}

				// insert Flash2008
				flash2008Pstmt.setFloat(1, latitude);
				flash2008Pstmt.setFloat(2, longtitude);
				flash2008Pstmt.setFloat(3, intens);
				flash2008Pstmt.setFloat(4, slope);
				flash2008Pstmt.setFloat(5, error);
				flash2008Pstmt.setString(6, location);
				flash2008Pstmt.setTimestamp(7, datetime);
				flash2008Pstmt.setInt(8, hour);
				flash2008Pstmt.setInt(9, minute);
				flash2008Pstmt.setInt(10, second);
				flash2008Pstmt.setInt(11, minisecond);
				flash2008Pstmt.setString(12, province);
				flash2008Pstmt.setString(13, district);
				flash2008Pstmt.setString(14, country);
				flash2008Pstmt.addBatch();
			}
			
            // 当flag!=3时插入NowFlashTable表
			if (!flag.equals("3")){
				// excute insert NowFlashTable
				nowFlashPstmt.executeBatch();
				System.out.println(" insert in out new !!!!!!!!");
			}
			// excute insert Flash2008
			flash2008Pstmt.executeBatch();
			db.closeConn();
		} catch (IOException e) {
			db.closeConn();
			System.out.println("error" + e.getMessage());
			logMsg = "classes:ReadTxtData.java line 260" + e.getMessage()
					+ "    " + todayDateTime;
			LogPrint.logWrite("d:\\log\\error\\error" + today + ".txt", logMsg);
		} catch (SQLException e) {
			db.closeConn();
			System.out.println("error" + e.getMessage());
			logMsg = "classes:ReadTxtData.java line 266" + e.getMessage()
					+ "    " + todayDateTime;
			LogPrint.logWrite("d:\\log\\error\\error" + today + ".txt", logMsg);
		} finally {
			// nowFlash表删除表内容
			if (flag.equals("1")) {
				System.out.println("nowFlash表删除表内容 is excute");
				String today = todayDateTime.substring(0, 10);
				deleteNowFlash(today);
			}
		}

		System.out.println("ReadTxtData inserMssql is out");
	}

	
	/**
     * 将文件拷贝到本地
	 * @throws IOException 
	 * @throws FileNotFoundException 
     * 
     * @ param fileName 源文件名称
     * @ param flag=3时 copy一天的数据
     * 
     */
    public void copyFile(String fileName, String flag) {
		System.out.println("ReadTxtDataBatch copyFile is in fileName=" + fileName);
		System.out.println("ReadTxtDataBatch copyFile is in flag=" + flag);
    	SmbFile dir;
		try {
			// flag=3时 copy一天的数据
			if (flag.equals("3")){
				dir = new SmbFile("smb://172.21.32.164/flash_data/one_day/");
			} else {
			
			// 否则copy半小时的数据
				dir = new SmbFile("smb://172.21.32.164/flash_data/half_hour/");
			}

			SmbFile files[] = dir.listFiles(new MySmbFileFilter(fileName));
			for (int i = 0; i < files.length; i++) {
				SmbFileInputStream in;
				in = new SmbFileInputStream(files[i]);
				int c = -1;
				String localFileName = files[i].getName();
			    String copyToDir;
                
			    // flag=3时 copy一天的数据 
			    if (flag.equals("3")) {
			    	copyToDir = "d:\\project\\tmp\\oneday\\";
			    } else {
			    	copyToDir = "d:\\project\\tmp\\halfhour\\";
			    }
				FileOutputStream fos = new FileOutputStream(new File(
						copyToDir + localFileName));
				while ((c = in.read()) > -1) {
					fos.write(c);
				}
				in.close();
				fos.close();
			}

			
		} catch (MalformedURLException e) {
			e.printStackTrace();
			logMsg = "classes:ReadTxtData.java copyFile()error1" + e.getMessage()
			+ "    " + todayDateTime;
	        LogPrint.logWrite("d:\\log\\error\\error" + today + ".txt", logMsg);
		} catch (SmbException e) {
			e.printStackTrace();
			logMsg = "classes:ReadTxtData.java copyFile()error2" + e.getMessage()
			+ "    " + todayDateTime;
	        LogPrint.logWrite("d:\\log\\error\\error" + today + ".txt", logMsg);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			logMsg = "classes:ReadTxtData.java copyFile()error3" + e.getMessage()
			+ "    " + todayDateTime;
	        LogPrint.logWrite("d:\\log\\error\\error" + today + ".txt", logMsg);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logMsg = "classes:ReadTxtData.java copyFile()error4" + e.getMessage()
			+ "    " + todayDateTime;
	        LogPrint.logWrite("d:\\log\\error\\error" + today + ".txt", logMsg);
		} catch (IOException e) {
			e.printStackTrace();
			logMsg = "classes:ReadTxtData.java copyFile()error5" + e.getMessage()
			+ "    " + todayDateTime;
	        LogPrint.logWrite("d:\\log\\error\\error" + today + ".txt", logMsg);
		}
		System.out.println("ReadTxtDataBatch copyFile is out");
    }
	
	
	/**
	 * 删除昨天的NowFlash表
	 */
	public void deleteNowFlash(String today) {
		System.out.println("ReadTxtData deleteNowFlash is in today = " + today);
		// 删除nowflash表中的前一天数据
		String delStrSql = "delete from nowflash_table  where CONVERT(varchar(19), datetime, 20) < '"
				+ today + "'";
		logMsg = "deleteNowFlash sql" + delStrSql + "    " + todayDateTime;
		LogPrint.logWrite("d:\\log\\" + today + ".txt", logMsg);

		MssqlDbconn db = new MssqlDbconn();
		PreparedStatement mssqlPstmt = db.DBConn(delStrSql);
		try {
			mssqlPstmt.executeUpdate();
		} catch (SQLException e) {
			logMsg = "classes:ReadTxtData.java line 302" + e.getMessage()
					+ "    " + todayDateTime;
			LogPrint.logWrite("d:\\log\\error\\error" + today + ".txt", logMsg);
			e.printStackTrace();
		} finally {
			db.closeConn();
		}
		System.out.println("ReadTxtData deleteNowFlash out");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -