Pusty Wyeksportowany CSV

0

Witam, wykonuje eksport metaDanych do pliku CSV i niestety plik po utworzeniu jest pusty,. Czy mam jakiś błąd w kodzie ?

W pętli rzutuje id który idzie do metody, która z bazy ( procedury sql) wyciąga metadane :


public AssortmentMetaInfoDto fetchAssortmentMetaInfo(Integer aid) throws Exception {
        CallableStatement cs = null;
        Connection conn = null;
        ResultSet rs = null;
        AssortmentMetaInfoDto metaInfo = null;
        logger.report(getClass(), "fetchAssortmentMetaInfo", "start");
        try {
            conn = dsm.getDataSource(DataSourceManagerBean.DS_MAIN).getConnection();
            cs = conn.prepareCall("{call tabela @aid = ?}");
            cs.setInt(1, aid);
            rs = cs.executeQuery();
            while (rs.next()) {
                String isbn = rs.getString("isbn");
                String tytul = rs.getString("tytul");
                String autorzy = rs.getString("autorzy");
                String tlumacz_lektor = rs.getString("tlumacz_lektor");
                Integer ilosc_stron = rs.getInt("ilosc_stron");
                String cena_det_brutto = rs.getString("cena_det_brutto");
                String data_premiery = rs.getString("data_premiery");
                String wygasniecie = rs.getString("wygasniecie");
                String isbn_wersji_papierowej = rs.getString("isbn_wersji_papierowej");
                String data_wydania = rs.getString("data_wydania");
                String tematyki = rs.getString("tematyki_helion");
                String opis = rs.getString("opis");
                String nazwa = rs.getString("nazwa");
                String indeks = rs.getString("indeks");
                metaInfo = new AssortmentMetaInfoDto(isbn, tytul, autorzy, tlumacz_lektor, ilosc_stron, cena_det_brutto, data_premiery, wygasniecie, isbn_wersji_papierowej, data_wydania, tematyki, opis, nazwa, indeks);
            }

            logger.report(getClass(), "fetchAssortmentMetaInfo", "end");
        } catch (Exception e) {
            logger.error(getClass(), "fetchAssortmentMetaInfo, errorMsg = " + e.getMessage(), e);
            throw e;
        }
        return metaInfo;
    }

przekazuje je do kodu ktory tworzy mi plik przyklad.csv i ma zapsać w nim podane nagłówki oraz kolejne wiersze z metadanymi



            List<AssortmentMetaInfoDto> assortmentMetaInfoDtos = Lists.newArrayList();
            for (Integer assortmentId : asortList) {
                assortmentMetaInfoDtos.add(
                        assortmentExportContentsDAO.fetchAssortmentMetaInfo(assortmentId)
                );
            }

            FileOutputStream fileOutputStream = null;
            try {
                File metaReport = new File(EXPORT_DIRECTORY + "\\" + "przyklad.csv");
                fileOutputStream = new FileOutputStream(metaReport);
                
                CsvExporter exporter = new CsvExporter(';');
                exporter.openStream(fileOutputStream);
                String[] headLine = {
                        "Nazwa pliku ksiazki",
                        "Nazwa pliku z okladka",
                        "ISBN 13/10",
                        "Tytuł",
                        "Autor",
                        "Tłumacz/Lektor",
                        "Liczba stron/Czas",
                        "Cena netto",
                        "Cena brutto",
                        "Data uruchomienia (rrrr-mm-dd)",
                        "Data końca (rrrr-mm-dd)",
                        "Data koniec NASBI (rrrr-mm-dd)",
                        "Cena netto NASBI",
                        "ISBN (druk)",
                        "Data wydania (druk)",
                        "Kategoria",
                        "Notka",
                        "Nazwa wydawcy",
                        "Dodatkowe ID wydawcy",
                };
                exporter.writeLine(headLine);

                for (AssortmentMetaInfoDto dto : assortmentMetaInfoDtos) {
                    String[] line = {
                            "",
                            "",
                            dto.getIsbn(),
                            dto.getTytul(),
                            dto.getAutorzy(),
                            dto.getTlumacz_lektor(),
                            dto.getIlosc_stron().toString(),
                            "",
                            dto.getCena_det_brutto(),
                            dto.getData_premiery(),
                            dto.getWygasniecie(),
                            "" ,
                            "" ,
                            dto.getIsbn_wersji_papierowej(),
                            dto.getData_wydania(),
                            dto.getTematyki_helion(),
                            dto.getOpis(),
                            dto.getNazwa(),
                            dto.getIndeks()
                    };
                    fileOutputStream.write(239);
                    fileOutputStream.write(187);
                    fileOutputStream.write(191);
                    exporter.writeLine(line);
                }
            } finally {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }

