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:com.pinterest.terrapin.tools.TerrapinAdmin.java

@VisibleForTesting
protected static int selectFileSetRollbackVersion(FileSetInfo fileSetInfo, InputStream inputStream) {
    int size = fileSetInfo.oldServingInfoList.size();
    if (fileSetInfo.numVersionsToKeep < 2 || fileSetInfo.oldServingInfoList.size() < 1) {
        throw new IllegalArgumentException("no available version for rollback");
    }/*w  w w.ja va  2 s  .  com*/
    System.out.println("available versions for rollback:");
    for (int i = 0; i < size; i++) {
        FileSetInfo.ServingInfo servingInfo = fileSetInfo.oldServingInfoList.get(i);
        System.out.println(String.format("[%d] %s", i, servingInfo.hdfsPath));
    }
    if (size > 1) {
        System.out.print(String.format("choose a version [0-%d]: ", size - 1));
    } else {
        System.out.print("choose a version [0]: ");
    }
    Scanner scanner = new Scanner(inputStream);
    if (scanner.hasNextLine()) {
        String input = scanner.nextLine();
        if (input.length() > 0) {
            int index = Integer.valueOf(input);
            if (index >= 0 && index < size) {
                return index;
            }
            throw new IllegalArgumentException(
                    String.format("version index should between 0 and %d", size - 1));
        }
    }
    throw new IllegalArgumentException("version index not specified");
}

From source file:de.tudarmstadt.ukp.dkpro.tc.crfsuite.task.CRFSuiteTestTask.java

private static String captureProcessOutput(Process aProcess) {
    InputStream src = aProcess.getInputStream();
    Scanner sc = new Scanner(src);
    StringBuilder dest = new StringBuilder();
    while (sc.hasNextLine()) {
        String l = sc.nextLine();
        dest.append(l + "\n");
    }//from w  w w .  ja va  2 s .  co m
    sc.close();
    return dest.toString();
}

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

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

    Configuration conf = new Configuration();

    if (numPartitions <= 0) {
        throw new IllegalArgumentException("numPartitions <= 0");
    }//from w  ww. ja v a  2s .  c  o m

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

    if (fs.exists(outputPath)) {
        throw new IllegalArgumentException("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]);
    }

    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);
    DataConverter converter = new DataConverter(dataset);
    int nbInstances = dataset.nbInstances();

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

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

        // write the tuple in files[tuple.label]
        Instance instance = converter.convert(id++, line);
        int label = instance.label;
        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) {
        file.close();
    }

    // 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);
     */
}

From source file:com.zimbra.cs.mailbox.util.MetadataDump.java

private static String loadFromFile(File file) throws ServiceException {
    try {/*from   w  w  w  . ja  v a 2s.  c  o m*/
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            Scanner scan = new Scanner(fis);
            StringBuilder builder = new StringBuilder();
            while (scan.hasNextLine()) {
                builder.append(scan.nextLine());
            }
            return builder.toString();
        } finally {
            if (fis != null)
                fis.close();
        }
    } catch (IOException e) {
        throw ServiceException.FAILURE("IOException while reading from " + file.getAbsolutePath(), e);
    }
}

From source file:magixcel.Main.java

private static void setup() {
    Scanner s = new Scanner(System.in);
    boolean doContinue = false;

    //        Intro
    printLogo();/*from  w  w  w  .  j  a v  a  2  s .  c  om*/
    System.out.println("Welcome to MagiXcel");
    System.out.println("Current encoding is " + Charset.defaultCharset());

    //        Table name
    while (!doContinue) {
        System.out.println("Type your desired table name:");
        if (s.hasNextLine()) {
            Table.setTableName(s.nextLine());
        }

        doContinue = isThisOk(s);
    }
    doContinue = false;

    //        File path
    String path = "";
    while (!doContinue) {
        System.out.println("Enter FULL path of .csv or .txt file you wish to convert");
        if (s.hasNextLine()) {
            path = StringEscapeUtils.escapeJava(s.nextLine());
        }
        FileMan.setPath(path);
        if (FileMan.currentPathIsValid()) {
            doContinue = isThisOk(s);
        } else {
            System.out.println("ERROR: Invalid Path");
            doContinue = false;
        }

    }
    doContinue = false;

    //        File setup
    FileMan.getLinesFromFile();
    Table.setColumnNames(FileMan.takeFirstRow());
    FileMan.addRowsToTable();

    //        Choose output mode
    System.out.println("Choose output mode:");
    OutputFormat.printOutputOptionsNumbered();
    int selectedOption = -1;
    while (!doContinue) {
        if (s.hasNextLine()) {
            selectedOption = Integer.parseInt(s.nextLine());
        }
        doContinue = OutputFormat.chooseOption(selectedOption, s);
    }

    doContinue = false;
    OutputFormat.format();

    FileMan.writeToFile();

    System.out.println("Thank you for using MagiXcel!");
    System.out.println("Press Enter to exit");
    s.nextLine();
    s.close();

}

