List of usage examples for java.util Scanner hasNextLine
public boolean hasNextLine()
From source file:ThreadedEchoServer.java
public void run() { try {/* w w w. j a v a2 s. co m*/ try { InputStream inStream = incoming.getInputStream(); OutputStream outStream = incoming.getOutputStream(); Scanner in = new Scanner(inStream); PrintWriter out = new PrintWriter(outStream, true /* autoFlush */); out.println("Hello! Enter BYE to exit."); // echo client input boolean done = false; while (!done && in.hasNextLine()) { String line = in.nextLine(); out.println("Echo: " + line); if (line.trim().equals("BYE")) done = true; } } finally { incoming.close(); } } catch (IOException e) { e.printStackTrace(); } }
From source file:org.apache.bigtop.bigpetstore.datagenerator.datareaders.ZipcodeReader.java
private ImmutableMap<String, Long> readPopulationData(InputStream path) throws FileNotFoundException { Scanner scanner = new Scanner(path); // skip header scanner.nextLine();/*from w ww. jav a 2s. c o m*/ Map<String, Long> entries = Maps.newHashMap(); while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); if (line.length() == 0) continue; String[] cols = line.split(","); String zipcode = cols[0].trim(); Long population = Long.parseLong(cols[1].trim()); if (entries.containsKey(zipcode)) { entries.put(zipcode, Math.max(entries.get(zipcode), population)); } else { entries.put(zipcode, population); } } scanner.close(); return ImmutableMap.copyOf(entries); }
From source file:com.joliciel.jochre.lexicon.LexiconErrorWriter.java
static void mergeCrossValidation(File evalDir, String prefix) { try {//from w ww . j a v a 2 s .c o m File[] files = evalDir.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { if (name.endsWith(".csv")) return true; else return false; } }); List<String> groupNames = new ArrayList<String>(); Map<String, Writer> writers = new HashMap<String, Writer>(); Map<String, ErrorStatistics> errorMap = new LinkedHashMap<String, ErrorStatistics>(); Map<String, Map<String, DescriptiveStatistics>> statMap = new HashMap<String, Map<String, DescriptiveStatistics>>(); for (File file : files) { String filename = file.getName(); LOG.debug("Processing " + filename); int index = Integer.parseInt(filename.substring(prefix.length(), prefix.length() + 1)); String suffix = filename.substring(prefix.length() + 2, filename.lastIndexOf('_')); String fileType = filename.substring(filename.lastIndexOf('_') + 1, filename.lastIndexOf('.')); LOG.debug("Processing " + filename); LOG.debug("index: " + index); LOG.debug("suffix: " + suffix); LOG.debug("fileType: " + fileType); Writer writer = writers.get(fileType); boolean firstFile = false; if (writer == null) { writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream( new File(evalDir, prefix + "A_" + suffix + "_" + fileType + ".csv"), false), "UTF8")); writers.put(fileType, writer); firstFile = true; } if (fileType.equals("KEMatrix")) { Scanner scanner = new Scanner(file); int i = 0; List<String> myGroupNames = new ArrayList<String>(); Map<String, Boolean> haveCountMap = new HashMap<String, Boolean>(); while (scanner.hasNextLine()) { String line = scanner.nextLine(); List<String> cells = CSV.getCSVCells(line); if (i == 0) { for (int j = 0; j < cells.size(); j += 5) { String groupName = cells.get(j); if (!errorMap.containsKey(groupName)) { errorMap.put(groupName, new ErrorStatistics()); statMap.put(groupName, new HashMap<String, DescriptiveStatistics>()); groupNames.add(groupName); } myGroupNames.add(groupName); } } else if (i == 1) { // do nothing } else { String rowName = cells.get(0); int j = 0; for (String groupName : myGroupNames) { ErrorStatistics errorStats = errorMap.get(groupName); Map<String, DescriptiveStatistics> stats = statMap.get(groupName); double correctCount = Double.parseDouble(cells.get(j * 5 + 1)); double errorCount = Double.parseDouble(cells.get(j * 5 + 2)); double totalCount = Double.parseDouble(cells.get(j * 5 + 3)); Boolean haveCount = haveCountMap.get(groupName); if (rowName.equals("known")) { errorStats.knownWordCorrectCount += correctCount; errorStats.knownWordErrorCount += errorCount; } else if (rowName.equals("unknown")) { errorStats.unknownWordCorrectCount += correctCount; errorStats.unknownWordErrorCount += errorCount; } else if (rowName.equals("goodSeg")) { errorStats.goodSegCorrectCount += correctCount; errorStats.goodSegErrorCount += errorCount; } else if (rowName.equals("badSeg")) { errorStats.badSegCorrectCount += correctCount; errorStats.badSegErrorCount += errorCount; } else if (rowName.equals("knownLetters")) { errorStats.knownWordCorrectLetterCount += correctCount; errorStats.knownWordErrorLetterCount += errorCount; } else if (rowName.equals("unknownLetters")) { errorStats.unknownWordCorrectLetterCount += correctCount; errorStats.unknownWordErrorLetterCount += errorCount; } else if (rowName.equals("goodSegLetters")) { errorStats.goodSegCorrectLetterCount += correctCount; errorStats.goodSegErrorLetterCount += errorCount; } else if (rowName.equals("badSegLetters")) { errorStats.badSegCorrectLetterCount += correctCount; errorStats.badSegErrorLetterCount += errorCount; } else if (rowName.equals("inBeam")) { errorStats.answerInBeamCorrectCount += correctCount; errorStats.answerInBeamErrorCount += errorCount; } else if (rowName.equals("total")) { haveCountMap.put(groupName, totalCount > 0); } else if (rowName.endsWith("%")) { if (haveCount) { String keyPrefix = rowName.substring(0, rowName.length() - 1); String key = keyPrefix + "|correct"; DescriptiveStatistics correctStat = stats.get(key); if (correctStat == null) { correctStat = new DescriptiveStatistics(); stats.put(key, correctStat); } correctStat.addValue(correctCount); key = keyPrefix + "|error"; DescriptiveStatistics errorStat = stats.get(key); if (errorStat == null) { errorStat = new DescriptiveStatistics(); stats.put(key, errorStat); } errorStat.addValue(errorCount); key = keyPrefix + "|total"; DescriptiveStatistics totalStat = stats.get(key); if (totalStat == null) { totalStat = new DescriptiveStatistics(); stats.put(key, totalStat); } totalStat.addValue(totalCount); } } j++; } } i++; } } else { Scanner scanner = new Scanner(file); boolean firstLine = true; while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (firstLine) { if (firstFile) writer.write(line + "\n"); firstLine = false; } else { writer.write(line + "\n"); } writer.flush(); } } // file type } // next file Writer statsWriter = writers.get("KEMatrix"); writeStats(statsWriter, errorMap); statsWriter.write("\n"); String[] statTypes = new String[] { "known", "unknown", "goodSeg", "badSeg", "inBeam", "total", "knownLetter", "unknownLetter", "goodSegLetter", "badSegLetter", "totalLetter" }; for (String statType : statTypes) { for (String groupName : groupNames) { Map<String, DescriptiveStatistics> statsMap = statMap.get(groupName); DescriptiveStatistics correctStat = statsMap.get(statType + "|correct"); DescriptiveStatistics errorStat = statsMap.get(statType + "|error"); DescriptiveStatistics totalStat = statsMap.get(statType + "|total"); statsWriter.write(CSV.format(statType + "%Avg") + CSV.format(correctStat.getMean()) + CSV.format(errorStat.getMean()) + CSV.format(totalStat.getMean()) + CSV.getCsvSeparator()); } // next group statsWriter.write("\n"); for (String groupName : groupNames) { Map<String, DescriptiveStatistics> statsMap = statMap.get(groupName); DescriptiveStatistics correctStat = statsMap.get(statType + "|correct"); DescriptiveStatistics errorStat = statsMap.get(statType + "|error"); DescriptiveStatistics totalStat = statsMap.get(statType + "|total"); statsWriter.write(CSV.format(statType + "%Dev") + CSV.format(correctStat.getStandardDeviation()) + CSV.format(errorStat.getStandardDeviation()) + CSV.format(totalStat.getStandardDeviation()) + CSV.getCsvSeparator()); } // next group statsWriter.write("\n"); statsWriter.flush(); } statsWriter.close(); } catch (IOException e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } }
From source file:ir.ac.ut.snl.mrcd.InputConverter.java
public void convert(String filename) throws FileNotFoundException, UnsupportedEncodingException { int paddingSize = 49; // 50-1; 1 baraye '\n' File file = new File(filename); FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); Scanner scanner = new Scanner(bufferedReader); PrintWriter printWriter = new PrintWriter(filename + "-converted", "UTF-8"); int n = scanner.nextInt(); scanner.nextLine();// w w w . ja v a 2 s . co m printWriter.write(StringUtils.leftPad(String.valueOf(n), paddingSize)); printWriter.write('\n'); while (scanner.hasNextLine()) { String line = scanner.nextLine(); printWriter.write(StringUtils.leftPad(line, paddingSize)); printWriter.write('\n'); } scanner.close(); printWriter.close(); }
From source file:imageClassify.TestForest.java
private void testFile(Path inPath, Path outPath, DataConverter converter, DecisionForest forest, Dataset dataset, Collection<double[]> results, Random rng) throws IOException { // create the predictions file FSDataOutputStream ofile = null;//from w w w .java2s . c o m if (outPath != null) { ofile = outFS.create(outPath); } FSDataInputStream input = dataFS.open(inPath); try { Scanner scanner = new Scanner(input, "UTF-8"); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.isEmpty()) { continue; // skip empty lines } Instance instance = converter.convert(line); double prediction = forest.classify(dataset, rng, instance); if (ofile != null) { ofile.writeChars(Double.toString(prediction)); // write the prediction ofile.writeChar('\n'); } results.add(new double[] { dataset.getLabel(instance), prediction }); } scanner.close(); } finally { Closeables.close(input, true); } }
From source file:org.apache.hadoop.hdfs.tools.TestDFSAdmin.java
private void reconfigurationOutErrFormatter(String methodName, String nodeType, String address, final List<String> outs, final List<String> errs) throws IOException { ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); PrintStream out = new PrintStream(bufOut); ByteArrayOutputStream bufErr = new ByteArrayOutputStream(); PrintStream err = new PrintStream(bufErr); if (methodName.equals("getReconfigurableProperties")) { admin.getReconfigurableProperties(nodeType, address, out, err); } else if (methodName.equals("getReconfigurationStatus")) { admin.getReconfigurationStatus(nodeType, address, out, err); } else if (methodName.equals("startReconfiguration")) { admin.startReconfiguration(nodeType, address, out, err); }/*from w ww . j a v a 2 s. co m*/ Scanner scanner = new Scanner(bufOut.toString()); while (scanner.hasNextLine()) { outs.add(scanner.nextLine()); } scanner.close(); scanner = new Scanner(bufErr.toString()); while (scanner.hasNextLine()) { errs.add(scanner.nextLine()); } scanner.close(); }
From source file:com.maxl.java.aips2xml.Aips2Xml.java
static String convertHtmlToXml(String med_title, String html_str, String regnr_str) { Document mDoc = Jsoup.parse(html_str); mDoc.outputSettings().escapeMode(EscapeMode.xhtml); mDoc.outputSettings().prettyPrint(true); mDoc.outputSettings().indentAmount(4); // <div id="monographie"> -> <fi> mDoc.select("div[id=monographie]").tagName("fi").removeAttr("id"); // <div class="MonTitle"> -> <title> mDoc.select("div[class=MonTitle]").tagName("title").removeAttr("class").removeAttr("id"); // Beautify the title to the best of my possibilities ... still not good enough! String title_str = mDoc.select("title").text().trim().replaceAll("<br />", "").replaceAll("(\\t|\\r?\\n)+", "");// w w w . j a va 2 s.c om if (!title_str.equals(med_title)) if (SHOW_ERRORS) System.err.println(med_title + " differs from " + title_str); // Fallback solution: use title from the header AIPS.xml file - the titles look all pretty good! mDoc.select("title").first().text(med_title); // <div class="ownerCompany"> -> <owner> Element owner_elem = mDoc.select("div[class=ownerCompany]").first(); if (owner_elem != null) { owner_elem.tagName("owner").removeAttr("class"); String owner_str = mDoc.select("owner").text(); mDoc.select("owner").first().text(owner_str); } else { mDoc.select("title").after("<owner></owner>"); if (DB_LANGUAGE.equals("de")) mDoc.select("owner").first().text("k.A."); else if (DB_LANGUAGE.equals("fr")) mDoc.select("owner").first().text("n.s."); } // <div class="paragraph"> -> <paragraph> mDoc.select("div[class=paragraph]").tagName("paragraph").removeAttr("class").removeAttr("id"); // <div class="absTitle"> -> <paragraphTitle> mDoc.select("div[class=absTitle]").tagName("paragraphtitle").removeAttr("class"); // <div class="untertitle1"> -> <paragraphSubTitle> mDoc.select("div[class=untertitle1]").tagName("paragraphsubtitle").removeAttr("class"); // <div class="untertitle"> -> <paragraphSubTitle> mDoc.select("div[class=untertitle]").tagName("paragraphsubtitle").removeAttr("class"); // <div class="shortCharacteristic"> -> <characteristic> mDoc.select("div[class=shortCharacteristic]").tagName("characteristic").removeAttr("class"); // <div class="image"> mDoc.select("div[class=image]").tagName("image").removeAttr("class"); // <p class="spacing1"> -> <p> / <p class="noSpacing"> -> <p> mDoc.select("p[class]").tagName("p").removeAttr("class"); // <span style="font-style:italic"> -> <i> mDoc.select("span").tagName("i").removeAttr("style"); // <i class="indention1"> -> <i> / <i class="indention2"> -> <b-i> mDoc.select("i[class=indention1]").tagName("i").removeAttr("class"); mDoc.select("i[class=indention2]").tagName("i").removeAttr("class"); // mDoc.select("p").select("i").tagName("i"); // mDoc.select("paragraphtitle").select("i").tagName("para-i"); // mDoc.select("paragraphsubtitle").select("i").tagName("parasub-i"); Elements elems = mDoc.select("paragraphtitle"); for (Element e : elems) { if (!e.text().isEmpty()) e.text(e.text()); } elems = mDoc.select("paragraphsubtitle"); for (Element e : elems) { if (!e.text().isEmpty()) e.text(e.text()); } // Here we take care of tables // <table class="s21"> -> <table> mDoc.select("table[class]").removeAttr("class"); mDoc.select("table").removeAttr("cellspacing").removeAttr("cellpadding").removeAttr("border"); mDoc.select("colgroup").remove(); mDoc.select("td").removeAttr("class").removeAttr("colspan").removeAttr("rowspan"); mDoc.select("tr").removeAttr("class"); elems = mDoc.select("div[class]"); for (Element e : elems) { if (e.text().isEmpty()) e.remove(); } mDoc.select("tbody").unwrap(); // Remove nested table (a nasty table-in-a-table Elements nested_table = mDoc.select("table").select("tr").select("td").select("table"); if (!nested_table.isEmpty()) { nested_table.select("table").unwrap(); } // Here we take care of the images mDoc.select("img").removeAttr("style").removeAttr("align").removeAttr("border"); // Subs and sups mDoc.select("sub[class]").tagName("sub").removeAttr("class"); mDoc.select("sup[class]").tagName("sup").removeAttr("class"); mDoc.select("td").select("sub").tagName("td-sub"); mDoc.select("td").select("sup").tagName("td-sup"); // Remove floating <td-sup> tags mDoc.select("p").select("td-sup").tagName("sup"); mDoc.select("p").select("td-sub").tagName("sub"); // Box mDoc.select("div[class=box]").tagName("box").removeAttr("class"); // Insert swissmedicno5 after <owner> tag mDoc.select("owner").after("<swissmedicno5></swissmedicno5"); mDoc.select("swissmedicno5").first().text(regnr_str); // Remove html, head and body tags String xml_str = mDoc.select("body").first().html(); //xml_str = xml_str.replaceAll("<tbody>", "").replaceAll("</tbody>", ""); xml_str = xml_str.replaceAll("<sup> </sup>", ""); xml_str = xml_str.replaceAll("<sub> </sub>", ""); xml_str = xml_str.replaceAll("<p> <i>", "<p><i>"); xml_str = xml_str.replaceAll("</p> </td>", "</p></td>"); xml_str = xml_str.replaceAll("<p> </p>", "<p></p>"); // MUST be improved, the space is not a real space!! xml_str = xml_str.replaceAll("", "- "); xml_str = xml_str.replaceAll("<br />", ""); xml_str = xml_str.replaceAll("(?m)^[ \t]*\r?\n", ""); // Remove multiple instances of <p></p> Scanner scanner = new Scanner(xml_str); String new_xml_str = ""; int counter = 0; while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.trim().equals("<p></p>")) { counter++; } else counter = 0; if (counter < 3) new_xml_str += line; } scanner.close(); return new_xml_str; }
From source file:edu.utah.further.core.util.text.LineListFileScanner.java
/** * Template method that parses an entire input from a scanner. * * @param scanner// ww w. j a v a2s. c o m * provides input * @param lineScanner * scans each line into an entity * @return list of entities, one per line */ @Override protected List<T> parseInput(final Scanner scanner) { final List<T> entities = CollectionUtil.newList(); int lineNumber = 0; String line = null; try { while (scanner.hasNextLine()) { lineNumber++; line = scanner.nextLine(); if (isIgnoreBlankLines() && StringUtils.isBlank(line)) { if (log.isDebugEnabled()) { log.debug("Ignoring blank line " + lineNumber); } } else { if (log.isDebugEnabled()) { log.debug("Parsing line " + lineNumber + ": " + quote(line)); } final String filteredLine = filterLine(line); entities.add(getLineScanner().parse(filteredLine)); } } } catch (final Exception e) { throw new ApplicationException("Failed to parse file at line " + lineNumber + ": " + quote(line), e); } finally { // Ensure the underlying stream is always closed scanner.close(); } return entities; }
From source file:org.sasabus.export2Freegis.network.DataReadyManager.java
@Override public void handle(HttpExchange httpExchange) throws IOException { long start = System.currentTimeMillis(); BufferedReader in = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody(), "UTF-8")); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(httpExchange.getResponseBody())); try {//w w w .ja v a 2 s . c o m StringBuilder requestStringBuff = new StringBuilder(); int b; while ((b = in.read()) != -1) { requestStringBuff.append((char) b); } Scanner sc = new Scanner(new File(DATAACKNOWLEDGE)); String rdyackstring = ""; while (sc.hasNextLine()) { rdyackstring += sc.nextLine(); } sc.close(); SimpleDateFormat date_date = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat date_time = new SimpleDateFormat("HH:mm:ssZ"); Date d = new Date(); String timestamp = date_date.format(d) + "T" + date_time.format(d); timestamp = timestamp.substring(0, timestamp.length() - 2) + ":" + timestamp.substring(timestamp.length() - 2); rdyackstring = rdyackstring.replaceAll(":timestamp", timestamp); httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, rdyackstring.length()); out.write(rdyackstring); out.flush(); long before_elab = System.currentTimeMillis() - start; DataRequestManager teqrequest = new DataRequestManager(this.hostname, this.portnumber); String datarequest = teqrequest.datarequest(); ArrayList<TeqObjects> requestelements = TeqXMLUtils.extractFromXML(datarequest); int vtcounter = 0; if (!requestelements.isEmpty()) { Iterator<TeqObjects> iterator = requestelements.iterator(); System.out.println("Sending List of Elements!"); String geoJson = "{\"type\":\"FeatureCollection\",\"features\":["; while (iterator.hasNext()) { TeqObjects object = iterator.next(); if (object instanceof VehicleTracking) { geoJson += ((VehicleTracking) object).toGeoJson() + ","; ++vtcounter; } } if (geoJson.charAt(geoJson.length() - 1) == ',') { geoJson = geoJson.substring(0, geoJson.length() - 1); } geoJson += "]}"; System.out.println( "GeoJson sent! (Nr of elements: " + vtcounter + "/" + requestelements.size() + " )"); HttpPost subrequest = new HttpPost(DATASEND); StringEntity requestEntity = new StringEntity(geoJson, ContentType.create("application/json", "UTF-8")); CloseableHttpClient httpClient = HttpClients.createDefault(); subrequest.setEntity(requestEntity); long after_elab = System.currentTimeMillis() - start; CloseableHttpResponse response = httpClient.execute(subrequest); //System.out.println("Stauts JsonSend Response: " + response.getStatusLine().getStatusCode()); //System.out.println("Status JsonSend Phrase: " + response.getStatusLine().getReasonPhrase()); httpClient.close(); long before_db = System.currentTimeMillis() - start; dbmanager.insertIntoDatabase(requestelements); System.out.format( "Thread time (ms) : Before elab: %d, After Elab (before http sending): %d, Before db: %d, Total: %d, Thread name: %s, Objects elaborated: %d", before_elab, after_elab, before_db, (System.currentTimeMillis() - start), Thread.currentThread().getName(), requestelements.size()); System.out.println(""); } } catch (IOException e) { System.err.println(e.getMessage()); e.printStackTrace(); } catch (Exception e) { System.err.println(e.getMessage()); e.printStackTrace(); System.exit(-1); return; } finally { if (in != null) in.close(); if (out != null) out.close(); if (httpExchange != null) httpExchange.close(); } }
From source file:com.gnadenheimer.mg3.controller.admin.AdminConfigController.java
@FXML private void cmdUpdateSET(ActionEvent event) { Task taskUpdateSET = new Task<Void>() { @Override//from www . j a va 2 s . c o m public Void call() { try { EntityManager entityManager = Utils.getInstance().getEntityManagerFactory() .createEntityManager(); entityManager.getTransaction().begin(); String temp = ""; Integer count = 0; entityManager.createQuery("delete from TblContribuyentes t").executeUpdate(); for (Integer i = 0; i <= 9; i++) { URL url = new URL( "http://www.set.gov.py/rest/contents/download/collaboration/sites/PARAGUAY-SET/documents/informes-periodicos/ruc/ruc" + String.valueOf(i) + ".zip"); ZipInputStream zipStream = new ZipInputStream(url.openStream(), StandardCharsets.UTF_8); zipStream.getNextEntry(); Scanner sc = new Scanner(zipStream, "UTF-8"); while (sc.hasNextLine()) { String[] ruc = sc.nextLine().split("\\|"); temp = ruc[0] + " - " + ruc[1] + " - " + ruc[2]; if (ruc[0].length() > 0 && ruc[1].length() > 0 && ruc[2].length() == 1) { TblContribuyentes c = new TblContribuyentes(); c.setRucSinDv(ruc[0]); c.setRazonSocial(StringEscapeUtils.escapeSql(ruc[1])); c.setDv(ruc[2]); entityManager.persist(c); updateMessage("Descargando listado de RUC con terminacion " + String.valueOf(i) + " - Cantidad de contribuyentes procesada: " + String.format("%,d", count) + " de aprox. 850.000."); count++; } else { updateMessage(temp); } } entityManager.getTransaction().commit(); entityManager.getTransaction().begin(); } updateMessage("Lista de RUC actualizada..."); return null; } catch (Exception ex) { App.showException(this.getClass().getName(), ex.getMessage(), ex); } return null; } }; lblUpdateSET.textProperty().bind(taskUpdateSET.messageProperty()); new Thread(taskUpdateSET).start(); }