今天下载了一本第四版电气工程师手册,非常棒的一本书,在给PDF添加目录的时候,由于目录有将近60页,使用老马开发的PdgCntEditor有点卡顿,不过补充下,老马这个PdgCntEditor还是非常好的。
所以我决定用Java编一个小程序来对目录文件进行缩进处理,然后再导入到PdgCntEditor中进行保存。
之前还没试过用AI编写程序,就采用豆包进行了一个程序测试。
输入指令如下:
作为一名java程序员,请你设计一段java程序,逐行读入文本,若读入的行的行首包含制表符,则去除行首制表符,然后当该行的开头字符为数字,且前三个字符未包含符号“.”,则在行首增加两个制表符,若包含有符号“.”,则在行首增加一个制表符。并将文本进行保存。
豆包自动生成了一个程序,结果输入的文本是中文,输出的是乱码。豆包又增加了通过指定StandardCharsets.UTF_8,结果
FileReader没有 new FileReader(String, StandardCharsets.UTF_8))方法,提交该问题后,豆包又进行了优化。
优化后的程序非常好用,而且编写规范。以下是豆包写的程序:
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TextProcessor {
// 匹配行尾的数字(整数或小数)
private static final Pattern NUMBER_PATTERN = Pattern.compile("(\\d+(\\.\\d+)?)\\s*$");
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// System.out.print("请输入源文件路径:");
// String sourcePath = scanner.nextLine();
//
// System.out.print("请输入目标文件路径:");
// String targetPath = scanner.nextLine();
String sourcePath = "E:\\input.txt";
String targetPath = "E:\\output.txt";
try {
processTextFile(sourcePath, targetPath);
System.out.println("文件处理完成,已保存至:" + targetPath);
} catch (IOException e) {
System.err.println("文件处理出错:" + e.getMessage());
} finally {
scanner.close();
}
}
private static void processTextFile(String sourcePath, String targetPath) throws IOException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream(sourcePath), StandardCharsets.UTF_8));
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(targetPath), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
String processedLine = processSingleLine(line);
writer.write(processedLine);
writer.newLine();
}
}
}
private static String processSingleLine(String line) {
if (line.isEmpty()) {
return line;
}
// 第一步:去除行首所有制表符
String lineWithoutLeadingTabs = line.replaceAll("^\\t+", "");
if (lineWithoutLeadingTabs.isEmpty()) {
return lineWithoutLeadingTabs;
}
// 第二步:处理行首数字逻辑(添加制表符)
String lineWithTabs = addLeadingTabs(lineWithoutLeadingTabs);
// 第三步:提取行尾数字并加70
return processTrailingNumber(lineWithTabs);
}
/**
* 根据行首数字和"."的存在情况添加制表符
*/
private static String addLeadingTabs(String line) {
char firstChar = line.charAt(0);
if (Character.isDigit(firstChar)) {
int endIndex = Math.min(3, line.length());
String firstThreeChars = line.substring(0, endIndex);
if (firstThreeChars.contains(".")) {
return "\t" + line; // 包含".",添加1个制表符
} else {
return "\t\t" + line; // 不包含".",添加2个制表符
}
}
return line;
}
/**
* 提取行尾的数字部分并加70
*/
private static String processTrailingNumber(String line) {
Matcher matcher = NUMBER_PATTERN.matcher(line);
if (matcher.find()) {
String numberStr = matcher.group(1);
try {
// 解析为double处理整数和小数
double number = Double.parseDouble(numberStr);
double newNumber = number + 68;
// 格式化结果:整数保持整数形式,小数保留两位
String formattedNumber = (newNumber == Math.floor(newNumber))
? String.format("%.0f", newNumber)
: String.format("%.2f", newNumber);
// 替换行尾的数字部分
return line.substring(0, matcher.start()) + formattedNumber;
} catch (NumberFormatException e) {
// 解析失败时返回原始行(理论上不会发生,因为正则已匹配有效数字)
return line;
}
}
// 未找到行尾数字,直接返回原始行
return line;
}
}
以下是缩进好的文本,非常巴适。
另外,豆包还会将编程要求进行梳理,列出表格,非常巴适。