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

Asked

Viewed 1,183 times

0

I am developing a project that extracted information from a *.pdf file, was saving this information in a *.txt file, but now I was asked that this information be saved in the format of a spreadsheet, I want to create a *.csv file, but the only way I could create the file would be by determining what would be written in each column, but I need to put the value of the variables in these 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.
                }
            }
        };
    }
}

}

  • I couldn’t understand where your problem lies. Create an Excel spreadsheet as you imagine and then save it in CSV to see how it looks. It’s simple. Only that the first line contains the header line and the lines below with all values separated by a point and a comma.

  • Yeah, it’s simple if I put the value of the lines StrW.write("1;123456789;Lido"); but if I want to put the value of variables on these error lines, for example StrW.write(pagina;autorizacao;status);

  • Absolutely. It is not possible to do this. The variables have to be solved for Excel. Unless the program that will read this file knows how to solve these variables, or something like that: StrW.write(pagina.value.toString();a‌​utorizacao.value.toString();status.value ); assuming, of course, that these variables have these methods.

  • Well face the truth, I didn’t understand what you meant, but thanks for the help anyway

1 answer

0


I managed to generate a file as 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
                                    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.