From source file:org.arasthel.almeribus.utils.LoadFromWeb.java

private static int loadCookie() throws ClientProtocolException, IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet get = new HttpGet("http://m.surbus.com/tiempo-espera");
    HttpResponse response = httpClient.execute(get);
    HttpEntity entity = response.getEntity();

    if (entity == null) {
        return ERROR_IO;
    }//from  w  w w .  ja  v a 2  s  .com

    Scanner scan = new Scanner(entity.getContent());
    StringBuilder strBuilder = new StringBuilder();
    while (scan.hasNextLine()) {
        strBuilder.append(scan.nextLine());
    }
    scan.close();
    if (!strBuilder.toString().contains("id=\"blockResult\" class=\"messageResult\"")) {
        return MANTENIMIENTO;
    }

    List<Cookie> cookies = httpClient.getCookieStore().getCookies();
    for (Cookie c : cookies) {
        if (c.getName().contains("ASP.NET_SessionId")) {
            cookie = c.getValue();
        }
    }
    return TODO_OK;
}

From source file:org.arasthel.almeribus.utils.LoadFromWeb.java

public static String getMensajeDesarrollador() {
    String mensaje = null;//from  w  w w .  j  a va  2s  .c om
    try {
        URL url = new URL("http://arasthel.byethost14.com/almeribus/message.html?token="
                + new Random().nextInt(Integer.MAX_VALUE));
        URLConnection connection = url.openConnection();
        connection.setUseCaches(false);
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(15000);
        Scanner scan = new Scanner(connection.getInputStream());
        StringBuilder strBuilder = new StringBuilder();
        while (scan.hasNextLine()) {
            strBuilder.append(scan.nextLine());
        }
        scan.close();
        mensaje = strBuilder.toString();
    } catch (Exception e) {

    }
    return mensaje;
}

From source file:org.arasthel.almeribus.utils.LoadFromWeb.java

public static String getUltimaVersion() {
    String mensaje = null;/*w  ww  .  j  a v a2 s.  c  o m*/
    try {
        URL url = new URL("http://arasthel.byethost14.com/almeribus/version.html?token="
                + new Random().nextInt(Integer.MAX_VALUE));
        URLConnection connection = url.openConnection();
        connection.setUseCaches(false);
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(15000);
        Scanner scan = new Scanner(connection.getInputStream());
        StringBuilder strBuilder = new StringBuilder();
        while (scan.hasNextLine()) {
            strBuilder.append(scan.nextLine());
        }
        scan.close();
        mensaje = strBuilder.toString();
    } catch (Exception e) {

    }
    return mensaje;
}

From source file:com.ibm.iotf.sample.devicemgmt.device.RasPiFirmwareHandlerSample.java

private static String getInstallLog() throws FileNotFoundException {
    File file = new File(INSTALL_LOG_FILE);
    Scanner scanner = new Scanner(file);
    StringBuilder sb = new StringBuilder();
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        sb.append(line);/* ww w .  j av a2  s .c o m*/
        sb.append('\n');
    }
    scanner.close();
    return sb.toString();
}

From source file:org.apache.accumulo.core.client.mapreduce.lib.util.ConfiguratorBase.java

/**
 * Reads from the token file in distributed cache. Currently, the token file stores data separated by colons e.g. principal:token_class:token
 * //from  ww  w. j  av  a 2  s .  c  om
 * @param conf
 *          the Hadoop context for the configured job
 * @return path to the token file as a String
 * @since 1.6.0
 * @see #setConnectorInfo(Class, Configuration, String, AuthenticationToken)
 */
public static AuthenticationToken getTokenFromFile(Configuration conf, String principal, String tokenFile) {
    FSDataInputStream in = null;
    try {
        URI[] uris = DistributedCacheHelper.getCacheFiles(conf);
        Path path = null;
        for (URI u : uris) {
            if (u.toString().equals(tokenFile)) {
                path = new Path(u);
            }
        }
        if (path == null) {
            throw new IllegalArgumentException(
                    "Couldn't find password file called \"" + tokenFile + "\" in cache.");
        }
        FileSystem fs = FileSystem.get(conf);
        in = fs.open(path);
    } catch (IOException e) {
        throw new IllegalArgumentException("Couldn't open password file called \"" + tokenFile + "\".");
    }
    java.util.Scanner fileScanner = new java.util.Scanner(in);
    try {
        while (fileScanner.hasNextLine()) {
            Credentials creds = Credentials.deserialize(fileScanner.nextLine());
            if (principal.equals(creds.getPrincipal())) {
                return creds.getToken();
            }
        }
        throw new IllegalArgumentException(
                "Couldn't find token for user \"" + principal + "\" in file \"" + tokenFile + "\"");
    } finally {
        if (fileScanner != null && fileScanner.ioException() == null)
            fileScanner.close();
        else if (fileScanner.ioException() != null)
            throw new RuntimeException(fileScanner.ioException());
    }
}