前提,整理excel文本内容到word中
整理前
整理后
写了一个java类对不规则的文本进行处理
public class ReadTxt {
public static void main(String[] args) {
readAndWriteFile_simple("D:\\IDEA-workspace\\MyJavaApplication\\MyJavaApplication\\txt\\work.txt",
"D:\\IDEA-workspace\\MyJavaApplication\\MyJavaApplication\\txt\\work_output.txt"
);
readAndWriteFile("D:\\IDEA-workspace\\MyJavaApplication\\MyJavaApplication\\txt\\work1.txt",
"D:\\IDEA-workspace\\MyJavaApplication\\MyJavaApplication\\txt\\work1_output.txt"
);
readAndWriteFile("D:\\IDEA-workspace\\MyJavaApplication\\MyJavaApplication\\txt\\work2.txt",
"D:\\IDEA-workspace\\MyJavaApplication\\MyJavaApplication\\txt\\work2_output.txt"
);
}
public static void readAndWriteFile_simple(String inputFilePath, String outputFilePath) {
try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
String line;
StringBuilder content = new StringBuilder();
while ((line = reader.readLine()) != null) {
// 移除行尾的换行符,如果有的话
line = line.trim();
// 如果该行非空,则添加到内容中
if (!line.isEmpty()) {
content.append(line);
content.append("、"); // 在每个文本行之后添加“、”
}
}
// 去除最后一个多余的“、”
if (content.length() > 0) {
content.setLength(content.length() - 1);
}
// 将处理后的内容写入输出文件
writer.write(content.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readAndWriteFile(String inputFilePath, String outputFilePath) {
try (BufferedReader br = new BufferedReader(new FileReader(inputFilePath));
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFilePath))) {
String line;
int count = 1;
Set<String> uniqueLines = new HashSet<>(); // 用于存储唯一行
while ((line = br.readLine()) != null) {
// 去掉空行
if (line.trim().isEmpty()) {
continue;
}
if ("\"".equals(line)) {
continue;
}
// 检查行是否以数字开头,如果不是则重新编号
if (!Character.isDigit(line.charAt(0))) {
line = java.lang.String.format("%d、%s", count, line.substring(line.indexOf("、") + 1));
} else {
// 提取行号并检查是否正确,如果不正确则重新编号\
try {
int lineNumber = Integer.parseInt(line.substring(0, line.indexOf("、")));
if (lineNumber != count) {
line = java.lang.String.format("%d、%s", count, line.substring(line.indexOf("、") + 1));
}
} catch (NumberFormatException e) {
line = java.lang.String.format("%d、%s", count, line.substring(line.indexOf("、") + 1));
}
}
// 去掉重复行
if (!uniqueLines.contains(line)) {
bw.write(line);
bw.newLine(); // 写入新的一行
uniqueLines.add(line); // 将当前行添加到集合中
}
count++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}