为什么 PDF 文件需要 LOG4J 和 SLF4J?为什么不需要 .Doc 文件?
why PDF Files needed LOG4J & SLF4J ? why not required for .Doc files?
我正在开发一个简单的应用程序,它提取文本并将其放入 excel,使用 PDFBox api 获取 PDF 文档,使用 POIFSFilesystem (HSSFWorkbook) 获取 excel 文件。最近我开发了一个从 .doc 文件中提取文本并放入 excel 的应用程序,那时我从来没有遇到过 LOGGER 问题。这次系统抛出几个错误 [How to find specific org/slf4j/Logger jar file out of multiple bindings from the apache zip?
我红色 Apache logging it says configure logging. i am not developing any web related functions in my application. Adding jar files are not enough? I red https://www.slf4j.org/codes.html 这篇错误处理文章 我从未找到与应用程序相关的特定错误。
log4j:WARN No appenders could be found for logger (org.apache.pdfbox.io.ScratchFileBuffer).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
您可以在下面找到我的代码,其中包含 POI API 和 PDFBOX API。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FIRST {
//private static WordExtractor we;
//static InputStream inc;
static PDDocument pdDoc = null;
public static void main(String[] args) throws IOException {
POIFSFileSystem fs = null;
// String target_dir = "E:\TESTTRS";
//File dir = new File(target_dir);
// File[] files = dir.listFiles();
String target_dir = "C:\Users";
File dir = new File(target_dir);
File[] files = dir.listFiles();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("firstsheet");
Row row0 = sheet.createRow(0);
row0.createCell(0).setCellValue("S.NO");
row0.createCell(1).setCellValue("DOCUMENT");
row0.createCell(2).setCellValue("VALUE1");
row0.createCell(3).setCellValue("VALUE2");
row0.createCell(4).setCellValue("TEND");
int j = 1;
for ( int s=0;s<files.length;s++){
if(files[s].isFile()){
pdDoc = PDDocument.load(files[s]);
//fs = new POIFSFileSystem(new FileInputStream(files[s]));
PDFTextStripper Stripper = new PDFTextStripper();
String st = Stripper.getText(pdDoc);
String linesp = System.lineSeparator();
String[] paragraph = st.split(linesp);
//HWPFDocument doc = new HWPFDocument(fs);
//we = new WordExtractor(doc);
//String[] paragraph= we.getParagraphText();
Row row1 = sheet.createRow(j);
/***************************1_PRINTS S.NO *************************************/
Cell cell_10 =row1.createCell(0);
cell_10.setCellValue(j);
j++;
/***************************2_PRINTS FILE NAMES *********************************/
Cell cell_11 = row1.createCell(1);
cell_11.setCellValue(files[s].getName());
/******************************3_PRINTS VALUE1*****************************************/
Cell cell_12 = row1.createCell(2);
String len = files[s].getName().substring(13, 19);
cell_12.setCellValue(len);
/**********************4_PRINTS VALUE2 *******************************/
Cell cell_13 = row1.createCell(3);
for(String p: paragraph){
if(p.startsWith("VALUE2"))
cell_13.setCellValue(p.substring(22));
}
/*******************5_PRINTS TEND*****************************************/
Cell cell_14 = row1.createCell(4);
for(String pp: paragraph){
if(pp.contains("TEND"))
cell_14.setCellValue(pp);
}
/**************6_TEST PATTERNS*********************************************/
Cell cell_15 = row1.createCell(5);
for(String c : paragraph){
final String regex = ("^.*([0-9]{6}\/[A-Z][0-9]{5}).*$");
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(c);
for (int i = 1; i < matcher.groupCount(); i++) {
if(c.startsWith("COMMENT:"))
cell_15.setCellValue(""+matcher.group(i));
}
}
workbook.write(new FileOutputStream("C:\Users\abc.xls"));
workbook.close();
}
pdDoc.close();
}}}
我通过将我项目的相关 mvnrepository 构建到 pom.xml 中来解决它。并使用适当的根记录器和 log4j 附加程序在 "src" 中配置 log4j.properties。如果我们想使用 PDFBox API 解析 PDF 文档。
我正在开发一个简单的应用程序,它提取文本并将其放入 excel,使用 PDFBox api 获取 PDF 文档,使用 POIFSFilesystem (HSSFWorkbook) 获取 excel 文件。最近我开发了一个从 .doc 文件中提取文本并放入 excel 的应用程序,那时我从来没有遇到过 LOGGER 问题。这次系统抛出几个错误 [How to find specific org/slf4j/Logger jar file out of multiple bindings from the apache zip? 我红色 Apache logging it says configure logging. i am not developing any web related functions in my application. Adding jar files are not enough? I red https://www.slf4j.org/codes.html 这篇错误处理文章 我从未找到与应用程序相关的特定错误。
log4j:WARN No appenders could be found for logger (org.apache.pdfbox.io.ScratchFileBuffer).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
您可以在下面找到我的代码,其中包含 POI API 和 PDFBOX API。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FIRST {
//private static WordExtractor we;
//static InputStream inc;
static PDDocument pdDoc = null;
public static void main(String[] args) throws IOException {
POIFSFileSystem fs = null;
// String target_dir = "E:\TESTTRS";
//File dir = new File(target_dir);
// File[] files = dir.listFiles();
String target_dir = "C:\Users";
File dir = new File(target_dir);
File[] files = dir.listFiles();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("firstsheet");
Row row0 = sheet.createRow(0);
row0.createCell(0).setCellValue("S.NO");
row0.createCell(1).setCellValue("DOCUMENT");
row0.createCell(2).setCellValue("VALUE1");
row0.createCell(3).setCellValue("VALUE2");
row0.createCell(4).setCellValue("TEND");
int j = 1;
for ( int s=0;s<files.length;s++){
if(files[s].isFile()){
pdDoc = PDDocument.load(files[s]);
//fs = new POIFSFileSystem(new FileInputStream(files[s]));
PDFTextStripper Stripper = new PDFTextStripper();
String st = Stripper.getText(pdDoc);
String linesp = System.lineSeparator();
String[] paragraph = st.split(linesp);
//HWPFDocument doc = new HWPFDocument(fs);
//we = new WordExtractor(doc);
//String[] paragraph= we.getParagraphText();
Row row1 = sheet.createRow(j);
/***************************1_PRINTS S.NO *************************************/
Cell cell_10 =row1.createCell(0);
cell_10.setCellValue(j);
j++;
/***************************2_PRINTS FILE NAMES *********************************/
Cell cell_11 = row1.createCell(1);
cell_11.setCellValue(files[s].getName());
/******************************3_PRINTS VALUE1*****************************************/
Cell cell_12 = row1.createCell(2);
String len = files[s].getName().substring(13, 19);
cell_12.setCellValue(len);
/**********************4_PRINTS VALUE2 *******************************/
Cell cell_13 = row1.createCell(3);
for(String p: paragraph){
if(p.startsWith("VALUE2"))
cell_13.setCellValue(p.substring(22));
}
/*******************5_PRINTS TEND*****************************************/
Cell cell_14 = row1.createCell(4);
for(String pp: paragraph){
if(pp.contains("TEND"))
cell_14.setCellValue(pp);
}
/**************6_TEST PATTERNS*********************************************/
Cell cell_15 = row1.createCell(5);
for(String c : paragraph){
final String regex = ("^.*([0-9]{6}\/[A-Z][0-9]{5}).*$");
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(c);
for (int i = 1; i < matcher.groupCount(); i++) {
if(c.startsWith("COMMENT:"))
cell_15.setCellValue(""+matcher.group(i));
}
}
workbook.write(new FileOutputStream("C:\Users\abc.xls"));
workbook.close();
}
pdDoc.close();
}}}
我通过将我项目的相关 mvnrepository 构建到 pom.xml 中来解决它。并使用适当的根记录器和 log4j 附加程序在 "src" 中配置 log4j.properties。如果我们想使用 PDFBox API 解析 PDF 文档。