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:ec.edu.ucuenca.dcc.sld.HttpUtils.java

public static synchronized String Http2(String s, Map<String, String> mp) throws SQLException, IOException {
    String resp = "";
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(s);
    method.getParams().setContentCharset("utf-8");

    client.getParams().setParameter("api-key", "58ef39e0-b91a-11e6-a057-97f4c970893c");
    client.getParams().setParameter("Content-Type", "application/json");

    //Add any parameter if u want to send it with Post req.
    for (Entry<String, String> mcc : mp.entrySet()) {
        method.addParameter(mcc.getKey(), mcc.getValue());
    }/*  ww  w  .  j ava  2  s.  c o  m*/

    int statusCode = client.executeMethod(method);

    if (statusCode != -1) {
        InputStream in = method.getResponseBodyAsStream();
        final Scanner reader = new Scanner(in, "UTF-8");
        while (reader.hasNextLine()) {
            final String line = reader.nextLine();
            resp += line + "\n";
        }
        reader.close();

    }

    return resp;
}

From source file:tr.edu.gsu.nerwip.tools.corpus.ArticleCompletion.java

/**
 * This methods allows setting the category of 
 * articles already retrieved and manually annotated
 * for evaluation. This way, the annotation can be
 * performed overall, or in function of the category.
 * The categories must be listed in a file in which
 * each line contains the name of the article folder
 * and the corresponding category./*from w  w w .j a  v a 2s . c om*/
 * 
 * @throws ParseException
 *       Problem while accessing the files.
 * @throws SAXException
 *       Problem while accessing the files.
 * @throws IOException
 *       Problem while accessing the files.
 */
private static void insertArticleCategories() throws ParseException, SAXException, IOException {
    logger.log("Setting categories in articles");
    logger.increaseOffset();

    File file = new File(FileNames.FO_OUTPUT + File.separator + "cats" + FileNames.EX_TXT);
    FileInputStream fis = new FileInputStream(file);
    InputStreamReader isr = new InputStreamReader(fis);
    Scanner scanner = new Scanner(isr);
    logger.log("Reading file " + file);

    logger.increaseOffset();
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine().trim();
        String temp[] = line.split("\\t");
        String name = temp[0];
        String folderPath = FileNames.FO_OUTPUT + File.separator + name;
        File folderFile = new File(folderPath);
        if (folderFile.exists()) {
            //            String gender = temp[1];
            String categoryStr = temp[2].toUpperCase(Locale.ENGLISH);
            ArticleCategory category = ArticleCategory.valueOf(categoryStr);
            //category = StringTools.initialize(category);
            logger.log("Processing '" + name + "': cat=" + category);
            List<ArticleCategory> categories = new ArrayList<ArticleCategory>();
            categories.add(category);

            Article article = Article.read(name);
            article.setCategories(categories);
            article.write();
        } else
            logger.log("Processing '" + temp[0] + "': folder not found");
    }
    logger.decreaseOffset();

    scanner.close();
    logger.decreaseOffset();
    logger.log("Categories set");
}

From source file:org.apache.solr.analytics.AbstractAnalyticsStatsTest.java

public static String[] fileToStringArr(Class<?> clazz, String fileName) throws FileNotFoundException {
    InputStream in = clazz.getResourceAsStream(fileName);
    if (in == null)
        throw new FileNotFoundException("Resource not found: " + fileName);
    Scanner file = new Scanner(in, "UTF-8");
    try {//  w ww .  j  av a 2  s  . com
        ArrayList<String> strList = new ArrayList<>();
        while (file.hasNextLine()) {
            String line = file.nextLine();
            line = line.trim();
            if (StringUtils.isBlank(line) || line.startsWith("#")) {
                continue;
            }
            String[] param = line.split("=");
            strList.add(param[0]);
            strList.add(param[1]);
        }
        return strList.toArray(new String[0]);
    } finally {
        IOUtils.closeWhileHandlingException(file, in);
    }
}

From source file:ec.edu.ucuenca.dcc.sld.HttpUtils.java

public static synchronized String Http(String s) throws SQLException, IOException {

    String resp = "";
    final URL url = new URL(s);
    final URLConnection connection = url.openConnection();
    connection.setConnectTimeout(60000);
    connection.setReadTimeout(60000);//from   w w  w  .  j a va2s.  c  o m
    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();

    return resp;
}

