Java reads PDF, Word, Excel files
Jar used:
Itextpdf-5.5.8.jar (PDF)
Poi.jar
public class FileUtils {
/**
* Determine if the file exists
*
* @Title: Isexcite
* @param @param FilePath
* @param @return
* @return Boolean return type
* @throws
*/
public static Boolean isexcite (String filePath) {
File File = new file (FilePath);
If the folder does not exist, create
if (!file.exists () &&!file.isdirectory ()) {
return false;
} else {
return true;
}
}
/**
*
* @Title: Getpdffiletext
* @Description: Gets the file contents of the PDF at the specified location
* @param @param fileName
* @param @return
* @param @throws IOException
* @return String return type
* @throws
*/
public static string Getpdffiletext (String fileName) throws IOException {
Pdfreader 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 Documentation
*
* @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 a reference to an Excel workbook file
Hssfworkbook wb = new Hssfworkbook (new FileInputStream (FilePath));
Creates a reference to the worksheet.
for (int numsheets = 0; numsheets < wb.getnumberofsheets (); numsheets++) {
if (null! = Wb.getsheetat (numsheets)) {
Hssfsheet Asheet = Wb.getsheetat (numsheets);//Get a sheet
for (int rownumofsheet = 0; Rownumofsheet <= asheet
. Getlastrownum (); rownumofsheet++) {
if (null! = Asheet.getrow (Rownumofsheet)) {
Hssfrow Arow = Asheet.getrow (Rownumofsheet); Get a row
for (int cellnumofrow = 0; Cellnumofrow <= arow
. Getlastcellnum (); cellnumofrow++) {
if (null! = Arow.getcell (Cellnumofrow)) {
Hssfcell Acell = Arow.getcell (Cellnumofrow);//Get column values
Switch (Acell.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 file contents
* @Title: Replacecontenttofile
* @param path @param path file
* @param @param str to replace the content
* @param @param con replace the content
* @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 = new BufferedReader (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.
Methods for working with files such as Java Excel,pdf,word