List of usage examples for java.util Scanner close
public void close()
From source file:com.rockhoppertech.music.DurationParser.java
/** * Copies new events into the TimeSeries parameter - which is also returned. * /*from ww w.j a v a 2 s . co m*/ * @param ts * a {@code TimeSeries} that will be added to * @param s * a duration string * @return the same{@code TimeSeries} that you passed in */ public static TimeSeries getDurationAsTimeSeries(TimeSeries ts, String s) { String token = null; Scanner scanner = new Scanner(s); if (s.indexOf(',') != -1) { scanner.useDelimiter(","); } while (scanner.hasNext()) { if (scanner.hasNext(dpattern)) { token = scanner.next(dpattern); double d = getDottedValue(token); ts.add(d); logger.debug("'{}' is dotted value is {}", token, d); } else if (scanner.hasNext(pattern)) { token = scanner.next(pattern); double d = durKeyMap.get(token); ts.add(d); logger.debug("'{}' is not dotted value is {}", token, d); // } else if (scanner.hasNext(tripletPattern)) { // token = scanner.next(tripletPattern); // double d = durKeyMap.get(token); // ts.add(d); // System.out.println(String // .format("'%s' is not dotted value is %f", // token, // d)); } else if (scanner.hasNextDouble()) { double d = scanner.nextDouble(); ts.add(d); logger.debug("{} is a double", d); } else { // just ignore it. or throw exception? String skipped = scanner.next(); logger.debug("skipped '{}'", skipped); } } scanner.close(); return ts; }
From source file:com.googlecode.esms.provider.Tim.java
/** * Search for a pattern inside a stream. * @param source The stream to search into. * @param pattern The pattern to match./*w w w. j ava 2s . c o m*/ * @return List of matches found. */ private List<String> findPattern(InputStream source, Pattern pattern) { List<String> results = new ArrayList<String>(); Scanner scanner = new Scanner(source, "UTF-8"); String match = ""; while (match != null) { match = scanner.findWithinHorizon(pattern, 0); if (match != null) results.add(match); } scanner.close(); return results; }
From source file:de.uzk.hki.da.grid.IrodsCommandLineConnector.java
/** * Gets checksum from ICAT for the newest instance destDao * @author Jens Peters//from w w w . j av a 2 s . co m * @param destDao * @return */ public String getChecksumOfLatestReplica(String destDao) { String ret = ""; String commandAsArray[] = new String[] { "ils -L", destDao }; String out = executeIcommand(commandAsArray); if (out.indexOf("ERROR") >= 0) throw new RuntimeException(" Get Checksum of " + destDao + " failed !"); Scanner scanner = new Scanner(out); String data_name = FilenameUtils.getName(destDao); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.contains(data_name)) { ret = line.substring(38, line.length()); } } scanner.close(); return ret; }
From source file:org.apache.taverna.activities.rest.ui.config.RESTActivityConfigurationPanel.java
private String[] getMediaTypes() { if (mediaTypes != null) { return mediaTypes; }//from w w w. j a v a 2 s .c om List<String> types = new ArrayList<String>(); InputStream typesStream = getClass().getResourceAsStream("mediatypes.txt"); try { // media types must be ASCII and can't have whitespace Scanner scanner = new Scanner(typesStream, "ascii"); while (scanner.hasNext()) { types.add(scanner.next()); } scanner.close(); } finally { try { typesStream.close(); } catch (IOException ex) { } } mediaTypes = types.toArray(new String[0]); return mediaTypes; }
From source file:net.sf.jaceko.mock.util.FileReader.java
public String readFileContents(final String fileName) { final StringBuilder text = new StringBuilder(); final String newLine = System.getProperty("line.separator"); Scanner scanner = null; try {//from w w w. j a v a 2 s.c om final InputStream resourceAsStream = FileReader.class.getClassLoader().getResourceAsStream(fileName); if (resourceAsStream == null) { LOG.error("File not found: " + fileName); return null; } else { LOG.info(fileName + " found in classpath"); } scanner = new Scanner(resourceAsStream); while (scanner.hasNextLine()) { text.append(scanner.nextLine() + newLine); } } catch (final Exception e) { LOG.error("Problem reading file : " + fileName, e); return null; } finally { if (scanner != null) { scanner.close(); } } return text.toString(); }
From source file:com.rockhoppertech.music.DurationParser.java
public static List<Double> getDurationsAsList(String durations) { String token = null;/* w w w . ja v a 2 s.c o m*/ List<Double> list = new ArrayList<Double>(); Scanner scanner = new Scanner(durations); if (durations.indexOf(',') != -1) { scanner.useDelimiter(","); } while (scanner.hasNext()) { if (scanner.hasNext(dpattern)) { token = scanner.next(dpattern); double d = getDottedValue(token); list.add(d); logger.debug("'{}' is dotted value is {}", token, d); } else if (scanner.hasNext(pattern)) { token = scanner.next(pattern); double d = durKeyMap.get(token); list.add(d); logger.debug("'{}' is not dotted value is {}", token, d); // } else if (scanner.hasNext(tripletPattern)) { // token = scanner.next(tripletPattern); // double d = durKeyMap.get(token); // ts.add(d); // System.out.println(String // .format("'%s' is not dotted value is %f", // token, // d)); } else if (scanner.hasNextDouble()) { double d = scanner.nextDouble(); list.add(d); logger.debug("{} is a double", d); } else { // just ignore it. or throw exception? String skipped = scanner.next(); logger.debug("skipped '{}'", skipped); } } scanner.close(); return list; }
From source file:com.tibco.tgdb.test.lib.TGAdmin.java
/** * Get connections for a given user. //from w ww . j a v a 2 s . c o m * Operation blocks until it is completed. * * @param tgServer TG server to get connections from * @param tgNetListenerName Name of the net listener for TG Admin to connect to - if null connect to 1st one * @param user User name to get the connections from * @param logFile TG admin log file location - Generated by admin * @param logLevel Specify the log level: info/user1/user2/user3/debug/debugmemory/debugwire * @param timeout Number of milliseconds allowed to complete the stop server operation - If lower than 0 wait forever * * @return Array of session IDs belonging to the user * @throws TGAdminException Admin execution fails or timeout occurs */ public static String[] getConnectionsByUser(TGServer tgServer, String tgNetListenerName, String user, String logFile, String logLevel, long timeout) throws TGAdminException { String output = showConnections(tgServer, tgNetListenerName, logFile, logLevel, timeout); Scanner scanner = new Scanner(output); boolean counting = false; //int indexClientId; int indexSessionId = 0; //int adminConnectionCount = 0; List<String> connectionList = new ArrayList<String>(); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.contains("Client ID")) { counting = true; //indexClientId = line.indexOf("Client ID"); indexSessionId = line.indexOf("Session ID"); } else if (line.contains("----------")) counting = false; else if (line.contains("connection(s) returned")) { counting = false; //adminConnectionCount = Integer.parseInt(line.substring(0, line.indexOf(' ', 0))); } else if (counting) { if (line.contains(" " + user + " ")) connectionList.add(line.substring(indexSessionId, line.indexOf(' ', indexSessionId))); } } scanner.close(); //if (connectionList.size() != adminConnectionCount) // throw new TGAdminException("TGAdmin - Not able to determine number of connections"); return connectionList.toArray(new String[0]); }
From source file:org.apache.asterix.experiment.builder.AbstractLSMBaseExperimentBuilder.java
protected Map<String, List<String>> readDatagenPairs(Path p) throws IOException { Map<String, List<String>> dgenPairs = new HashMap<>(); Scanner s = new Scanner(p, StandardCharsets.UTF_8.name()); try {//w ww . j a v a2 s. c o m while (s.hasNextLine()) { String line = s.nextLine(); String[] pair = line.split("\\s+"); List<String> vals = dgenPairs.get(pair[0]); if (vals == null) { vals = new ArrayList<>(); dgenPairs.put(pair[0], vals); } vals.add(pair[1]); } } finally { s.close(); } return dgenPairs; }
From source file:edu.cmu.lti.oaqa.framework.eval.gs.KeytermGoldStandardFilePersistenceProvider.java
@Override public boolean initialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams) throws ResourceInitializationException { boolean ret = super.initialize(aSpecifier, aAdditionalParams); String dataset = (String) getParameterValue("DataSet"); Pattern lineSyntaxPattern = Pattern.compile((String) getParameterValue("LineSyntax")); try {// w ww . ja v a 2 s .c om Resource[] resources = resolver.getResources((String) getParameterValue("PathPattern")); for (Resource resource : resources) { Scanner scanner = new Scanner(resource.getInputStream()); while (scanner.findInLine(lineSyntaxPattern) != null) { MatchResult result = scanner.match(); DatasetSequenceId id = new DatasetSequenceId(dataset, result.group(1)); if (!id2gsSpans.containsKey(id)) { id2gsSpans.put(id, new ArrayList<String>()); } id2gsSpans.get(id).add(result.group(4)); if (scanner.hasNextLine()) { scanner.nextLine(); } else { break; } } scanner.close(); } } catch (IOException e) { e.printStackTrace(); } return ret; }
From source file:com.mapr.db.utils.ImportTPCHJSONFiles.java
public void readFileAndWriteToTable(String maprdbJsonTableName, String maprdbJsonTablePath, String jsonFilePath) {/*from w w w .ja va2 s . c o m*/ System.out.println("Importing " + maprdbJsonTableName); try { Scanner scan = new Scanner(new FileReader(jsonFilePath)); StringBuilder jsonFileContents = new StringBuilder(); while (scan.hasNextLine()) { jsonFileContents.append(scan.nextLine()); } scan.close(); StringTokenizer st = new StringTokenizer(jsonFileContents.toString(), "%"); String record = ""; while (st.hasMoreTokens()) { record = st.nextToken(); //System.out.println(record); JSONParser parser = new JSONParser(); try { Object obj = parser.parse(record); JSONObject jsonObject = (JSONObject) obj; if (maprdbJsonTableName.equalsIgnoreCase("Customer")) { c = new Customer(); c = c.getDocument(jsonObject); //System.out.println(c.toString()); c_table = this.getTable(c_table, maprdbJsonTablePath); c.insertDocument(c_table); } else if (maprdbJsonTableName.equalsIgnoreCase("Lineitem")) { l = new Lineitem(); l = l.getDocument(jsonObject); //System.out.println(l.toString()); l_table = this.getTable(l_table, maprdbJsonTablePath); l.insertDocument(l_table); } else if (maprdbJsonTableName.equalsIgnoreCase("Orders")) { o = new Orders(); o = o.getDocument(jsonObject); //System.out.println(o.toString()); o_table = this.getTable(o_table, maprdbJsonTablePath); o.insertDocument(o_table); } else if (maprdbJsonTableName.equalsIgnoreCase("Part")) { p = new Part(); p = p.getDocument(jsonObject); //System.out.println(p.toString()); p_table = this.getTable(p_table, maprdbJsonTablePath); p.insertDocument(p_table); } else if (maprdbJsonTableName.equalsIgnoreCase("Partsupp")) { ps = new Partsupp(); ps = ps.getDocument(jsonObject); //System.out.println(ps.toString()); ps_table = this.getTable(ps_table, maprdbJsonTablePath); ps.insertDocument(ps_table); } else if (maprdbJsonTableName.equalsIgnoreCase("Nation")) { n = new Nation(); n = n.getDocument(jsonObject); //System.out.println(n.toString()); n_table = this.getTable(n_table, maprdbJsonTablePath); n.insertDocument(n_table); } else if (maprdbJsonTableName.equalsIgnoreCase("Supplier")) { s = new Supplier(); s = s.getDocument(jsonObject); //System.out.println(s.toString()); s_table = this.getTable(s_table, maprdbJsonTablePath); s.insertDocument(s_table); } else if (maprdbJsonTableName.equalsIgnoreCase("Region")) { r = new Region(); r = r.getDocument(jsonObject); //System.out.println(r.toString()); r_table = this.getTable(r_table, maprdbJsonTablePath); r.insertDocument(r_table); } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } switch (maprdbJsonTableName) { case "CUSTOMER": System.out.println("Imported " + c_count + " Records\n"); break; case "LINEITEM": System.out.println("Imported " + l_count + " Records\n"); break; case "ORDERS": System.out.println("Imported " + o_count + " Records\n"); break; case "PART": System.out.println("Imported " + p_count + " Records\n"); break; case "PARTSUPP": System.out.println("Imported " + ps_count + " Records\n"); break; case "NATION": System.out.println("Imported " + n_count + " Records\n"); break; case "SUPPLIER": System.out.println("Imported " + s_count + " Records\n"); break; case "REGION": System.out.println("Imported " + r_count + " Records\n"); break; } }