From source file:com.dm.estore.common.utils.FileUtils.java

/**
 * Save text file from classpath to file.
 *
 * @param resource Classpath text resource
 * @param outputFile Output file//from   w  w w. j  a v a  2 s .c  o  m
 * @param processor Optional line processor
 * @param encoding Text encoding
 * @throws IOException Exception
 */
public static void saveTextFileFromClassPath(String resource, String outputFile, String encoding,
        TextStreamProcessor processor) throws IOException {

    final InputStream inputStream = FileUtils.class.getResourceAsStream(resource);
    final Scanner scanner = new Scanner(inputStream, encoding);
    final Writer out = new OutputStreamWriter(new FileOutputStream(outputFile), encoding);
    try {

        while (scanner.hasNextLine()) {
            final String nextLine = scanner.nextLine();
            out.write(processor != null ? processor.process(nextLine) : nextLine);
            out.write(NEW_LINE_SEP);
        }

    } finally {
        out.close();
        scanner.close();
    }

}

From source file:css.variable.converter.CSSVariableConverter.java

/**
 * Collect variables from main file/*from   www  . j a  v a  2  s  .  co m*/
 *
 * @param mainFile File with ":root" identifier for variables.
 * @return
 */
private static ArrayList<CSSVar> getCSSVars(File mainFile, String identifier) {
    ArrayList<CSSVar> cSSVarsList = new ArrayList<CSSVar>();
    boolean isInVarDefinition = false;
    boolean isFinished = false;

    try {
        Scanner fileReader = new Scanner(mainFile);

        // Loop until end of text file, or outside of :root 
        while (fileReader.hasNextLine() && !isFinished) {
            String currentLine = fileReader.nextLine();

            if (isInVarDefinition) {

                // I identify all of my variables with --
                if (currentLine.contains("--")) {

                    int varNameBegIndex = currentLine.indexOf("--");
                    int varNameEndIndex = currentLine.indexOf(":");
                    String varName = currentLine.substring(varNameBegIndex, varNameEndIndex);

                    int varValueBegIndex = currentLine.indexOf(":") + 1;
                    int varValueEndIndex = currentLine.indexOf(";");
                    String varValue = currentLine.substring(varValueBegIndex, varValueEndIndex);

                    varName = varName.trim();
                    varValue = varValue.trim();
                    cSSVarsList.add(new CSSVar(varName, varValue));
                }

                if (currentLine.contains("}")) {
                    isInVarDefinition = false;
                    isFinished = true;
                }
            } else {
                if (currentLine.contains(identifier)) {
                    isInVarDefinition = true;
                }
            }

        }
        fileReader.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
    }

    return cSSVarsList;
}

From source file:org.apache.solr.analytics.legacy.LegacyAbstractAnalyticsTest.java

public static String[] fileToStringArr(Class<?> clazz, String fileName) throws FileNotFoundException {
    InputStream in = clazz.getResourceAsStream("/solr/analytics/legacy/" + fileName);
    if (in == null)
        throw new FileNotFoundException("Resource not found: " + fileName);
    Scanner file = new Scanner(in, "UTF-8");
    try {/*  w  ww . j ava 2s .  c om*/
        ArrayList<String> strList = new ArrayList<>();
        while (file.hasNextLine()) {
            String line = file.nextLine();
            line = line.trim();
            if (StringUtils.isBlank(line) || line.startsWith("#")) {
                continue;
            }
            String[] param = line.split("=");
            strList.add(param[0]);
            strList.add(param[1]);
        }
        return strList.toArray(new String[0]);
    } finally {
        IOUtils.closeWhileHandlingException(file, in);
    }
}

From source file:com.pinterest.terrapin.tools.TerrapinAdmin.java

@VisibleForTesting
protected static boolean confirmFileSetDeletion(String fileSet, InputStream inputStream) {
    System.out.print(String.format("are you sure to delete %s? [y/n]: ", fileSet));
    Scanner scanner = new Scanner(inputStream);
    if (scanner.hasNextLine()) {
        String input = scanner.nextLine();
        if (input.length() > 0 && input.equalsIgnoreCase("y")) {
            return true;
        }/*from w w w.  j  a va  2 s  . c  o m*/
    }
    return false;
}

From source file:com.pinterest.terrapin.tools.TerrapinAdmin.java

