Example usage for java.util Scanner close

List of usage examples for java.util Scanner close

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes this scanner.

Usage

From source file:com.joliciel.csvLearner.CSVEventListReader.java

void scanResultsFile() throws IOException {
    if (this.resultFilePath != null) {
        // have results
        Scanner resultScanner = new Scanner(new FileInputStream(resultFilePath), "UTF-8");

        try {//from  ww w .j a  va2 s.  c  om
            int i = 0;
            boolean firstLine = true;
            while (resultScanner.hasNextLine()) {
                String line = resultScanner.nextLine();
                if (!firstLine) {
                    List<String> cells = CSVFormatter.getCSVCells(line);
                    String ref = cells.get(0);

                    String outcome = cells.get(1);
                    boolean includeEvent = true;
                    if (includedOutcomes != null) {
                        if (!includedOutcomes.contains(outcome))
                            includeEvent = false;
                    } else if (excludedOutcomes != null) {
                        if (excludedOutcomes.contains(outcome))
                            includeEvent = false;
                    }

                    if (includeEvent) {
                        if (eventMap.containsKey(ref)) {
                            throw new RuntimeException("Duplicate identifier in result file: " + ref);
                        }

                        GenericEvent event = new GenericEvent(ref);
                        outcomes.add(outcome);
                        event.setOutcome(outcome);
                        if (testIds != null) {
                            if (testIds.contains(ref))
                                event.setTest(true);
                            else
                                event.setTest(false);
                        } else if (trainingSetType.equals(TrainingSetType.ALL_TRAINING)) {
                            event.setTest(false);
                        } else if (trainingSetType.equals(TrainingSetType.ALL_TEST)) {
                            event.setTest(true);
                        } else if (trainingSetType.equals(TrainingSetType.TEST_SEGMENT)) {
                            event.setTest(i % 10 == testSegment);
                        } else {
                            throw new RuntimeException("Unknown TrainingSetType: " + trainingSetType);
                        }
                        eventMap.put(ref, event);
                        i++;
                    } else {
                        eventsToExclude.add(ref);
                    }
                }
                firstLine = false;

            }
        } finally {
            resultScanner.close();
        }
    } // have results      
}

From source file:carmen.demo.LocationResolverStatsDemo.java

