List of usage examples for java.util Scanner hasNextLine
public boolean hasNextLine()
From source file:com.mucommander.job.FindFileJob.java
private boolean fileContainsString0(AbstractFile f) { //Profiler.start("check_old"); if (fileContent == null || fileContent.isEmpty()) { return true; }/*from w w w.j a v a2 s . com*/ if (f.isDirectory()) { return false; } Scanner in = null; boolean result = false; try { in = new Scanner(f.getInputStream()); while (in.hasNextLine() && !result) { String line = in.nextLine(); if (!caseSensitive) { line = line.toLowerCase(); } result = line.contains(fileContent); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (Exception e) { e.printStackTrace(); } } //Profiler.stop("check_old"); return result; }
From source file:org.apache.bigtop.bigpetstore.datagenerator.datareaders.ZipcodeReader.java
private ImmutableMap<String, ZipcodeLocationRecord> readCoordinates(InputStream path) throws FileNotFoundException { Scanner scanner = new Scanner(path); // skip header scanner.nextLine();/*from ww w . ja v a2 s.c om*/ Map<String, ZipcodeLocationRecord> entries = Maps.newHashMap(); while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); String[] cols = line.split(", "); // remove quote marks String zipcode = cols[0].substring(1, cols[0].length() - 1); String state = cols[1].substring(1, cols[1].length() - 1); Double latitude = Double.parseDouble(cols[2].substring(1, cols[2].length() - 1)); Double longitude = Double.parseDouble(cols[3].substring(1, cols[3].length() - 1)); String city = cols[4].substring(1, cols[4].length() - 1); Pair<Double, Double> coords = Pair.of(latitude, longitude); ZipcodeLocationRecord record = new ZipcodeLocationRecord(coords, city, state); entries.put(zipcode, record); } scanner.close(); return ImmutableMap.copyOf(entries); }
From source file:Methods.ManageKey.java
/** * Mtodo que procesa el fichero obtenido en el metodo Open * @param keyFile//from w w w . j ava 2 s . c o m * @return */ private String[] processFile(File keyFile) { String[] keys = new String[4]; String line; // dentro de las primeras 20 lineas han de estar todos los componentes necesarios int numLines = 0; try { Scanner file = new Scanner(keyFile); while (file.hasNextLine() && numLines < 20) { line = file.nextLine(); numLines++; if (StringUtils.contains(line, "P generado")) { line = file.nextLine(); //para dejar solo los nmeros keys[0] = StringUtils.replaceAll(line, "[^0-9]", ""); numLines++; } if (StringUtils.contains(line, "Q generado")) { line = file.nextLine(); keys[1] = StringUtils.replaceAll(line, "[^0-9]", ""); numLines++; } if (StringUtils.contains(line, "e generada")) { line = file.nextLine(); keys[2] = StringUtils.replaceAll(line, "[^0-9]", ""); numLines++; } if (StringUtils.contains(line, "Unidades:")) { if (StringUtils.contains(line, "Decimal")) { keys[3] = "Decimal"; } else { keys[3] = "Hexadecimal"; } numLines++; } } } catch (FileNotFoundException e) { this.errorDialog.readingFile(); return null; } if (this.keysNotOk(keys)) { this.errorDialog.missingComponents(); keys = null; } return keys; }
From source file:org.calrissian.accumulorecipes.commons.hadoop.GroupedKeyRangePartitioner.java
private synchronized Text[] getCutPoints() throws IOException { if (cutPointArray == null) { Path[] cf = DistributedCache.getLocalCacheFiles(conf); if (cf != null) { Map<String, String> curFilesAndGroups = getCurFilesAndGroups(); SortedMap<String, SortedSet<String>> cutPointMap = new TreeMap<String, SortedSet<String>>(); for (Path path : cf) { String group = null; for (Map.Entry<String, String> groupSplits : curFilesAndGroups.entrySet()) { if (path.toString().endsWith(groupSplits.getKey())) group = groupSplits.getValue(); }/*from w ww. j a v a 2 s . c o m*/ if (group != null) { Scanner in = new Scanner(new BufferedReader(new FileReader(path.toString()))); try { while (in.hasNextLine()) { String split = new String(Base64.decodeBase64(in.nextLine().getBytes())); SortedSet<String> splits = cutPointMap.get(group); if (splits == null) { splits = new TreeSet<String>(); cutPointMap.put(group, splits); } } SortedSet<Text> treeSet = new TreeSet<Text>(); for (Map.Entry<String, SortedSet<String>> entry : cutPointMap.entrySet()) { treeSet.add(new Text(entry.getKey() + NULL_BYTE + NULL_BYTE)); for (String string : entry.getValue()) treeSet.add(new Text(entry.getKey() + NULL_BYTE + string)); treeSet.add(new Text(entry.getKey() + NULL_BYTE + END_BYTE)); } cutPointArray = treeSet.toArray(new Text[] {}); } finally { in.close(); } break; } else { throw new FileNotFoundException( "A file was not found in distribution cache files: " + path.toString()); } } } } return cutPointArray; }
From source file:org.apache.accumulo.core.client.mapreduce.lib.partition.RangePartitioner.java
private synchronized Text[] getCutPoints() throws IOException { if (cutPointArray == null) { String cutFileName = conf.get(CUTFILE_KEY); Path[] cf = DistributedCacheHelper.getLocalCacheFiles(conf); if (cf != null) { for (Path path : cf) { if (path.toUri().getPath().endsWith(cutFileName.substring(cutFileName.lastIndexOf('/')))) { TreeSet<Text> cutPoints = new TreeSet<Text>(); Scanner in = new Scanner(new BufferedReader( new InputStreamReader(new FileInputStream(path.toString()), Constants.UTF8))); try { while (in.hasNextLine()) cutPoints//from w w w .ja v a 2s . c o m .add(new Text(Base64.decodeBase64(in.nextLine().getBytes(Constants.UTF8)))); } finally { in.close(); } cutPointArray = cutPoints.toArray(new Text[cutPoints.size()]); break; } } } if (cutPointArray == null) throw new FileNotFoundException(cutFileName + " not found in distributed cache"); } return cutPointArray; }
From source file:org.apache.bigtop.bigpetstore.datagenerator.datareaders.ZipcodeReader.java
private ImmutableMap<String, Double> readIncomeData(InputStream path) throws FileNotFoundException { Scanner scanner = new Scanner(path); // skip headers scanner.nextLine();/*w w w.j a va2 s.c o m*/ scanner.nextLine(); Map<String, Double> entries = Maps.newHashMap(); while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); String[] cols = line.split(","); // zipcodes are in the form "ZCTA5 XXXXX" String zipcode = cols[2].split(" ")[1].trim(); try { double medianHouseholdIncome = Integer.parseInt(cols[5].trim()); entries.put(zipcode, medianHouseholdIncome); } catch (NumberFormatException e) { } } scanner.close(); return ImmutableMap.copyOf(entries); }
From source file:org.languagetool.tools.SynthDictionaryBuilder.java
private File reverseLineContent(File plainTextDictFile, Set<String> itemsToBeIgnored, Pattern ignorePosRegex) throws IOException { File reversedFile = File.createTempFile(SynthDictionaryBuilder.class.getSimpleName() + "_reversed", ".txt"); String separator = getOption("fsa.dict.separator"); if (separator == null || separator.trim().isEmpty()) { throw new IOException( "A separator character (fsa.dict.separator) must be defined in the dictionary info file."); }/* w w w. ja va 2s . c o m*/ String encoding = getOption("fsa.dict.encoding"); int posIgnoreCount = 0; Scanner scanner = new Scanner(plainTextDictFile, encoding); try (Writer out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(reversedFile), encoding))) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (itemsToBeIgnored.contains(line)) { System.out.println("Ignoring: " + line); continue; } String[] parts = line.split("\t"); if (parts.length == 3) { String posTag = parts[2]; if (ignorePosRegex != null && ignorePosRegex.matcher(posTag).find()) { posIgnoreCount++; continue; } out.write(parts[0] + separator + parts[1] + "|" + posTag); out.write("\n"); } else { System.err.println("Invalid input, expected three tab-separated columns in " + plainTextDictFile + ": " + line + " => ignoring"); } } scanner.close(); } System.out.println( "Number of lines ignored due to POS tag filter ('" + ignorePosRegex + "'): " + posIgnoreCount); return reversedFile; }
From source file:biblivre3.cataloging.CatalogingHandler.java
@Override public String processModule(HttpServletRequest request, HttpServletResponse response, String submitButton, HashMap<String, String> requestParametersHash) { if (submitButton.equals("EXPORT_RECORD")) { final String recordIds = request.getParameter("serial_list"); final BiblioBO bo = new BiblioBO(); final MemoryFileDTO export = bo.export(recordIds.split(",")); if (export != null) { this.returnFile(export, response); }/* w ww.j av a 2 s .co m*/ return "x-download"; } else if (submitButton.equals("UPLOAD_IMPORT")) { String records = ""; int totalRecords = 0; int validRecords = 0; final HttpSession session = request.getSession(); final UploadedFileBean file = (UploadedFileBean) session.getAttribute("UPLOADED_FILE"); if (file != null) { final StringBuilder builder = new StringBuilder(); final Scanner scanner = new Scanner(file.getInputStream(), "UTF-8"); while (scanner.hasNextLine()) { String iso2709 = scanner.nextLine(); if (StringUtils.isBlank(iso2709)) { continue; } String lines[] = iso2709.replace("\u001E\u001D", "\u001E\u001D\r\n").split("\r\n"); for (String line : lines) { if (StringUtils.isNotBlank(line)) { try { String freemarc = MarcReader.iso2709ToMarc(line); freemarc = TextUtils.combine(freemarc); builder.append(freemarc); builder.append(ApplicationConstants.FREEMARC_RECORD_SEPARATOR); builder.append(ApplicationConstants.LINE_BREAK); totalRecords++; validRecords++; } catch (Exception e) { log.error(e.getMessage(), e); } } } } records = builder.toString(); } JSONObject json = new JSONObject(); try { if (StringUtils.isBlank(records)) { json.put("success", false); json.put("message", I18nUtils.getText(session, "biblivre3", "ERROR_MARC_NOT_FILLED")); } else { json.put("success", true); json.put("marc", records); json.put("totalRecords", String.valueOf(totalRecords)); json.put("validRecords", String.valueOf(validRecords)); } } catch (JSONException je) { } this.returnJson(json, response); return "x-json"; } else if (submitButton.equals("SAVE_IMPORT")) { final String freemarc = request.getParameter("marc"); if (StringUtils.isBlank(freemarc)) { Dialog.showWarning(request, "ERROR_MARC_NOT_FILLED"); return "/jsp/cataloging/import.jsp"; } final String importType = request.getParameter("import_type"); boolean success = false; //biblio_MAIN, biblio_WORK, authorities, vocabulary if (importType.startsWith("biblio")) { String[] ex_auto = null; if (request.getParameter("holding") != null) { ex_auto = new String[6]; ex_auto[0] = request.getParameter("quant"); ex_auto[1] = request.getParameter("nvol"); ex_auto[2] = request.getParameter("nvol_obra"); ex_auto[3] = request.getParameter("biblio_dep"); ex_auto[4] = request.getParameter("aquis"); ex_auto[5] = request.getParameter("dt_tomb"); } String base = "WORK"; if (importType.contains("MAIN")) { base = "MAIN"; } FreeMarcBO fbo = new FreeMarcBO(); success = fbo.importRecords(freemarc, Database.valueOf(base), null, ex_auto); } else { boolean authorities = importType.equals("authorities"); MaterialType mt = authorities ? MaterialType.AUTHORITIES : MaterialType.VOCABULARY; AuthoritiesBO abo = new AuthoritiesBO(); VocabularyBO vbo = new VocabularyBO(); final String[] records = freemarc.split(ApplicationConstants.FREEMARC_RECORD_SEPARATOR); for (final String record : records) { if (StringUtils.isBlank(record)) { continue; } Record recordObj = MarcReader.marcToRecord(record, mt, RecordStatus.NEW); success = authorities ? abo.insert(recordObj) : vbo.insert(recordObj); } } if (success) { Dialog.showNormal(request, "SUCCESS_IMPORT_RECORD"); return "/jsp/cataloging/import.jsp"; } else { Dialog.showError(request, "ERROR_IMPORT_RECORD"); return "/jsp/cataloging/import.jsp"; } } return "/jsp/cataloging/biblio.jsp"; }
From source file:de.vandermeer.skb.commons.utils.Json2Collections.java
/** * Reads JSON from the input scanner, transforms into an SKB map and logs errors. * //w w w.j a v a2s. c om * This method parses the <code>input</code> and removes every line starting with either of the two possible single line comments: '//' and '#'. * It then calls s2o with the altered input. * @param input scanner wit JSON specification * @return a tree with information from the JSON file or null in case of errors */ public Object read(Scanner input) { Object ret = null; if (input != null) { String content = new String(); try { while (input.hasNextLine()) { String line = input.nextLine(); if (!StringUtils.startsWithAny(line.trim(), new String[] { "//", "#" })) content += line.trim(); } } catch (Exception ignore) { } ret = this.s2o(content); } return ret; }
From source file:analysePortalU.AnalysePortalUData.java
public void analyse() throws HttpException, IOException, ParserConfigurationException, SAXException, TransformerException { Map<String, Map<String, String>> resultMap = new HashMap<String, Map<String, String>>(); Scanner in = new Scanner(getClass().getClassLoader().getResourceAsStream("keywords.txt")); while (in.hasNextLine()) { String keyword = URLEncoder.encode(in.nextLine().trim(), "UTF-8"); int currentPage = 1; boolean moreResults = true; while (moreResults) { String url = "http://www.portalu.de/opensearch/query?q=" + keyword.replace(' ', '+') + "+datatype:metadata+ranking:score&h=" + PAGE_SIZE + "&detail=1&ingrid=1&p=" + currentPage; HttpClientParams httpClientParams = new HttpClientParams(); HttpConnectionManager httpConnectionManager = new SimpleHttpConnectionManager(); httpClientParams.setSoTimeout(60 * 1000); httpConnectionManager.getParams().setConnectionTimeout(60 * 1000); httpConnectionManager.getParams().setSoTimeout(60 * 1000); HttpClient client = new HttpClient(httpClientParams, httpConnectionManager); HttpMethod method = new GetMethod(url); // set a request header // this can change in the result of the response since it might // be // interpreted differently // method.addRequestHeader("Accept-Language", language); // //"de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4"); System.out.println("Query: " + url); int status = client.executeMethod(method); if (status == 200) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(method.getResponseBodyAsStream()); XPathUtils xpath = new XPathUtils(new ConfigurableNamespaceContext()); NodeList results = xpath.getNodeList(doc, "/rss/channel/item"); int numberOfResults = results.getLength(); for (int i = 0; i < results.getLength(); i++) { Node node = results.item(i); String fileIdentifier = xpath.getString(node, ".//*/fileIdentifier/CharacterString"); if (!resultMap.containsKey(fileIdentifier)) { resultMap.put(fileIdentifier, new HashMap<String, String>()); }// ww w . java2 s. co m Map<String, String> currentMap = resultMap.get(fileIdentifier); currentMap.put("uuid", fileIdentifier); currentMap.put("partner", xpath.getString(node, "partner")); currentMap.put("provider", xpath.getString(node, "provider")); currentMap.put("udk-class", xpath.getString(node, "udk-class")); currentMap.put("source", xpath.getString(node, "source")); currentMap.put("url", new URL(xpath.getString(node, "link")).toString()); currentMap.put("title", xpath.getString(node, ".//*/title/CharacterString")); currentMap.put("description", xpath.getString(node, ".//*/abstract/CharacterString")); Node addressNode = xpath.getNode(node, ".//*/contact/idfResponsibleParty"); String addressString = ""; String tmp = xpath.getString(addressNode, "indiviualName/CharacterString"); if (tmp != null && tmp.length() > 0) addressString += tmp + "\n"; tmp = xpath.getString(addressNode, "organisationName/CharacterString"); if (tmp != null && tmp.length() > 0) addressString += tmp + "\n"; tmp = xpath.getString(addressNode, "contactInfo/CI_Contact/address/CI_Address/deliveryPoint/CharacterString"); if (tmp != null && tmp.length() > 0) addressString += tmp + "\n"; tmp = xpath.getString(addressNode, "contactInfo/CI_Contact/address/CI_Address/postalCode/CharacterString"); if (tmp != null && tmp.length() > 0) addressString += tmp + " "; tmp = xpath.getString(addressNode, "ontactInfo/CI_Contact/address/CI_Address/city/CharacterString"); if (tmp != null && tmp.length() > 0) addressString += tmp + "\n"; tmp = xpath.getString(addressNode, "contactInfo/CI_Contact/address/CI_Address/country/CharacterString"); if (tmp != null && tmp.length() > 0) addressString += tmp + "\n"; tmp = xpath.getString(addressNode, "contactInfo/CI_Contact/address/CI_Address/electronicMailAddress/CharacterString"); if (tmp != null && tmp.length() > 0) addressString += "Email: " + tmp + "\n"; tmp = xpath.getString(addressNode, "contactInfo/CI_Contact/phone/CI_Telephone/voice/CharacterString"); if (tmp != null && tmp.length() > 0) addressString += "Tel: " + tmp + "\n"; tmp = xpath.getString(addressNode, "contactInfo/CI_Contact/phone/CI_Telephone/facsimile/CharacterString"); if (tmp != null && tmp.length() > 0) addressString += "Fax: " + tmp + "\n"; currentMap.put("pointOfContact", addressString); } if (numberOfResults > 0 && numberOfResults >= PAGE_SIZE) { currentPage++; } else { moreResults = false; } } else { moreResults = false; } } } StringWriter sw = new StringWriter(); ExcelCSVPrinter ecsvp = new ExcelCSVPrinter(sw); boolean fieldsWritten = false; for (String key : resultMap.keySet()) { Map<String, String> result = resultMap.get(key); if (!fieldsWritten) { for (String field : result.keySet()) { ecsvp.print(field); } ecsvp.println(""); fieldsWritten = true; } for (String value : result.values()) { ecsvp.print(value); } ecsvp.println(""); } PrintWriter out = new PrintWriter("result.csv"); out.write(sw.toString()); out.close(); in.close(); System.out.println("Done."); }