List of usage examples for java.util Scanner nextLine
public String nextLine()
From source file:com.joliciel.jochre.lexicon.LexiconErrorWriter.java
static void mergeCrossValidation(File evalDir, String prefix) { try {/* w w w. j a v a 2s . c om*/ 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: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 {/* www.j av a 2s . com*/ 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:net.k3rnel.arena.server.GameServer.java
/** * Load pre-existing settings if any// w w w . ja v a 2 s . co m * NOTE: It loads the database password if available. * Password is line after serverName */ private void loadSettings() { System.out.println("Loading settings..."); File foo = new File("res/settings.txt"); if (foo.exists()) { try { Scanner s = new Scanner(foo); m_dbServer = s.nextLine(); m_dbName = s.nextLine(); m_dbUsername = s.nextLine(); m_serverName = s.nextLine(); m_dbPassword = s.nextLine(); if (m_dbPassword.isEmpty() || m_dbPassword.equals(" ")) { ConsoleReader r = new ConsoleReader(); System.out.println("Please enter the required information."); System.out.println("Database Password: "); m_dbPassword = r.readToken(); } s.close(); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("Settings not found."); getConsoleSettings(); } }
From source file:com.mapr.db.utils.ImportCSV.java
public void readAndImportCSV(String path, String delimiter) { String dataLine = ""; String[] dataList;/*from w ww. ja v a 2 s . com*/ String data = ""; ArrayList<Object> values = new ArrayList<>(); int countColumnsInData = 0; try { Scanner scan = new Scanner(new FileReader(path)); while (scan.hasNextLine()) { countColumnsInData = 0; dataLine = scan.nextLine().trim(); //System.out.println(dataLine); if (dataLine.endsWith(delimiter)) { dataLine = dataLine.substring(0, dataLine.length() - 1); } dataList = StringUtils.splitPreserveAllTokens(dataLine, delimiter); for (int i = 0; i < dataList.length; i++) { data = dataList[i]; if (data.isEmpty()) { if (valueTypesInSchema.get(i) == "String") { data = "null"; } else { data = "0"; } } switch (valueTypesInSchema.get(i).toLowerCase()) { case "int": case "integer": case "bigint": case "long": values.add(countColumnsInData, Long.valueOf(data)); break; case "float": case "double": values.add(countColumnsInData, Double.valueOf(data)); break; case "date": values.add(countColumnsInData, Date.valueOf(data)); break; default: values.add(countColumnsInData, String.valueOf(data)); break; } //System.out.println("Inserting " + values.get(countColumnsInData) // + " into column " + columnNamesInSchema.get(countColumnsInData)); countColumnsInData++; } insertDocument(values, countColumnsInData, maprdbTable, maprdbTablePath); } scan.close(); } catch (Exception e) { System.out.println("Error importing text:\n\t" + dataLine + "\ninto\n\t" + maprdbTablePath); e.printStackTrace(); System.exit(-1); } }
From source file:com.github.matthesrieke.realty.CrawlerServlet.java
private void readCrawlingLinks() { InputStream is = getClass().getResourceAsStream("/links.txt"); if (is != null) { Scanner sc = new Scanner(is); String l;//from w w w . jav a 2 s . com while (sc.hasNext()) { l = sc.nextLine().trim(); if (!l.startsWith("#")) { this.crawlLinks.add(l); } } sc.close(); } }
From source file:com.mucommander.job.FindFileJob.java
private boolean fileContainsString0(AbstractFile f) { //Profiler.start("check_old"); if (fileContent == null || fileContent.isEmpty()) { return true; }/* w w w . ja v a2s. c om*/ 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:com.gnadenheimer.mg3.controller.admin.AdminConfigController.java
@FXML private void cmdUpdateSET(ActionEvent event) { Task taskUpdateSET = new Task<Void>() { @Override/*w ww . ja v a 2s .c om*/ 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(); }
From source file:cpd3314.project.CPD3314ProjectTest.java
private void assertFilesEqual(File a, File b) throws IOException { Scanner aIn = new Scanner(a); Scanner bIn = new Scanner(b); while (aIn.hasNext() && bIn.hasNext()) { assertEquals(aIn.nextLine().trim(), bIn.nextLine().trim()); }/*w w w . j a v a 2 s . c om*/ assertTrue("Files Not Equal Length", !aIn.hasNext() && !bIn.hasNext()); }
From source file:edu.clemson.cs.nestbed.server.management.instrumentation.ProgramWeaverManagerImpl.java
public String findComponentFromMakefile(File makefile) throws RemoteException { String component = null;/*w ww . j av a 2 s . c om*/ Scanner scanner = null; try { log.debug("Makefile: " + makefile); scanner = new Scanner(makefile); while (scanner.hasNext() && component == null) { String line = scanner.nextLine(); String regExp = "^COMPONENT=(\\S+)"; Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(line); if (matcher.find()) { component = matcher.group(1); } } } catch (FileNotFoundException fnfe) { throw new RemoteException("FileNotFoundException", fnfe); } finally { try { scanner.close(); } catch (Exception ex) { /* empty */ } } if (component == null) { // FIXME: Shouldn't be throwing generic Exceptions throw new RemoteException("No main component found in Makefile."); } return component; }
From source file:edu.harvard.med.iccbl.screensaver.io.users.UserAgreementExpirationUpdater.java
/** * Return the subject first and the message second. * Message:/* w ww . ja v a 2s .com*/ * {0} Expiration Date * * @throws MessagingException */ private Pair<String, String> getExpireNotificationSubjectMessage() throws MessagingException { InputStream in = null; if (isCommandLineFlagSet(EXPIRATION_EMAIL_MESSAGE_LOCATION[SHORT_OPTION_INDEX])) { try { in = new FileInputStream( new File(getCommandLineOptionValue(EXPIRATION_EMAIL_MESSAGE_LOCATION[SHORT_OPTION_INDEX]))); } catch (FileNotFoundException e) { sendErrorMail( "Operation not completed for UserAgreementExpirationUpdater, could not locate expiration message", toString(), e); throw new DAOTransactionRollbackException(e); } } else { in = this.getClass().getResourceAsStream(EXPIRATION_MESSAGE_TXT_LOCATION); } Scanner scanner = new Scanner(in); try { StringBuilder builder = new StringBuilder(); String subject = scanner.nextLine(); while (scanner.hasNextLine()) { builder.append(scanner.nextLine()).append("\n"); } return Pair.newPair(subject, builder.toString()); } finally { scanner.close(); } }