梯度下降法原理与线性回归应用解析
2025/12/26 17:02:52
importcom.spire.doc.Document;importcom.spire.doc.FileFormat;importcom.spire.pdf.PdfDocument;importjava.io.*;public class DesktopConverter{public static void main(String[]args){//1. 配置路径 String sourceFolderPath="C:\\Users\\qinqin\\Desktop";String outputFolderPath="C:\\Users\\qinqin\\Desktop\\ofd_output";//2. 创建输出目录 File outputDir=new File(outputFolderPath);if(!outputDir.exists()){outputDir.mkdirs();System.out.println("已创建输出目录: "+ outputFolderPath);}//3. 扫描文件 File sourceDir=new File(sourceFolderPath);File[]files=sourceDir.listFiles();if(files==null||files.length==0){System.out.println("目录下没有文件!");return;}System.out.println("=== 开始批量转换 ===");for(Filefile:files){if(file.isDirectory())continue;// 跳过文件夹 String fileName=file.getName();String lowerName=fileName.toLowerCase();String srcPath=file.getAbsolutePath();// 生成输出文件名(例如:1.doc ->1_doc.ofd)String outName=fileName.replace(".","_")+".ofd";String destPath=outputFolderPath + File.separator + outName;try{if(lowerName.endsWith(".doc")||lowerName.endsWith(".docx")){System.out.print("正在转换 Word: "+ fileName +" ... ");convertWordToOfd(srcPath, destPath);System.out.println("✅ 成功");}elseif(lowerName.endsWith(".txt")){System.out.print("正在转换 Txt: "+ fileName +" ... ");convertTxtToOfd(srcPath, destPath);System.out.println("✅ 成功");}elseif(lowerName.endsWith(".pdf")){System.out.print("正在转换 PDF: "+ fileName +" ... ");convertPdfToOfd(srcPath, destPath);System.out.println("✅ 成功");}}catch(Exception e){System.out.println("❌ 失败 ("+ e.getMessage()+")");e.printStackTrace();}}System.out.println("=== 全部完成,请查看文件夹: "+ outputFolderPath +" ===");}// --- 核心转换方法 --- /** * Word/Docx 转 OFD */ public static void convertWordToOfd(String srcPath, String destPath)throws Exception{Document doc=new Document();doc.loadFromFile(srcPath);// Word 转 PDF 流 ByteArrayOutputStream pdfStream=new ByteArrayOutputStream();doc.saveToStream(pdfStream, FileFormat.PDF);// PDF 流转 OFD streamToOfd(new ByteArrayInputStream(pdfStream.toByteArray()), destPath);}/** * Txt 转 OFD */ public static void convertTxtToOfd(String srcPath, String destPath)throws Exception{Document doc=new Document();// 显式指定加载格式为 Text doc.loadFromFile(srcPath, FileFormat.Txt);ByteArrayOutputStream pdfStream=new ByteArrayOutputStream();doc.saveToStream(pdfStream, FileFormat.PDF);streamToOfd(new ByteArrayInputStream(pdfStream.toByteArray()), destPath);}/** * PDF 转 OFD */ public static void convertPdfToOfd(String srcPath, String destPath){PdfDocument pdf=new PdfDocument();pdf.loadFromFile(srcPath);pdf.saveToFile(destPath, com.spire.pdf.FileFormat.OFD);pdf.close();}/** * 通用:内存PDF流 ->OFD文件 */ private static void streamToOfd(InputStream inputStream, String destPath){PdfDocument pdf=new PdfDocument();pdf.loadFromStream(inputStream);pdf.saveToFile(destPath, com.spire.pdf.FileFormat.OFD);pdf.close();}}