private void run(String inputFile, String outputFile) throws FileNotFoundException, IOException {
    Writer output = null;/*from  w w w .j a  v a2s  .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:edu.harvard.iq.dataverse.dataaccess.TabularSubsetGenerator.java

public static String[] subsetStringVector(InputStream in, int column, int numCases) {
    String[] retVector = new String[numCases];
    Scanner scanner = new Scanner(in);
    scanner.useDelimiter("\\n");

    for (int caseIndex = 0; caseIndex < numCases; caseIndex++) {
        if (scanner.hasNext()) {
            String[] line = (scanner.next()).split("\t", -1);
            retVector[caseIndex] = line[column];

            if ("".equals(line[column])) {
                // An empty string is a string missing value!
                // An empty string in quotes is an empty string!
                retVector[caseIndex] = null;
            } else {
                // Strip the outer quotes:
                line[column] = line[column].replaceFirst("^\\\"", "");
                line[column] = line[column].replaceFirst("\\\"$", "");

                // We need to restore the special characters that 
                // are stored in tab files escaped - quotes, new lines 
                // and tabs. Before we do that however, we need to 
                // take care of any escaped backslashes stored in 
                // the tab file. I.e., "foo\t" should be transformed 
                // to "foo<TAB>"; but "foo\\t" should be transformed 
                // to "foo\t". This way new lines and tabs that were
                // already escaped in the original data are not 
                // going to be transformed to unescaped tab and 
                // new line characters!
                String[] splitTokens = line[column].split(Matcher.quoteReplacement("\\\\"), -2);

                // (note that it's important to use the 2-argument version 
                // of String.split(), and set the limit argument to a
                // negative value; otherwise any trailing backslashes 
                // are lost.)
                for (int i = 0; i < splitTokens.length; i++) {
                    splitTokens[i] = splitTokens[i].replaceAll(Matcher.quoteReplacement("\\\""), "\"");
                    splitTokens[i] = splitTokens[i].replaceAll(Matcher.quoteReplacement("\\t"), "\t");
                    splitTokens[i] = splitTokens[i].replaceAll(Matcher.quoteReplacement("\\n"), "\n");
                    splitTokens[i] = splitTokens[i].replaceAll(Matcher.quoteReplacement("\\r"), "\r");
                }//from w w w .ja  v a 2 s  .  c o m
                // TODO: 
                // Make (some of?) the above optional; for ex., we 
                // do need to restore the newlines when calculating UNFs;
                // But if we are subsetting these vectors in order to 
                // create a new tab-delimited file, they will 
                // actually break things! -- L.A. Jul. 28 2014

                line[column] = StringUtils.join(splitTokens, '\\');

                retVector[caseIndex] = line[column];
            }

        } else {
            scanner.close();
            throw new RuntimeException("Tab file has fewer rows than the stored number of cases!");
        }
    }

    int tailIndex = numCases;
    while (scanner.hasNext()) {
        String nextLine = scanner.next();
        if (!"".equals(nextLine)) {
            scanner.close();
            throw new RuntimeException(
                    "Column " + column + ": tab file has more nonempty rows than the stored number of cases ("
                            + numCases + ")! current index: " + tailIndex + ", line: " + nextLine);
        }
        tailIndex++;
    }

    scanner.close();
    return retVector;

}

From source file:edu.uci.ics.asterix.transaction.management.service.locking.LockManagerDeterministicUnitTest.java

public void readRequest() throws IOException, ACIDException {
    int i = 0;//from   w  ww. j  a  v a 2  s . c  om
    LockRequest lockRequest = null;
    TransactionContext txnContext = null;
    HashMap<Integer, TransactionContext> jobMap = new HashMap<Integer, TransactionContext>();

    int threadId;
    String requestType;
    int jobId;
    int datasetId;
    int PKHashVal;
    int waitTime;
    ArrayList<Integer> list = null;
    String lockMode;

    Scanner scanner = new Scanner(new FileInputStream(requestFileName));
    while (scanner.hasNextLine()) {
        try {
            threadId = Integer.parseInt(scanner.next().substring(1));
            requestType = scanner.next();
            if (requestType.equals("CSQ") || requestType.equals("CST") || requestType.equals("END")) {
                log("LockRequest[" + i++ + "]:T" + threadId + "," + requestType);
                lockRequest = new LockRequest("Thread-" + threadId, getRequestType(requestType));
                if (requestType.equals("CSQ") || requestType.equals("CST")) {
                    list = new ArrayList<Integer>();
                    while (scanner.hasNextInt()) {
                        threadId = scanner.nextInt();
                        if (threadId < 0) {
                            break;
                        }
                        list.add(threadId);
                    }
                    expectedResultList.add(list);
                }
            } else if (requestType.equals("DW")) {
                defaultWaitTime = scanner.nextInt();
                log("LockRequest[" + i++ + "]:T" + threadId + "," + requestType + "," + defaultWaitTime);
                continue;
            } else if (requestType.equals("W")) {
                waitTime = scanner.nextInt();
                log("LockRequest[" + i++ + "]:T" + threadId + "," + requestType);
                lockRequest = new LockRequest("Thread-" + threadId, getRequestType(requestType), waitTime);
            } else {
                jobId = Integer.parseInt(scanner.next().substring(1));
                datasetId = Integer.parseInt(scanner.next().substring(1));
                PKHashVal = Integer.parseInt(scanner.next().substring(1));
                lockMode = scanner.next();
                txnContext = jobMap.get(jobId);
                if (txnContext == null) {
                    txnContext = new TransactionContext(new JobId(jobId), txnProvider);
                    jobMap.put(jobId, txnContext);
                }
                log("LockRequest[" + i++ + "]:T" + threadId + "," + requestType + ",J" + jobId + ",D"
                        + datasetId + ",E" + PKHashVal + "," + lockMode);
                lockRequest = new LockRequest("Thread-" + threadId, getRequestType(requestType),
                        new DatasetId(datasetId), PKHashVal, getLockMode(lockMode), txnContext);
            }

            requestList.add(lockRequest);
        } catch (NoSuchElementException e) {
            scanner.close();
            break;
        }
    }
}

From source file:com.joliciel.talismane.TalismaneConfig.java

/**
 * A regex used to process the input, when pre-annotated.
 * @return//  www  . j a  v a2s  .  c o m
 */
public String getInputRegex() {
    try {
        if (inputRegex == null && inputPatternFilePath != null && inputPatternFilePath.length() > 0) {
            Scanner inputPatternScanner = null;
            File inputPatternFile = new File(inputPatternFilePath);
            inputPatternScanner = new Scanner(inputPatternFile);
            if (inputPatternScanner.hasNextLine()) {
                inputRegex = inputPatternScanner.nextLine();
            }
            inputPatternScanner.close();
            if (inputRegex == null)
                throw new TalismaneException("No input pattern found in " + inputPatternFilePath);
        }
        return inputRegex;
    } catch (Exception e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:files.populate.java

void populate_users(Connection connection, String filename) {
    String fileName = "/Users/yash/Documents/workspace/HW 3/data/yelp_user.json";
    Path path = Paths.get(fileName);
    Scanner scanner = null;
    try {/*from  w  w  w  .  j a  v a2  s. c om*/
        scanner = new Scanner(path);

    } catch (IOException e) {
        e.printStackTrace();
    }

    //read file line by line
    scanner.useDelimiter("\n");
    int i = 0;
    while (scanner.hasNext()) {
        if (i < 500) {
            JSONParser parser = new JSONParser();
            String s = (String) scanner.next();
            JSONObject obj;
            try {
                obj = (JSONObject) parser.parse(s);
                Map votes = (Map) obj.get("votes");
                ArrayList friends;
                friends = (ArrayList) obj.get("friends");
                Map compliments = (Map) obj.get("compliments");
                //                    
                String query = "INSERT INTO YELP_USERS (user_id,yelping_since,name,fans,average_stars,type,review_count,VOTES_FUNNY,VOTES_COOL,VOTES_USEFUL) VALUES('"
                        + obj.get("user_id") + "','" + obj.get("yelping_since") + "','" + obj.get("name") + "',"
                        + obj.get("fans") + "," + obj.get("average_stars") + ",'" + obj.get("type") + "',"
                        + obj.get("review_count") + "," + votes.get("funny") + "," + votes.get("cool") + ","
                        + votes.get("useful") + ")";
                System.out.println(query);
                Statement statement = connection.createStatement();
                statement.executeUpdate(query);
                for (int j = 0; j < friends.size(); ++j) {
                    String q2 = "insert into users_friends values ('" + obj.get("user_id") + "','"
                            + friends.get(j) + "')";
                    System.out.println(q2);
                    statement.executeUpdate(q2);
                }
                Set keys = compliments.keySet();
                Object[] keys1 = keys.toArray();
                for (int j = 0; j < keys1.length; ++j) {
                    //                             String q2 = "insert into users_friends values ("+obj.get("user_id")+","+compliments.get(j)+")";
                    String thiskey = keys1[j].toString();
                    long val = (long) compliments.get(thiskey);
                    String q3 = "insert into users_compliments values ('" + obj.get("user_id") + "','" + thiskey
                            + "'," + val + ")";
                    System.out.println(q3);
                    statement.executeUpdate(q3);
                }
                statement.close();
            } catch (ParseException ex) {
                Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex);
            }

            i++;
        } else
            break;
    }

    scanner.close();

}

From source file:com.joliciel.talismane.TalismaneConfig.java

/**
 * A regex used to process the evaluation corpus.
 * @return//from  w  ww  . j  av  a 2s .c  om
 */
public String getEvaluationRegex() {
    try {
        if (evaluationRegex == null) {
            if (evaluationPatternFilePath != null && evaluationPatternFilePath.length() > 0) {
                Scanner evaluationPatternScanner = null;
                File evaluationPatternFile = new File(evaluationPatternFilePath);
                evaluationPatternScanner = new Scanner(evaluationPatternFile);
                if (evaluationPatternScanner.hasNextLine()) {
                    evaluationRegex = evaluationPatternScanner.nextLine();
                }
                evaluationPatternScanner.close();
                if (evaluationRegex == null)
                    throw new TalismaneException("No evaluation pattern found in " + evaluationPatternFilePath);
            } else {
                evaluationRegex = this.getInputRegex();
            }
        }
        return evaluationRegex;
    } catch (Exception e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:se.lu.nateko.edca.svc.GeoHelper.java

/**
 * Reads the geometry and attribute data from the local storage
 * into the active GeographyLayer.//  w  ww.  j a  va  2 s  .co m
 */
private boolean readIntoGeographyLayer() {
    Log.d(TAG, "readIntoGeographyLayer() called.");
    File layerPath = new File(getPath());

    File geomFile = new File(layerPath, "geometry.txt");
    File attFile = new File(layerPath, "attributes.txt");
    try {
        if (!geomFile.exists()) // If there is no geometry file.
            Log.i(TAG, "No geometry stored.");
        else {
            /* Read the geometry with ID and geometry WKT. */
            Scanner scanner = new Scanner(new FileInputStream(geomFile));

            try {
                while (scanner.hasNextLine()) {
                    String[] line = scanner.nextLine().split("[ ]{1}", 2);
                    WKTReader reader = new WKTReader();
                    switch (mGeoLayer.getTypeMode()) {
                    case GeographyLayer.TYPE_POINT: {
                        Coordinate p = ((Point) reader.read(line[1])).getCoordinate();
                        mGeoLayer.addGeometry(new LatLng(p.y, p.x), Long.parseLong(line[0]));
                        break;
                    }
                    case GeographyLayer.TYPE_LINE: {
                        Geometry geom = reader.read(line[1]);
                        Log.v(TAG, "Geom type: " + geom.getGeometryType());
                        if (geom.getGeometryType().equalsIgnoreCase("LineString"))
                            mGeoLayer.addLine((LineString) geom, Long.parseLong(line[0]), true);
                        else if (geom.getGeometryType().equalsIgnoreCase("Point")) {
                            Coordinate p = ((Point) geom).getCoordinate();
                            mGeoLayer.addGeometry(new LatLng(p.y, p.x));
                        }
                        break;
                    }
                    case GeographyLayer.TYPE_POLYGON: {
                        Geometry geom = reader.read(line[1]);
                        Log.v(TAG, "Geom type: " + geom.getGeometryType());
                        if (geom.getGeometryType().equalsIgnoreCase("Polygon"))
                            mGeoLayer.addPolygon((Polygon) reader.read(line[1]), Long.parseLong(line[0]), true);
                        else if (geom.getGeometryType().equalsIgnoreCase("Point")) {
                            Coordinate p = ((Point) geom).getCoordinate();
                            mGeoLayer.addGeometry(new LatLng(p.y, p.x));
                        }
                        break;
                    }
                    }
                }
            } catch (ParseException e) {
                Log.e(TAG, e.toString());
                return false;
            } finally {
                scanner.close();
            }
        }
        if (!attFile.exists()) // If there is no attribute file.
            Log.i(TAG, "No attributes stored.");
        else {
            /* Read the attributes with ID and field info. */
            Scanner scanner = new Scanner(new FileInputStream(attFile));
            try {
                while (scanner.hasNextLine()) {
                    String[] line = scanner.nextLine().split("[;]{1}", -1);
                    for (int i = 0; i < mGeoLayer.getNonGeomFields().size(); i++)
                        mGeoLayer.addAttribute(Long.parseLong(line[0]),
                                mGeoLayer.getNonGeomFields().get(i).getName(), line[i + 1]);
                }
            } finally {
                scanner.close();
            }
        }
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        return false;
    }
    return true;
}

From source file:com.joliciel.talismane.TalismaneConfig.java

private TokeniserPatternManager getTokeniserPatternManager() {
    if (tokeniserPatternManager == null) {
        if (tokeniserPatternFilePath.length() == 0)
            throw new RuntimeException("Missing argument: tokeniserPatterns");
        try {//w  ww  .j a  v  a2s. com
            File tokeniserPatternFile = new File(tokeniserPatternFilePath);
            Scanner scanner = new Scanner(new BufferedReader(
                    new InputStreamReader(new FileInputStream(tokeniserPatternFile), this.getInputCharset())));
            List<String> patternDescriptors = new ArrayList<String>();
            while (scanner.hasNextLine()) {
                String descriptor = scanner.nextLine();
                patternDescriptors.add(descriptor);
                LOG.debug(descriptor);
            }
            scanner.close();

            tokeniserPatternManager = this.getTokeniserPatternService().getPatternManager(patternDescriptors);
        } catch (Exception e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        }
    }
    return tokeniserPatternManager;
}