/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package com.megatim.fdxconvert.util; import static com.megatim.typefichier.validator.utilities.Utilities.getCharset; import com.megatim.fdxconvert.pojo.FileToValidateDescription; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * * @author ASUS */ public class Count { public static int countEltsOfTextFile(FileToValidateDescription fileDesc, boolean headerPresent) throws Exception { int nbElts = 0; if (fileDesc != null) { Charset charset = getCharset(fileDesc.getFile()); //Lecture des lignes du fichier et chargement dans une liste try ( FileInputStream fis = new FileInputStream(fileDesc.getFile()); InputStreamReader isr = new InputStreamReader(fis, charset); BufferedReader reader = new BufferedReader(isr)) { boolean withHeader = headerPresent; int c = 0; boolean carriageReaded = false; //Indique si on a lu un retour chariot char lastCharacter = '\u0000'; while ((c = reader.read()) != -1) { char characterRead = (char) c; if (characterRead == '\r') { if (carriageReaded) { //On a lu le retour chariot deux fois consécutives donc on a pas de séparateur de ligne au sens windows carriageReaded = false; //On marque que la lecture d'une nouvelle ligne redémarre } else { // Première lecture du retour chariot carriageReaded = true; // On marque que nous avons lu un retour chariot } } else { if (carriageReaded) { // Le caractère précédemment lu c'est le retour chariot if (characterRead == '\n') { // Si le nouveau caractère lu est un saut de ligne c'est que nous venons de lire une nouvelle ligne if (!withHeader) { if (fileDesc.getColumnDelimiter().equals("\\t")) { nbElts++; } else if (fileDesc.getColumnDelimiter().isEmpty()) { nbElts++; } else { nbElts++; } } else { withHeader = false; } carriageReaded = false; //On marque que la lecture d'une nouvelle ligne redémarre } else { } } else { if (characterRead == '\n') { if (lastCharacter != '\n') { } } else { } } } lastCharacter = characterRead; } } } return nbElts; } public static int countEltsOfCsvFile(FileToValidateDescription fileDesc, boolean headerPresent) throws Exception { int nbElts = 0; Charset charset = getCharset(fileDesc.getFile()); AtomicBoolean withHeader = new AtomicBoolean(headerPresent); try ( FileInputStream fis = new FileInputStream(fileDesc.getFile()); InputStreamReader isr = new InputStreamReader(fis, charset); BufferedReader reader = new BufferedReader(isr)) { CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.builder() .setSkipHeaderRecord(headerPresent) .setIgnoreSurroundingSpaces(true) .setTrim(true) .setDelimiter(fileDesc.getColumnDelimiter()) .setRecordSeparator(fileDesc.getRowDelimiter()) .build()); for (CSVRecord record : parser) { if (!withHeader.get()) { nbElts++; } else { withHeader.set(false); } } } catch (Exception e) { Logger.getLogger(ImportData.class.getName()).log(Level.SEVERE, e.getMessage(), e); } return nbElts; } public static int countEltsOfparseXlsxFile(File file, int sheetNumber, boolean headerPresent) throws Exception { try ( InputStream is = new FileInputStream(file); Workbook wb = new XSSFWorkbook(is)) { return countEltsOfExcelFile(wb, sheetNumber, headerPresent); } } public static int countEltsOfXlsFile(File file, int sheetNumber, boolean headerPresent) throws Exception { try ( InputStream is = new FileInputStream(file); Workbook wb = new HSSFWorkbook(is)) { return countEltsOfExcelFile(wb, sheetNumber, headerPresent); } } private static int countEltsOfExcelFile(Workbook wb, int sheetNumber, boolean headerPresent) { int nbElts = 0; Sheet sheet = wb.getSheetAt(sheetNumber); AtomicBoolean withHeader = new AtomicBoolean(headerPresent); for (Row r : sheet) { if (!withHeader.get()) { nbElts++; } else { withHeader.set(false); } } return nbElts; } }