Create a * .csv file with data found from a file

0

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.
                }
            }
        };
    }
}

}

    
asked by anonymous 15.08.2016 / 20:46

1 answer

0

I was able to generate a file as I needed from this code:

for (int i = 1; i <= pdDoc.getNumberOfPages(); i++) {
                                        pdfStripper.setStartPage(i);
                                        pdfStripper.setEndPage(i);
                                        String parsedText = pdfStripper.getText(pdDoc);

                                        //<editor-fold defaultstate="collapsed" desc="VARIAVEIS STRINGS">
                                        String aut = "";
                                        String Status = "";
                                        String pagina = Integer.toString(i);
                                        //</editor-fold>

                                        //<editor-fold defaultstate="collapsed" desc="VARIAVEIS "INT" REUTILIZADAS EM CADA PÁGINA">
                                        int AUT = 0;
                                        //</editor-fold>

                                        //<editor-fold defaultstate="collapsed" desc="MATCHER - EXPRESSÕES REGULARES">                            
                                        Matcher matcherAut = Pattern.compile("\s\b00\d{7}\b|\b[3-9]\d{8}\b").matcher(parsedText);
                                        //</editor-fold>

                                        //<editor-fold defaultstate="collapsed" desc="MATCHER AUTORIZAÇÃO">
                                        if (!matcherAut.find()) {
                                            Status = "Não Lido";
                                            aut = "-";
                                        } else {
                                            Pattern pattern = Pattern.compile("\s");
                                            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(pagina+";");
                                        StrW.write(aut+";");
                                        StrW.write(Status+"\n");
                                        //</editor-fold>

                                        linhasGravadas.clear(); // LIMPAR ARRAY DE TODOS OS DADOS ENCONTRADOS
                                    }
    
11.11.2016 / 12:17