Example usage for java.util Scanner hasNextLine

List of usage examples for java.util Scanner hasNextLine

Introduction

In this page you can find the example usage for java.util Scanner hasNextLine.

Prototype

public boolean hasNextLine() 

Source Link

Document

Returns true if there is another line in the input of this scanner.

Usage

From source file:eu.creatingfuture.propeller.webLoader.propellent.Propellent.java

public List<String> getPorts() {
    List<String> ports = new ArrayList<String>();
    try {//from w ww. j a va  2 s. co  m
        CommandLine cmdLine = new CommandLine("propellent/Propellent.exe");
        cmdLine.addArgument("/id");
        cmdLine.addArgument("/gui").addArgument("OFF");
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValues(new int[] { 451, 301 });

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            //   exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            return ports;
        }

        output = outputStream.toString();

        // 301 = None found
        // 451 = Chip found
        if (exitValue == 301) {
            return ports;
        }

        //            System.out.println("output: " + output);
        Scanner scanner = new Scanner(output);

        Pattern chipFoundPattern = Pattern.compile(".*?(EVT:505).*?");
        Pattern pattern = Pattern.compile(".*?found on (?<comport>[a-zA-Z0-9]*).$");
        while (scanner.hasNextLine()) {
            String portLine = scanner.nextLine();
            if (chipFoundPattern.matcher(portLine).matches()) {
                Matcher portMatch = pattern.matcher(portLine);
                if (portMatch.find()) {
                    String port = portMatch.group("comport");
                    ports.add(port);
                }
            }
        }

        //            System.out.println("output: " + output);
        //            System.out.println("exitValue: " + exitValue);
        return ports;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        return null;
    }
}

From source file:com.joliciel.talismane.extensions.standoff.ConllFileSplitter.java

/**
 * @param args//from   ww  w .ja  va  2 s.c  o  m
 * @throws IOException 
 * @throws UnsupportedEncodingException 
 */
public void split(String filePath, int startIndex, int sentencesPerFile, String encoding) {
    try {
        String fileBase = filePath;
        if (filePath.indexOf('.') > 0)
            fileBase = filePath.substring(0, filePath.lastIndexOf('.'));
        File file = new File(filePath);
        Scanner scanner = new Scanner(
                new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)));

        boolean hasSentence = false;
        int currentFileIndex = startIndex;

        int sentenceCount = 0;
        Writer writer = null;
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.length() == 0 && !hasSentence) {
                continue;
            } else if (line.length() == 0) {
                writer.write("\n");
                writer.flush();
                hasSentence = false;
            } else {
                if (!hasSentence) {
                    hasSentence = true;
                    sentenceCount++;
                }
                if (writer == null || sentenceCount % sentencesPerFile == 0) {
                    if (writer != null) {
                        writer.flush();
                        writer.close();
                    }
                    File outFile = new File(fileBase + "_" + df.format(currentFileIndex) + ".tal");
                    outFile.delete();
                    outFile.createNewFile();
                    writer = new BufferedWriter(
                            new OutputStreamWriter(new FileOutputStream(outFile, false), encoding));
                    currentFileIndex++;
                    hasSentence = false;
                }

                writer.write(line + "\n");
                writer.flush();
            }
        }
        if (writer != null) {
            writer.flush();
            writer.close();
        }
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:edu.brandeis.cs.nlp.mae.io.DTDLoader.java

public boolean read(InputStream stream) throws MaeIODTDException, MaeDBException {
    Scanner sc = new Scanner(stream, "UTF-8");
    int lineNum = 1;
    while (sc.hasNextLine()) {
        String next = sc.nextLine();
        // getting rid of comments
        if (next.contains("<!--")) {
            while (sc.hasNextLine() && !next.contains("-->")) {
                next = sc.nextLine();/*from ww w  .  j  ava  2s .  co m*/
                lineNum++;
            }
            next = sc.nextLine();
        }

        //then, concatenate lines about a tag into one string
        String element = "";
        if (next.contains("<")) {
            element += next;
            while (sc.hasNextLine() && !next.contains(">")) {
                next = sc.nextLine();
                lineNum++;
                element += next;
            }
        }
        lineNum++;
        // remove some problematic unicode characters before processing
        element = normalizeLine(element);
        process(element, lineNum);
    }
    validateLinkTagTypes();
    return validateReadTask();
}

From source file:carmen.demo.LocationResolverStatsDemo.java

