List of usage examples for org.apache.commons.io LineIterator closeQuietly
public static void closeQuietly(LineIterator iterator)
From source file:playground.staheale.preprocess.AgentInteractionEnterpriseCensusParser.java
private final void readPresenceCodes(AgentInteractionEnterpriseCensus ec) { log.info("Reading the presence code file..."); int lineCounter = 0; int skip = 1; String filename = presenceCodeFile; String separator = ";"; File file = new File(filename); LineIterator it = null;/*from w w w. j av a2 s . co m*/ String line = null; String[] tokens = null; String reli = null; try { it = FileUtils.lineIterator(file, "UTF-8"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { while (it.hasNext()) { line = it.nextLine(); tokens = line.split(separator); if (lineCounter == 0) { log.info("Processing header line..."); for (String token : tokens) { ec.addPresenceCodeNOGAType(token.replaceAll("\"", "")); } log.info("Processing header line...done."); } else { reli = tokens[0]; for (int pos = 0; pos < tokens.length; pos++) { if (Pattern.matches("1", tokens[pos])) { ec.addPresenceCode(reli, ec.getPresenceCodeNOGAType(pos)); } } } lineCounter++; if (lineCounter % skip == 0) { log.info("Processed hectares: " + Integer.toString(lineCounter)); skip *= 2; } } } finally { LineIterator.closeQuietly(it); } log.info("Processed hectares: " + Integer.toString(lineCounter)); log.info("Reading the presence code file...done."); }
From source file:playground.staheale.preprocess.AgentInteractionEnterpriseCensusParser.java
private final void readHectareAggregations(AgentInteractionEnterpriseCensus ec) { log.info("Reading the hectare aggregation file..."); String separator = ","; String filename = inputHectareAggregationFile; File file = new File(filename); LineIterator it = null;/* w w w .ja va 2 s .co m*/ String line = null; String[] tokens = null; String reli = null; int lineCounter = 0, skip = 1; try { it = FileUtils.lineIterator(file, "UTF-8"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { while (it.hasNext()) { line = it.nextLine(); tokens = line.split(separator); if (lineCounter == 0) { log.info("Processing header line..."); for (String token : tokens) { ec.addhectareAggregationNOGAType(token.replaceAll("\"", "")); } log.info("Processing header line...done."); } else { reli = tokens[0]; for (int pos = 0; pos < tokens.length; pos++) { if (!Pattern.matches("0", tokens[pos])) { ec.addHectareAggregationInformation(reli, ec.getHectareAggregationNOGAType(pos), Double.parseDouble(tokens[pos])); } } } lineCounter++; if (lineCounter % skip == 0) { log.info("Processed hectares: " + Integer.toString(lineCounter)); skip *= 2; } } } finally { LineIterator.closeQuietly(it); } log.info("Processed hectares: " + Integer.toString(lineCounter)); log.info("Reading the hectare aggregation file...done."); }
From source file:se.alingsas.alfresco.repo.utils.byggreda.ReadMetadataDocument.java
/** * Takes an input stream which should point to a metadata document for * byggreda. Validates and parses the data and returns a set of * ByggRedaDocument/* w ww. j a va 2 s . co m*/ * * @param inputStream * @return */ public static Set<ByggRedaDocument> read(final InputStream inputStream, List<String> globalMessages) { if (inputStream == null) { return null; } Set<ByggRedaDocument> result = new HashSet<ByggRedaDocument>(); LineIterator lineIterator = null; String line = ""; int lineNumber = 1; try { lineIterator = IOUtils.lineIterator(inputStream, "ISO-8859-1"); // Skip first line which is a header line if (lineIterator.hasNext()) { line = lineIterator.nextLine(); if (!line.startsWith("\"Film\";\"") && !line.startsWith("Film;")) { globalMessages.add( "#1: Sidhuvud ej funnet p frsta raden i styrfilen. Frsta raden var: " + line); LOG.error("No header found on the first line in the document. First line was: " + line + ". Aborting..."); return result; } else { LOG.debug("Line #" + lineNumber + ": Skipping header"); } } while (lineIterator.hasNext()) { lineNumber++; line = lineIterator.nextLine(); // if it's an empty line or a comment, skip if (!StringUtils.hasText(line) || line.startsWith("#")) { globalMessages.add("#" + lineNumber + ": Tom rad, eller bortkommenterad rad funnel, skippas"); LOG.info("Line #" + lineNumber + ": Skipping comment or empty line"); continue; } // Validation and error handling ByggRedaDocument document = parseAndValidate(line); document.setLineNumber(lineNumber); if (!document.isReadSuccessfully()) { // An error occured, we need to log this LOG.error("Line #" + document.getLineNumber() + ": " + document.getStatusMsg()); } else { // Document successfully read LOG.debug("Line #" + document.getLineNumber() + ": " + "Successfully read record. , Record number: " + document.getRecordDisplay()); } result.add(document); } } catch (final Exception ex) { globalMessages.add("#" + lineNumber + ": Fel vid inlsning av rad " + lineNumber + " frn styrfil. Radens innehll: " + line + " Systemmeddelande: " + ex.getMessage()); LOG.error("Error on line '" + lineNumber + "'. Line contents: " + line, ex); } finally { IOUtils.closeQuietly(inputStream); LineIterator.closeQuietly(lineIterator); } return result; }
From source file:se.inera.intyg.rehabstod.integration.it.stub.PersonnummerLoaderImpl.java
@Override public List<String> readTestPersonnummer() throws IOException { Resource resource = getResource(testPersonnummerFile); LineIterator it = FileUtils.lineIterator(resource.getFile(), "UTF-8"); List<String> personnummerList = new ArrayList<>(); try {/* w ww . j a v a2s.co m*/ // Skip CSV column name if (it.hasNext()) { it.nextLine(); } while (it.hasNext()) { String line = it.nextLine(); personnummerList.add(line); } } finally { LineIterator.closeQuietly(it); } return personnummerList; }
From source file:se.inera.intyg.rehabstod.service.diagnos.DiagnosGruppLoaderImpl.java
private List<DiagnosGrupp> getDiagnosGrupperInternal(Resource resource) throws IOException { LineIterator it = FileUtils.lineIterator(resource.getFile(), "UTF-8"); List<DiagnosGrupp> list = new ArrayList<>(); try {/*from w w w . java 2 s.co m*/ while (it.hasNext()) { String line = it.nextLine(); list.add(new DiagnosGrupp(line)); } } finally { LineIterator.closeQuietly(it); } return list; }
From source file:se.inera.intyg.rehabstod.service.diagnos.DiagnosKapitelLoaderImpl.java
@Override public List<DiagnosKapitel> loadDiagnosKapitel() throws IOException { LineIterator it = FileUtils.lineIterator(diagnosKapitelFile.getFile(), "UTF-8"); List<DiagnosKapitel> list = new ArrayList<>(); try {/*from w w w . j av a 2s .c om*/ while (it.hasNext()) { String line = it.nextLine(); list.add(new DiagnosKapitel(line)); } } finally { LineIterator.closeQuietly(it); } return list; }
From source file:se.inera.intyg.rehabstod.service.diagnos.DiagnosKoderFileTest.java
private List<String> loadDiagnosFile(String fileName) throws IOException { String filePath = getClass().getClassLoader().getResource(fileName).getFile(); LineIterator it = FileUtils.lineIterator(new File(filePath), "ISO-8859-1"); List<String> koder = new ArrayList<>(); try {/* w w w . ja va2 s . c o m*/ while (it.hasNext()) { String line = it.nextLine(); String kod = line.substring(0, DiagnosKod.KOD_LENGTH).trim(); koder.add(kod); } } finally { LineIterator.closeQuietly(it); } return koder; }
From source file:se.inera.intyg.rehabstod.service.diagnos.DiagnosKoderLoaderImpl.java
private Map<String, String> loadDiagnosFile(Resource resource) throws IOException { LineIterator it = FileUtils.lineIterator(resource.getFile(), "ISO-8859-1"); Map<String, String> map = new HashMap<>(); try {/*from w w w . ja va 2 s . co m*/ while (it.hasNext()) { String line = it.nextLine(); DiagnosKod kod = new DiagnosKod(line); if (kod.getCleanedCode() != null) { map.put(kod.getCleanedCode(), kod.getName()); } } } finally { LineIterator.closeQuietly(it); } return map; }
From source file:yet.another.hackernews.reader.functions.DownloadText.java
@Override protected String doInBackground(String... urlAdress) { /*//from w ww . j a v a 2 s . co m * Core of the class that does the background task. */ id = urlAdress[0].hashCode(); // If we want to use the cache, try and get it first String cacheResults = null; if (useCache) { cacheResults = HardCache.getString(id, context); if (cacheResults != null) { if (!forceUpdate && !Cache.doUpdate(context, id)) { return cacheResults; } } } // Set target url URL url; try { url = new URL(urlAdress[0]); } catch (MalformedURLException e) { // If listener is attached, report error. if (listener != null) { handler.post(new Runnable() { @Override public void run() { listener.onError(MALFORMED_URL_ERROR); } }); } return cacheResults; } // Stream to URL adress BufferedReader bufferedReader = null; LineIterator line = null; // Results, size set to 100 chars final StringBuffer textResults = new StringBuffer(0); try { // Open stream bufferedReader = new BufferedReader(new InputStreamReader(url.openStream(), charset), bufferSize); // Download text line = IOUtils.lineIterator(bufferedReader); while (line.hasNext()) { if (this.isCancelled()) { break; } textResults.append(line.nextLine()); } } catch (IOException e) { if (textResults.length() > 0) { textResults.delete(0, textResults.length()); } if (cacheResults != null) { textResults.append(cacheResults); } if (listener != null) { handler.post(new Runnable() { @Override public void run() { listener.onError(IOEXCEPTION_ERROR); } }); } } finally { // Close reader LineIterator.closeQuietly(line); IOUtils.closeQuietly(bufferedReader); } if (textResults.length() <= 0) { return null; } // If use cache, put it in cache if (useCache && (!textResults.toString().equals(cacheResults))) { HardCache.putString(id, textResults.toString(), context); Cache.saveUpdate(context, id); } return textResults.toString(); }