说明:在1.0基础上增加对每行的继续处理
package com.zj.commons.fileutils;
import java.io.*;
public class FileUtils {
private static final String fileSourcePath = "输入路径";
private static final String fileOutPath = "输出路径";
public static void processFile(String fileSourcePath, String separator, String subStringChar, String fileOutPath) throws IOException {
String result = "";
FileReader fr = new FileReader(new File(fileSourcePath));
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
if ("".equalsIgnoreCase(line.trim())) {
line = br.readLine();
continue;
}
result += processWithSeparator(line, separator, subStringChar);
line = br.readLine();
}
FileWriter fw = new FileWriter(new File(fileOutPath));
fw.write(result);
fw.flush();
fw.close();
}
private static String processWithSeparator(String line, String separator, String subStringChar) {
line = line.trim();
String[] sp = line.split(separator);
String res = "";
for (String item : sp) {
res += processWithSubString(item.trim(), subStringChar) + "\n";
}
return res;
}
private static String processWithSubString(String trim, String subStringChar) {
if ("".equalsIgnoreCase(subStringChar.trim())) {
return trim.trim();
} else {
return trim.trim().substring(trim.trim().indexOf(subStringChar) + 1);
}
}
public static void main(String[] args) throws IOException {
processFile(fileSourcePath, ";", "、", fileOutPath);
}
}
效果 处理前: 处理后:
|