private void run(String inputFile, String outputFile) throws FileNotFoundException, IOException {
    Writer output = null;/*from w ww .  ja v  a2  s  . c  o m*/
    if (outputFile != null)
        output = Utils.createWriter(outputFile);

    HashMap<ResolutionMethod, Integer> resolutionMethodCounts = new HashMap<ResolutionMethod, Integer>();

    int numCity = 0;
    int numCounty = 0;
    int numState = 0;
    int numCountry = 0;

    int hasPlace = 0;
    int hasCoordinate = 0;
    int hasCoordinate2 = 0;
    int hasGeo = 0;
    int hasUserProfile = 0;

    Scanner scanner = Utils.createScanner(inputFile);

    ObjectMapper mapper = new ObjectMapper();
    int numResolved = 0;
    int total = 0;
    int skipped = 0;
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();

        HashMap<String, Object> tweet = null;

        try {
            @SuppressWarnings("unchecked")
            HashMap<String, Object> readValue = (HashMap<String, Object>) mapper.readValue(line, Map.class);
            tweet = readValue;
        } catch (com.fasterxml.jackson.core.JsonParseException exception) {
            logger.warn("Skipping bad tweet: " + line);
            skipped++;
            continue;
        } catch (com.fasterxml.jackson.databind.JsonMappingException exception) {
            logger.warn("Skipping bad tweet: " + line);
            skipped++;
            continue;
        }
        Map<String, Object> place = Utils.getPlaceFromTweet(tweet);
        if (place != null && place.size() > 0)
            hasPlace++;
        LatLng latLng = Utils.getLatLngFromTweet(tweet);
        if (latLng != null)
            hasCoordinate++;
        if (tweet.get("coordinates") != null)
            hasCoordinate2++;
        if (tweet.get("geo") != null)
            hasGeo++;

        String tweet_location = Utils.getLocationFromTweet(tweet);
        if (tweet_location != null && tweet_location.length() != 0) {
            hasUserProfile++;
        }

        total++;

        if (total % 10000 == 0) {
            logger.info(total + "\r");
        }
        Location resolvedLocation = this._locationResolver.resolveLocationFromTweet(tweet);

        if (resolvedLocation != null && !resolvedLocation.isNone()) {
            tweet.put(Constants.TWEET_USER_LOCATION, Location.createJsonFromLocation(resolvedLocation));

            numResolved++;
            ResolutionMethod resolutionMethod = resolvedLocation.getResolutionMethod();
            if (!resolutionMethodCounts.containsKey(resolutionMethod))
                resolutionMethodCounts.put(resolutionMethod, 0);
            resolutionMethodCounts.put(resolutionMethod, resolutionMethodCounts.get(resolutionMethod) + 1);

            // What resolution is this location?
            if (resolvedLocation.getCity() != null) {
                numCity++;
            } else if (resolvedLocation.getCounty() != null) {
                numCounty++;
            } else if (resolvedLocation.getState() != null) {
                numState++;
            } else if (resolvedLocation.getCountry() != null) {
                numCountry++;
            }
        }
        if (output != null) {
            String outputString = mapper.writeValueAsString(tweet);
            output.write(outputString);
            output.write("\n");
        }

    }
    if (output != null)
        output.close();

    scanner.close();
    logger.info("Total: " + total);
    logger.info("Resolved: " + numResolved);
    logger.info("Skipped (not included in total): " + skipped);

    logger.info("Has Place:" + hasPlace);
    logger.info("Has Coordinate: " + hasCoordinate);
    logger.info("Has Coordinate2: " + hasCoordinate2);
    logger.info("Has UserProfile: " + hasUserProfile);
    logger.info("Has Geo: " + hasGeo);

    logger.info("Num city: " + numCity);
    logger.info("Num county: " + numCounty);
    logger.info("Num state: " + numState);
    logger.info("Num country: " + numCountry);

    for (ResolutionMethod method : resolutionMethodCounts.keySet()) {
        int count = resolutionMethodCounts.get(method);
        logger.info(method + "\t" + count);
    }

}

From source file:org.apache.activemq.transport.discovery.http.HTTPDiscoveryAgent.java

synchronized private Set<String> doLookup(long freshness) {
    String url = registryURL + "?freshness=" + freshness;
    try {//  w  w w.  j  a  va 2s. c o m
        HttpGet method = new HttpGet(url);
        ResponseHandler<String> handler = new BasicResponseHandler();
        String response = httpClient.execute(method, handler);
        LOG.debug("GET to " + url + " got a " + response);
        Set<String> rc = new HashSet<String>();
        Scanner scanner = new Scanner(response);
        while (scanner.hasNextLine()) {
            String service = scanner.nextLine();
            if (service.trim().length() != 0) {
                rc.add(service);
            }
        }
        scanner.close();
        return rc;
    } catch (Exception e) {
        LOG.debug("GET to " + url + " failed with: " + e);
        return null;
    }
}

From source file:com.gitblit.auth.HtpasswdAuthProvider.java

/**
 * Reads the realm file and rebuilds the in-memory lookup tables.
 */// w w  w .ja va  2s  .c  o  m