@VisibleForTesting
protected static boolean confirmFileSetRollbackVersion(String fileSet, FileSetInfo fileSetInfo,
        int versionIndex, InputStream inputStream) {
    String rollbackHdfs = fileSetInfo.oldServingInfoList.get(versionIndex).hdfsPath;
    System.out.print(String.format("are you sure to rollback %s from %s to %s? [y/n]: ", fileSet,
            fileSetInfo.servingInfo.hdfsPath, rollbackHdfs));
    Scanner scanner = new Scanner(inputStream);
    if (scanner.hasNextLine()) {
        String input = scanner.nextLine();
        if (input.length() > 0 && input.equalsIgnoreCase("y")) {
            return true;
        }//from   w  ww . j a  va  2  s  . c  om
    }
    return false;
}

From source file:org.apache.mahout.classifier.df.tools.UDistrib.java

private static void runTool(String dataStr, String datasetStr, String output, int numPartitions)
        throws IOException {

    Preconditions.checkArgument(numPartitions > 0, "numPartitions <= 0");

    // make sure the output file does not exist
    Path outputPath = new Path(output);
    Configuration conf = new Configuration();
    FileSystem fs = outputPath.getFileSystem(conf);

    Preconditions.checkArgument(!fs.exists(outputPath), "Output path already exists");

    // create a new file corresponding to each partition
    // Path workingDir = fs.getWorkingDirectory();
    // FileSystem wfs = workingDir.getFileSystem(conf);
    // File parentFile = new File(workingDir.toString());
    // File tempFile = FileUtil.createLocalTempFile(parentFile, "Parts", true);
    // File tempFile = File.createTempFile("df.tools.UDistrib","");
    // tempFile.deleteOnExit();
    File tempFile = FileUtil.createLocalTempFile(new File(""), "df.tools.UDistrib", true);
    Path partsPath = new Path(tempFile.toString());
    FileSystem pfs = partsPath.getFileSystem(conf);

    Path[] partPaths = new Path[numPartitions];
    FSDataOutputStream[] files = new FSDataOutputStream[numPartitions];
    for (int p = 0; p < numPartitions; p++) {
        partPaths[p] = new Path(partsPath, String.format(Locale.ENGLISH, "part.%03d", p));
        files[p] = pfs.create(partPaths[p]);
    }/*w  w  w. j  av  a 2  s .co  m*/

    Path datasetPath = new Path(datasetStr);
    Dataset dataset = Dataset.load(conf, datasetPath);

    // currents[label] = next partition file where to place the tuple
    int[] currents = new int[dataset.nblabels()];

    // currents is initialized randomly in the range [0, numpartitions[
    Random random = RandomUtils.getRandom();
    for (int c = 0; c < currents.length; c++) {
        currents[c] = random.nextInt(numPartitions);
    }

    // foreach tuple of the data
    Path dataPath = new Path(dataStr);
    FileSystem ifs = dataPath.getFileSystem(conf);
    FSDataInputStream input = ifs.open(dataPath);
    Scanner scanner = new Scanner(input, "UTF-8");
    DataConverter converter = new DataConverter(dataset);

    int id = 0;
    while (scanner.hasNextLine()) {
        if (id % 1000 == 0) {
            log.info("progress : {}", id);
        }

        String line = scanner.nextLine();
        if (line.isEmpty()) {
            continue; // skip empty lines
        }

        // write the tuple in files[tuple.label]
        Instance instance = converter.convert(line);
        int label = (int) dataset.getLabel(instance);
        files[currents[label]].writeBytes(line);
        files[currents[label]].writeChar('\n');

        // update currents
        currents[label]++;
        if (currents[label] == numPartitions) {
            currents[label] = 0;
        }
    }

    // close all the files.
    scanner.close();
    for (FSDataOutputStream file : files) {
        Closeables.close(file, false);
    }

    // merge all output files
    FileUtil.copyMerge(pfs, partsPath, fs, outputPath, true, conf, null);
    /*
     * FSDataOutputStream joined = fs.create(new Path(outputPath, "uniform.data")); for (int p = 0; p <
     * numPartitions; p++) {log.info("Joining part : {}", p); FSDataInputStream partStream =
     * fs.open(partPaths[p]);
     * 
     * IOUtils.copyBytes(partStream, joined, conf, false);
     * 
     * partStream.close(); }
     * 
     * joined.close();
     * 
     * fs.delete(partsPath, true);
     */
}