今天下載了一本第四版電氣工程師手冊,非常棒的一本書,在給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);// 第三步:提取行尾數字并加70return 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;}
}
以下是縮進好的文本,非常巴適。
另外,豆包還會將編程要求進行梳理,列出表格,非常巴適。