Jar used:
Itextpdf-5.5.8.jar (PDF)
Poi.jar
public class FileUtils {/** * Determines if the file exists * * @Title: Isexcite * @param @param filePath * @param @return * @return Boolean return Back type * @throws */public static Boolean isexcite (String filePath) {File File = new file (filePath);//If the folder does not exist, create if (!file.ex Ists () &&!file.isdirectory ()) {return false;} else {return true;}} /** * * @Title: Getpdffiletext * @Description: Gets the file contents of the PDF in the specified location * @param @param fileName * @param @return * @param @throws IOException * @return String return type * @throws */public static string Getpdffiletext (String fileName) throws IOException {PDF Reader reader = new Pdfreader (fileName); Pdfreadercontentparser parser = new Pdfreadercontentparser (reader); StringBuffer buff = new StringBuffer (); Textextractionstrategy strategy;for (int i = 1; I <= reader.getnumberofpages (); i++) {strategy = Parser.processcontent ( I,new simpletextextractionstrategy ()); Buff.append (Strategy.getresultanttext ());} return buff.tostring ();} /** * Get Doc Documents * * @Title: Gettextfromword * @param @param FilepaTH * @param @return * @return string return type * @throws */public static string Gettextfromword (String filePath) {string result = NULL; File File = new file (FilePath); try {fileinputstream fis = new FileInputStream (file); Wordextractor wordextractor = new Wordextractor (FIS); result = Wordextractor.gettext ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return result;} /** * Read Excel content * * @Title: Gettextfromexcel * @param @param filePath * @param @return * @return String return type * @throws * * public static string Gettextfromexcel (String filePath) {StringBuffer buff = new StringBuffer (); try {//Create reference to Excel workbook file hs Sfworkbook wb = new Hssfworkbook (new FileInputStream (FilePath));//Create a reference to the worksheet. for (int numsheets = 0; numsheets < wb.getnumberofsheets (); numsheets++) {if (Null! = Wb.getsheetat (numsheets)) {Hssfsh Asheet = Wb.getsheetat (numsheets);//Get a sheetfor (int rownumofsheet = 0; Rownumofsheet <= asheet.getlastrownum (); rownumofsheet++) {if (Null! = Asheet.getrow (Rownumofsheet)) {Hssfrow Arow = Asheet.getrow (Rownumofsheet);//Get a line for (int cellnumofrow = 0; Cellnumofrow <= arow.getlastcellnum (); cellnumofrow++) {if (Null! = Arow.getcell (Cellnumofrow)) {Hssfcell Acell = Arow.getcell (Cellnumofrow);//Get column value switch (aCe Ll.getcelltype ()) {Case Hssfcell.cell_type_formula:break;case HSSFCell.CELL_TYPE_NUMERIC:buff.append ( Acell.getnumericcellvalue ()). Append (' \ t '); Break;case HSSFCell.CELL_TYPE_STRING:buff.append ( Acell.getstringcellvalue ()). Append (' \ t '); break;}}} Buff.append (' \ n ');}}}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return buff.tostring ();} /** * Replace the contents of the file * @Title: Replacecontenttofile * @param @param path file paths * @param @param str to replace * @param @param con Replace the contents of the name * @return void return type * @throws */public static void Replacecontenttofile (string path, String str, string con) {try { if (isexcite (path)) {FileReader read = new FileReader (path); BufferedReader br = newBufferedReader (read); StringBuilder content = new StringBuilder (); while (Br.ready ()! = False) {Content.append (Br.readline ()); Content.append ( "\ r \ n");} int dex = Content.indexof (str), if (dex! =-1) {System.out.println ("Find tag!");} else {System.out.println ("The specified tag does not exist!");} Content.replace (Dex, Dex, con); Br.close (); Read.close (); FileOutputStream fs = new FileOutputStream (path), Fs.write (Content.tostring (). GetBytes ()); Fs.close (); else {System.out.println ("file does not exist!");}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}}
Save it and take it directly.
Java reads PDF, Word, Excel files