niestety Plik okazuje się być pusty...

0

Dlaczego nie stosujesz obsługi błędów?

            ...
            catch(Exception e){
                 e.printStackTrace();
            } finally {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }
0
bogdans napisał(a):

Dlaczego nie stosujesz obsługi błędów?

            ...
            catch(Exception e){
                 e.printStackTrace();
            } finally {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }

Oczywiscie że stosuje i jest niżej, niestety nie ma exceptiona zadnego :


 List<AssortmentMetaInfoDto> assortmentMetaInfoDtos = Lists.newArrayList();
            for (Integer assortmentId : asortList) {
                assortmentMetaInfoDtos.add(
                        assortmentExportContentsDAO.fetchAssortmentMetaInfo(assortmentId)
                );
            }

            FileOutputStream fileOutputStream = null;
            try {
                File metaReport = new File(EXPORT_DIRECTORY + "\\" + "przyklad.csv");
                fileOutputStream = new FileOutputStream(metaReport);

                CsvExporter exporter = new CsvExporter(';');
                exporter.openStream(fileOutputStream);
                String[] headLine = {
                        "Nazwa pliku ksiazki",
                        "Nazwa pliku z okladka",
                        "ISBN 13/10",
                        "Tytuł",
                        "Autor",
                        "Tłumacz/Lektor",
                        "Liczba stron/Czas",
                        "Cena netto",
                        "Cena brutto",
                        "Data uruchomienia (rrrr-mm-dd)",
                        "Data końca (rrrr-mm-dd)",
                        "Data koniec NASBI (rrrr-mm-dd)",
                        "Cena netto NASBI",
                        "ISBN (druk)",
                        "Data wydania (druk)",
                        "Kategoria",
                        "Notka",
                        "Nazwa wydawcy",
                        "Dodatkowe ID wydawcy",
                };
                exporter.writeLine(headLine);

                for (AssortmentMetaInfoDto dto : assortmentMetaInfoDtos) {
                    String[] line = {

                            "",
                            "",
                            dto.getIsbn(),
                            dto.getTytul(),
                            dto.getAutorzy(),
                            dto.getTlumacz_lektor(),
                            dto.getIlosc_stron().toString(),
                            "",
                            dto.getCena_det_brutto(),
                            dto.getData_premiery(),
                            dto.getWygasniecie(),
                            "" ,
                            "" ,
                            dto.getIsbn_wersji_papierowej(),
                            dto.getData_wydania(),
                            dto.getTematyki_helion(),
                            dto.getOpis(),
                            dto.getNazwa(),
                            dto.getIndeks()
                    };
                    fileOutputStream.write(239);
                    fileOutputStream.write(187);
                    fileOutputStream.write(191);
                    exporter.writeLine(line);
                }
            } finally {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }

            logger.report(getClass(), exportName, "end");
        } catch (Exception e) {
            logger.error(getClass(), exportName, e);
        }
    }

0

Policz nawiasy, to jest catch od innego try (catch nie może być po finally).

0

podałem wam tylko część kodu, wiec catch jest z innego traya.. a. Jeśli tutaj coś piszę to znaczy że debugowałem i nic z tego nie było. Generalnie rozwiązałem problem ;) nie zamknąłem stream-a :

przed finally :


try {
                    exporter.closeStream();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

1 użytkowników online, w tym zalogowanych: 0, gości: 1