/*
* @(#) ReadTxtFile.java 2012-12-27
* Copyright 2012 xian huilong technology co.,ltd. All rights reserved.
* HUILONG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* 新代码
* 读取TXT文件,包括文件头和文件内容,文件头保存到数据库中的一个表中,文件内容保存到对应的表中,然后再把
* 所有的表名保存到一个表中。
*/
public class ReadTxtFile
{
private void readtxthead() throws IOException
{
/**
* 读取文件头
*/
try
{
WriteFile write = new WriteFile();
String sqlInsert2 = "";
String sqlInsert1 = "";
// 有用的代码,以下代码为读取文件头几行标题
BufferedReader br1 = new BufferedReader(
new FileReader(
"D:\\HWTD_DATA\\CM\\CFGMML-RNC1701-20130201101646.txt"));
String str = "";
String str1 = "";
String str2 = "";
String str5 = "";
String r1 = "";
String m10 = "";
String m5 = "";
// 表CFGMML_INFO中保存文件的文件头。
String table = "CFGMML_INFO";
int count = 0;
br1.readLine();
count = 1;
// 从文件的第二行开始读文件的头,因为第二行比较特殊。
while ((r1 = br1.readLine()) != null && count == 1)
{
str = r1.substring(2, r1.length());
count += 1;
str1 = str.substring(0, 17);
// str2,数据库INFO表中的列名Export_start_time(ExportTime)对应的值,2012-11-06
// 16:25:41
str2 = str.substring(18, 38);
String str3[] = str1.split(" ");
String m1 = str3[0];
String m2 = str3[1];
String m3 = str3[2];
// str3[0],数据库INFO表中的列名,Export_start_time
str3[0] = m1 + "_" + m2 + "_" + m3;
m10 = str3[0].toString() + ",";
m5 = "'" + str2 + "'" + ",";
}
count = 2;
// 从文件的第三行开始读文件的头,因为第三行以后直到第六行都不特殊。
String str8 = "";
String SRNCID = "";
while ((r1) != null && (count <= 5))
{
if (count == 3)
{
str8 = r1.substring(2, r1.length());
String str9[] = str8.split(":");
SRNCID = str9[1];
}
str5 = r1.substring(2, r1.length());
String str6[] = str5.split(":");
String m4 = str6[0];
// m5,数据库INFO表中的列名对应的值
m5 += "'" + str6[1] + "'" + ",";
String str3[] = m4.split(" ");
String m6 = str3[0];
String m7 = str3[1];
// str3[0],数据库INFO表中的列名
str3[0] = m6 + "_" + m7;
if (str3.length == 4)
{
String m8 = str3[2];
str3[0] += "_" + m8;
String m9 = str3[3];
str3[0] += "_" + m9;
}
if (str3.length == 3)
{
String m8 = str3[2];
str3[0] += "_" + m8;
}
count += 1;
r1 = br1.readLine();
m10 += str3[0] + ",";
}
m5 = m5.substring(0, m5.length() - 1);
m10 = m10.substring(0, m10.length() - 1);
// 插入到数据库中开始
// String sqlInsert = "insert into " + tableTitle + " (" + m10
// + ") values(";
sqlInsert1 = "insert into " + table + " values(";
sqlInsert1 = sqlInsert1 + m5 + ")";
// 插入到数据库中结束
br1.close();
/**
* 读取文件内容
*/
BufferedReader br = new BufferedReader(
new FileReader(
"D:\\HWTD_DATA\\CM\\CFGMML-RNC1701-20130201101646.txt"));
String str6 = "";
String r = "";
String tableTitle = "";
for (int i = 0; i <= 6; i++)
{
br.readLine();
}
int coun = 0;
r = br.readLine();
while (r != null)
{
String m7 = "";
String m12 = "";
String m6 = "";
String m8 = "";
String m9 = "";
String m13 = "";
String m11 = "";
coun += 1;
System.out.println("正在处理第" + coun + "行");
str6 = r;
String arr[] = str6.split(":");
String m1 = arr[0];
String m2 = arr[1];
if (m1.contains(" "))
{
String arr1[] = m1.split(" ");
String m3 = arr1[0];
String m4 = arr1[1];
// arr1[0],数据库中的表名
arr1[0] = m3 + "_" + m4;
tableTitle = arr1[0];
// 把读取到的当前表名放到table数组中。
}
String arr2[] = m2.split(",");
for (int i = 0; i < arr2.length; i++)
{
String arr3[] = arr2[i].split("=");
// m8,数据库中对应表的列,column
m12 += arr3[0] + ",";
m8 = m12.substring(0, m12.length() - 1);
// 替换"'",因为对数据库插入语句有影响。
if (arr3[1].contains("'"))
{
String a = "NSAP";
try{
if(!a.equals(arr3[0].trim())){
if(arr3[1].contains(";")){
arr3[1] = arr3[1].substring(2, arr3[1].length()-1);
System.out.println(arr3[1]);
int v = Integer.valueOf(arr3[1], 16);
//arr3[1] = v + "";
arr3[1] = v +";";
}
else{
arr3[1] = arr3[1].substring(2, arr3[1].length());
System.out.println(arr3[1]);
int v = Integer.valueOf(arr3[1], 16);
arr3[1] = v + "";
}
//arr3[1] = arr3[1].substring(0, arr3[1].length()-1);
}
else{
arr3[1] = arr3[1].replace("'", "-");
//arr3[1] = arr3[1].substring(1, arr3[1].length()-1);
}
}catch (Exception e) {
e.printStackTrace();
write.appendMethod("E:/test1.txt", e.getMessage() + "\n");
}
}
// m6,数据库中对应列的值,values
m6 += "'" + arr3[1] + "'" + ",";
if (m6.contains(";"))
{
m7 = m6.substring(0, m6.length() - 1);
}
else
{
m7 = m6;
}
if (m7.contains(";"))
{
m9 = m7.substring(0, m7.length() - 2);
m13 = m7.substring(m7.length() - 1, m7.length());
m11 = m9 + m13;
}
else
{
m11 = m7;
}
}
// 插入到数据库中开始
String sqlInsert = "insert into " + tableTitle + " ("
+ "ExportTime" + "," + "SRNCID" + "," + m8
+ ") values(" + "'" + str2 + "'" + "," + "'" + SRNCID
+ "'" + ",";
sqlInsert = sqlInsert + m11 + ")";
// statement.addBatch(sqlInsert);
System.out.println(sqlInsert);
write.appendMethod("E:/test.txt", sqlInsert + "\n");
String sqldelete ="delete from " + tableTitle;
write.appendMethod("E:/test3.txt", sqldelete+"\n");
// 表TABLE_INFO保存ExportTime对应的时间的SRNCID和导入的表名。
sqlInsert2 = "insert into TABLE_INFO" + " (" + "ExportTime"
+ "," + "SRNCID" + "," + "TABLE_NAME" + ") values("
+ "'" + str2 + "'" + "," + "'" + SRNCID + "'" + ","
+ "'" + tableTitle + "'" + ")";
// statement.addBatch(sqlInsert);
System.out.println(sqlInsert2);
//write.appendMethod("E:/test2.txt", sqlInsert2 + "\n");
// 插入到数据库中结束
// 测试写文件,既把sqlInsert1写入文件中看结果
r = br.readLine();
}
System.out.println(sqlInsert1);
//write.appendMethod("E:/test1.txt", sqlInsert1 + "\n");
System.out.println("一共处理了" + coun + "行");
br.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String arg[])
{
try
{
ReadTxtFile rt = new ReadTxtFile();
rt.readtxthead();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}