protected synchronized void read() {
    boolean forceReload = false;
    File file = getFileOrFolder(KEY_HTPASSWD_FILE, DEFAULT_HTPASSWD_FILE);
    if (!file.equals(htpasswdFile)) {
        this.htpasswdFile = file;
        this.htUsers.clear();
        forceReload = true;
    }

    if (htpasswdFile.exists() && (forceReload || (htpasswdFile.lastModified() != lastModified))) {
        lastModified = htpasswdFile.lastModified();
        htUsers.clear();

        Pattern entry = Pattern.compile("^([^:]+):(.+)");

        Scanner scanner = null;
        try {
            scanner = new Scanner(new FileInputStream(htpasswdFile));
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine().trim();
                if (!line.isEmpty() && !line.startsWith("#")) {
                    Matcher m = entry.matcher(line);
                    if (m.matches()) {
                        htUsers.put(m.group(1), m.group(2));
                    }
                }
            }
        } catch (Exception e) {
            logger.error(MessageFormat.format("Failed to read {0}", htpasswdFile), e);
        } finally {
            if (scanner != null) {
                scanner.close();
            }
        }
    }
}

From source file:com.joliciel.jochre.lexicon.TextFileLexicon.java

public TextFileLexicon(File textFile, String charset) {
    Scanner scanner;
    try {/*w w  w .j  av  a2 s  .  c o m*/
        scanner = new Scanner(
                new BufferedReader(new InputStreamReader(new FileInputStream(textFile), charset)));

        try {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (!line.startsWith("#")) {
                    String[] parts = line.split("\t");

                    if (parts.length > 0) {
                        String word = parts[0];
                        int frequency = 1;
                        if (parts.length > 1)
                            frequency = Integer.parseInt(parts[1]);
                        entries.put(word, frequency);
                    }

                }

            }
        } finally {
            scanner.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.mythesis.userbehaviouranalysis.Utils.java

/**
 * a method that reads a txt file with the necessary parameters
 * @param input the file that includes the necessary parameters
 *//*from  w  w w.  ja  v  a2s  .c o  m*/
public void readInput(File input) {
    FileInputStream inputStream = null;
    Scanner sc = null;
    wordvectors = new HashMap<>();
    crawlerOutputPaths = new HashMap<>();

    HashMap<String, String> wordvectorsPaths = new HashMap<>();
    try {
        inputStream = new FileInputStream(input);
        sc = new Scanner(inputStream);

        if (sc.hasNextLine()) {
            int numProfiles = Integer.parseInt(sc.nextLine().split(";")[1].trim().toLowerCase());

            int j = 0;
            while (j < numProfiles) {
                String profile = "";
                if (sc.hasNextLine()) {
                    profile = sc.nextLine().split(";")[1].trim();
                }
                if (sc.hasNextLine()) {
                    String path = sc.nextLine().split(";")[1].trim();
                    crawlerOutputPaths.put(profile, path);
                }
                if (sc.hasNextLine()) {
                    String path = sc.nextLine().split(";")[1].trim();
                    wordvectorsPaths.put(profile, path);
                }
                j++;
            }
        }

        for (String profile : wordvectorsPaths.keySet()) {
            File file = new File(wordvectorsPaths.get(profile));
            try {
                List<String> wordvector = FileUtils.readLines(file);
                wordvectors.put(profile, (ArrayList<String>) wordvector);
            } catch (IOException ex) {
                Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    } catch (FileNotFoundException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
                Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (sc != null) {
            sc.close();
        }
    }

}

From source file:org.apache.marmotta.ucuenca.wk.commons.function.SemanticDistance.java

private synchronized String http(String s) throws SQLException, IOException {

    Statement stmt = conn.createStatement();
    String sql;//from   w w  w  .  j a va 2  s  . com
    sql = "SELECT * FROM cache where cache.key='" + commonservices.getMD5(s) + "'";
    java.sql.ResultSet rs = stmt.executeQuery(sql);
    String resp = "";
    if (rs.next()) {
        resp = rs.getString("value");
        rs.close();
        stmt.close();
    } else {
        rs.close();
        stmt.close();
        final URL url = new URL(s);
        final URLConnection connection = url.openConnection();
        connection.setConnectTimeout(60000);
        connection.setReadTimeout(60000);
        connection.addRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0");
        connection.addRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8");
        while (reader.hasNextLine()) {
            final String line = reader.nextLine();
            resp += line + "\n";
        }
        reader.close();

        try {
            JsonParser parser = new JsonParser();
            parser.parse(resp);
            PreparedStatement stmt2 = conn.prepareStatement("INSERT INTO cache (key, value) values (?, ?)");
            stmt2.setString(1, commonservices.getMD5(s));
            stmt2.setString(2, resp);
            stmt2.executeUpdate();
            stmt2.close();
        } catch (Exception e) {

        }
    }

    return resp;
}

From source file:org.apache.hadoop.hdfs.server.diskbalancer.command.TestDiskBalancerCommand.java

private List<String> runCommandInternal(final String cmdLine) throws Exception {
    String[] cmds = StringUtils.split(cmdLine, ' ');
    org.apache.hadoop.hdfs.tools.DiskBalancer db = new org.apache.hadoop.hdfs.tools.DiskBalancer(conf);

    ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(bufOut);
    db.run(cmds, out);/*from   w  w  w  .  ja v a2  s.co  m*/

    Scanner scanner = new Scanner(bufOut.toString());
    List<String> outputs = Lists.newArrayList();
    while (scanner.hasNextLine()) {
        outputs.add(scanner.nextLine());
    }
    return outputs;
}