0%

java读取文件更改并生成另一个文件



写在前面:

如本文描述有错误,希望读到这篇文章的您能够提出批评指正。 联系方式:172310978@qq.com

首先,用java读取文件和写入文件都其实很简单,但重点是修改文件。
修改可以为替换、删除、新增字符。所以不能用inputstream那种字节流的方式,以下例子使用BufferedReader的方式来读取每行的字符数据。
完成一个Util java文件如下:
主要要用到common-io.jar 包,其中有一些正则表达式规则来做替换工作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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();
}
}
}
}

本文转自 https://blog.csdn.net/laijieyao/article/details/52540320,如有侵权,请联系删除。