I'm developing a project that extracts information from a * .pdf file, I was saving this information in a * .txt file, but now I was asked to save this information in the form of a worksheet, I want to create a * file. csv, but the only way I found to create the file would be to determine what would be written in each column, but I need to put the value of the variables in those columns.
public class modelo {
public static void main(String[] args) {
System.out.println("inicio");
Timer timer = null;
if (timer == null) {
timer = new Timer();
TimerTask tarefa = new TimerTask() {
public void run() {
try {
File diretorio = new File("C//teste//");
File[] arquivos = diretorio.listFiles();
if (arquivos != null) {
for (int x = 0; x < arquivos.length; x++) {
if (arquivos[x].getName().endsWith("pdf")) {
File f = arquivos[x];
try (RandomAccessBufferedFileInputStream acesso = new RandomAccessBufferedFileInputStream(f.getAbsolutePath())) {
PDFParser parser = new PDFParser(acesso);
parser.parse();
COSDocument cosDoc = parser.getDocument();
PDFTextStripper pdfStripper = new PDFTextStripper();
PDDocument pdDoc = new PDDocument(cosDoc);
BufferedWriter StrW = new BufferedWriter(new FileWriter("C//teste2//" + f.getName().replace(".pdf", ".csv")));
List<String> linhasGravadas = new ArrayList<>();
int teste = 0, Aut = 0, vAut = 0;
StrW.write("Página;Autotrização;Status Leitura;Retorno Ticket Log");
for (int i = 1; i <= pdDoc.getNumberOfPages(); i++) {
pdfStripper.setStartPage(i);
pdfStripper.setEndPage(i);
String parsedText = pdfStripper.getText(pdDoc);
String aut = "";
String Status = "";
int AUT = 0;
Matcher matcherAut = Pattern.compile("\s\b00\d{7}\b|\b[3-9]\d{8}\b").matcher(parsedText);
if (!matcherAut.find()) {
vAut = vAut + 1;
AUT = AUT + 1;
aut = "-";
Status = "Nao Lido";
} else {
Pattern pattern = Pattern.compile("\s^0.*|^0.*"); // Segundo Filtro (Elimina os que não começam com 3|4)
Scanner scanner = new Scanner(matcherAut.group()).useDelimiter(pattern);
Matcher matcher2 = pattern.matcher(aut);
while (scanner.hasNext()) {
aut = scanner.next();
if (!linhasGravadas.contains(aut)) {
linhasGravadas.add(aut);
Aut = Aut + 1;
Status = "Lido";
}
}
}
StrW.write(i,teste,status); // AQUI FICARIA AS VARIAVEIS
linhasGravadas.clear(); // LIMPAR ARRAY DE TODOS OS DADOS ENCONTRADOS
}
acesso.close();
}
f.renameTo(new File("C//testes3//", f.getName()));
}
}
}
} catch (IOException e) {
e.printStackTrace(); // Tratar a exceção adequadamente.
}
}
};
}
}
}