首先,用java读取文件和写入文件都其实很简单,但重点是修改文件。
修改可以为替换、删除、新增字符。所以不能用inputstream那种字节流的方式,以下例子使用BufferedReader的方式来读取每行的字符数据。
完成一个Util java文件如下:
主要要用到common-io.jar 包,其中有一些正则表达式规则来做替换工作。
package com.zjhc.hcdream.util;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Gerrard on 2016-9-14.
*/
public class TransformUtil {
public static void main(final String[] args) throws Exception {
String inFile = "D:/3XDATA/test/"; // 输入文件路径
String outFile = "D:/3XDATA/out/"; // 输出文件路径
String paths="/user/hive/ggzyjy/";
String dbo="ggzyjy";
File file=new File(inFile);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
tranformMethod(tempList[i].toString(),outFile,paths,dbo);
}
}
}
public static void tranformMethod(String inFile,String outFile,String paths,String dbo) throws IOException {
String fileName="";
BufferedReader bs = null;
BufferedWriter bw = null;
try{
bs = new BufferedReader(new FileReader(new File(inFile)));
List<String> outData = new ArrayList<String>();
String line = null;
String[] data = null;
//如果确定ID1一直是整数的活,可以用整数比,否则可以用字符串的equals比较
int oldId1 = 0;
int newId1 = 0;
int oldId2 = -1;
int newId2 = -1;
int num=0;
int count=0;
while((line = bs.readLine()) != null){
String lowLine=line.toLowerCase();
// 把所有[]都去掉
lowLine=lowLine.replace("[","");
lowLine=lowLine.replace("]","");
if(num==1){ // 字符修改处
// 将Table换成
if(lowLine.contains("table")){
lowLine=lowLine.replace("table","external table");
lowLine=lowLine.replace("dbo","ggzyjy");
// 获取表明
Pattern pattern = Pattern.compile("ggzyjy.(.*)\\($");
Matcher matcher = pattern.matcher(lowLine);
while(matcher.find()){
fileName=matcher.group(1);
fileName=fileName.trim();
}
}
if(count==1){// 加固操作,只改()里面的
// 替换字符类型
boolean flag=false;
String[] regex = {"nvarchar.*,$","datetime.*,$","bit.*,$","varchar.*,$","timestamp .*,$",
"date.*,$","ntext.*,$","int.*,$","bigint.*,$","float.*,$","double.*,$","numeric.*,$","nvarchar.*\\s+$","datetime.*\\s+$","bit.*\\s+$","varchar.*\\s+$","timestamp .*\\s+$",
"date.*\\s+$","ntext.*\\s+$","int.*\\s+$","bigint.*\\s+$","float.*\\s+$","double.*\\s+$","numeric.*\\s+$"};// 设置最后一行不以逗号结尾的行
String[] regex1={"nvarchar.*","datetime.*","bit.*","varchar.*","timestamp .*",
"date.*","ntext.*","int.*","bigint.*","float.*","double.*","numeric.*\\s+$"};
for(int i=0;i<regex.length;i++){
Pattern pattern = Pattern.compile(regex[i]);
Matcher matcher = pattern.matcher(lowLine);
if(regex[i].startsWith("int") && regex[i].endsWith(",$")){
lowLine = matcher.replaceAll("int ,");
}else if(regex[i].startsWith("bigint") && regex[i].endsWith(",$")){
lowLine = matcher.replaceAll("bigint ,");
}else if(regex[i].startsWith("float") && regex[i].endsWith(",$")){
lowLine = matcher.replaceAll("float ,");
}else if(regex[i].startsWith("double") && regex[i].endsWith(",$")){
lowLine = matcher.replaceAll("double ,");
}else if(regex[i].startsWith("numeric") && regex[i].endsWith(",$")){
lowLine = matcher.replaceAll("double ,");
}else if(regex[i].startsWith("int") && regex[i].endsWith("+$")){
lowLine = matcher.replaceAll("int");
}else if(regex[i].startsWith("bigint") && regex[i].endsWith("+$")){
lowLine = matcher.replaceAll("bigint");
}else if(regex[i].startsWith("float") && regex[i].endsWith("+$")){
lowLine = matcher.replaceAll("float");
}else if(regex[i].startsWith("double") && regex[i].endsWith("+$")){
lowLine = matcher.replaceAll("double");
}else if(regex[i].startsWith("numeric") && regex[i].endsWith("+$")){
lowLine = matcher.replaceAll("double");
}else{
if(regex[i].endsWith("+$")){
lowLine = matcher.replaceAll("string ");
}
if(regex[i].endsWith(",$")){
lowLine = matcher.replaceAll("string ,");
}
}
}
}
if(count==2){
String str="row format delimited\n" +
"fields terminated by '\\t' \n" +
"STORED AS TEXTFILE \n" +
"location '"+paths+fileName+"' ;";
outData.add(str);
count++;
}
if(lowLine.endsWith("(") || lowLine.endsWith(")")){
count++;
}
if(!lowLine.equals("go")){
outData.add(lowLine);
}
}
if(lowLine.equals("go")){
num++;
}
}
// 在最后要新增的行数据
if(!outData.isEmpty()){
bw = new BufferedWriter(new FileWriter(new File(outFile+fileName+".sql")));
for(String s : outData){
bw.write(s + "\r\n");
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(bs != null){
bs.close();
}
if(bw != null){
bw.close();
}
}
}
}