文件行转换器
程序员文章站
2022-03-03 10:26:47
...
最近公司在做数据迁移,迁移脚本是在原有的基础上修改的.迁移完成之后,感觉脚本在格式上不是很美观,于是就在想能不能写一段程序进行格式化.恰好最近正在看<<企业集成模式>>,能否通过它的思想来完成这个事呢?
当然还可以编写其它的行转换器,只需要实现LineTransformation接口即可,再将其加入到行处理器中就可以了.
package com.app.common.util.file.transformer;
/**
* 行转换器
*
* @author Administrator
*
*/
public interface LineTransformation {
public static final String EMPTY_STRING = "";
public String transform(String line);
}
package com.app.common.util.file.transformer;
/**
* 小写转换器
* @author Administrator
*
*/
public class LowerCaseTransformation implements LineTransformation {
@Override
public String transform(String line) {
if (null == line) {
return EMPTY_STRING;
}
return line.toLowerCase();
}
}
package com.app.common.util.file.transformer;
/**
* 大写转换器
* @author Administrator
*
*/
public class UpperCaseLineTransformation implements LineTransformation {
@Override
public String transform(String line) {
if (null == line) {
return EMPTY_STRING;
}
return line.toUpperCase();
}
}
package com.app.common.util.file.transformer;
/**
* trim转换器
* @author Administrator
*
*/
public class TrimLineTransformation implements LineTransformation {
@Override
public String transform(String line) {
if (null == line) {
return EMPTY_STRING;
}
return line.trim();
}
}
package com.app.common.util.file.transformer;
import java.util.TreeMap;
/**
* 行输入适配器
* @author Administrator
*
*/
public interface LineInAdapter {
public static final TreeMap<Long, String> EMPTY_MAP = new TreeMap<Long, String>();
public TreeMap<Long, String> doExecute();
}
package com.app.common.util.file.transformer;
import java.io.*;
import java.util.TreeMap;
/**
* 文件行输入适配器
* @author Administrator
*
*/
public class FileLineInAdapter implements LineInAdapter {
private File file;
public FileLineInAdapter(File file) {
if (null == file || !file.exists() || file.isDirectory()
|| !file.canRead()) {
throw new IllegalArgumentException(
"file must exist,be a file and can read.");
}
this.file = file;
}
@Override
public TreeMap<Long, String> doExecute() {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(this.file));
} catch (FileNotFoundException e) {
}
if (null == reader) {
return EMPTY_MAP;
}
TreeMap<Long, String> result = new TreeMap<Long, String>();
Long lineNum = 1l;
String line = null;
try {
while (null != (line = reader.readLine())) {
result.put(lineNum++, line);
}
} catch (IOException e) {
} finally {
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
}
}
}
return result;
}
}
package com.app.common.util.file.transformer;
import java.util.TreeMap;
/**
* 行输出适配器
* @author Administrator
*
*/
public interface LineOutAdapter {
public void doExecute(TreeMap<Long, String> lineMap);
}
package com.app.common.util.file.transformer;
import java.io.*;
import java.util.*;
/**
* 文件行输出适配器
* @author Administrator
*
*/
public class FileLineOutAdapter implements LineOutAdapter {
public static final String FILE_EXTENSION = ".";
private File file;
public FileLineOutAdapter(File file) {
if (null == file) {
throw new IllegalArgumentException("file can't be null.");
}
String filePath = null;
try {
filePath = file.getCanonicalPath();
} catch (IOException e) {
}
if (null == filePath) {
throw new IllegalArgumentException("can't get CanonicalPath.");
}
if (filePath.contains(FILE_EXTENSION) == false) {
throw new IllegalArgumentException("file must be a file.");
}
this.file = file;
}
@Override
public void doExecute(TreeMap<Long, String> lineMap) {
if (null == lineMap || lineMap.isEmpty()) {
return;
}
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(this.file));
} catch (IOException e) {
e.printStackTrace();
}
if (null == writer) {
return;
}
Collection<String> lines = lineMap.values();
try {
for (String line : lines) {
writer.write(line);
writer.newLine();
}
writer.flush();
} catch (IOException e) {
} finally {
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
}
}
}
}
}
package com.app.common.util.file.transformer;
import java.util.List;
import java.util.TreeMap;
/**
* 行处理器
*
* @author Administrator
*
*/
public interface LineProcesser {
public void setLineTransformations(List<LineTransformation> transformations);
public TreeMap<Long, String> process(TreeMap<Long, String> lineMap);
}
package com.app.common.util.file.transformer;
import java.util.*;
import java.util.Map.Entry;
/**
* 默认行处理器
* @author Administrator
*
*/
public class DefLineProcesser implements LineProcesser {
private List<LineTransformation> transformations;
public DefLineProcesser() {
}
public DefLineProcesser(List<LineTransformation> transformations) {
this.setLineTransformations(transformations);
}
@Override
public void setLineTransformations(List<LineTransformation> transformations) {
this.transformations = transformations;
}
@Override
public TreeMap<Long, String> process(TreeMap<Long, String> lineMap) {
if (null == this.transformations || this.transformations.size() == 0) {
throw new IllegalArgumentException(
"please set transformations first.");
}
if (null == lineMap || lineMap.isEmpty()) {
return new TreeMap<Long, String>();
}
TreeMap<Long, String> result = new TreeMap<Long, String>();
Set<Entry<Long, String>> entries = lineMap.entrySet();
for (Entry<Long, String> entry : entries) {
String line = entry.getValue();
for (LineTransformation transformation : this.transformations) {
line = transformation.transform(line);
}
result.put(entry.getKey(), line);
}
return result;
}
}
package com.app.common.util.file.transformer;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
/**
* 测试类
* @author Administrator
*
*/
public class TestLineProcesser {
/**
* @param args
*/
public static void main(String[] args) {
File inputFile = new File("c:/test/a.txt");
File outFile = new File("c:/a.txt");
LineProcesser processer = new DefLineProcesser();
List<LineTransformation> transformations = new ArrayList<LineTransformation>();
transformations.add(new TrimLineTransformation());
transformations.add(new UpperCaseLineTransformation());
processer.setLineTransformations(transformations);
LineInAdapter inAdapter = new FileLineInAdapter(inputFile);
TreeMap<Long, String> lineMap = processer
.process(inAdapter.doExecute());
LineOutAdapter outAdapter = new FileLineOutAdapter(outFile);
outAdapter.doExecute(lineMap);
System.out.println("finished...");
}
}
当然还可以编写其它的行转换器,只需要实现LineTransformation接口即可,再将其加入到行处理器中就可以了.
下一篇: JAVA XML文件转MAP