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:org.arasthel.almeribus.utils.LoadFromWeb.java

public static String getMensajeDesarrollador() {
    String mensaje = null;/*ww  w.  jav a2s .c  o  m*/
    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;/*from ww  w .j a v a2s.c  om*/
    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.microsoft.intellij.util.WAHelper.java

/**
 * This API compares if two files content is identical. It ignores extra
 * spaces and new lines while comparing/*w w w.j a  v  a 2  s  .  co  m*/
 *
 * @param sourceFile
 * @param destFile
 * @return
 * @throws Exception
 */
public static boolean isFilesIdentical(URL sourceFile, File destFile) throws Exception {
    try {
        Scanner sourceFileScanner = new Scanner(sourceFile.openStream());
        Scanner destFileScanner = new Scanner(destFile);

        while (sourceFileScanner.hasNext()) {
            /*
             * If source file is having next token then destination file
            * also should have next token, else they are not identical.
            */
            if (!destFileScanner.hasNext()) {
                destFileScanner.close();
                sourceFileScanner.close();
                return false;
            }
            if (!sourceFileScanner.next().equals(destFileScanner.next())) {
                sourceFileScanner.close();
                destFileScanner.close();
                return false;
            }
        }
        /*
        * Handling the case where source file is empty and destination file
        * is having text
        */
        if (destFileScanner.hasNext()) {
            destFileScanner.close();
            sourceFileScanner.close();
            return false;
        } else {
            destFileScanner.close();
            sourceFileScanner.close();
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } /*finally {
      sourceFile.close();
      }*/
}

From source file:de.rub.syssec.saaf.analysis.steps.hash.SSDeep.java

protected static String calculateFuzzyHash(File f) throws IOException {
    String hash = null;// w w w  . java 2 s. c om

    if (SSDEEP_PATH != null) {
        ProcessBuilder pb = new ProcessBuilder(SSDEEP_PATH, f.getAbsolutePath());
        Process proc;
        Scanner in = null;
        try {
            proc = pb.start();
            // Start reading from the program
            in = new Scanner(proc.getInputStream());
            while (in.hasNextLine()) {
                hash = in.nextLine();
            }
            if (hash != null)
                return hash.substring(0, hash.lastIndexOf(","));
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (Exception ignored) {
            }
        }
    } else {
        LOGGER.warn("exec_ssdeep could not be found in saaf.conf");
    }
    return "";
}

From source file:org.springframework.hateoas.alps.JacksonSerializationTest.java

private static String read(Resource resource) throws IOException {

    Scanner scanner = null;

    try {/* w w  w. j  a va  2s .  com*/

        scanner = new Scanner(resource.getInputStream());
        StringBuilder builder = new StringBuilder();

        while (scanner.hasNextLine()) {

            builder.append(scanner.nextLine());

            if (scanner.hasNextLine()) {
                builder.append(System.getProperty("line.separator"));
            }
        }

        return builder.toString();

    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
}

From source file:edu.cmu.cs.lti.ark.fn.utils.LemmatizeStuff.java

private static void run() throws FileNotFoundException {
    Scanner sc = new Scanner(new FileInputStream(infilename));
    PrintStream ps = new PrintStream(new FileOutputStream(outfilename));
    while (sc.hasNextLine()) {
        String line = sc.nextLine();
        ps.print(line + "\t");
        String[] toks = line.trim().split("\\s");
        int sentLen = Integer.parseInt(toks[0]);
        for (int i = 0; i < sentLen; i++) {
            String lemma = lemmatizer.getLemma(toks[i + 1].toLowerCase(), toks[i + 1 + sentLen]);
            ps.print(lemma + "\t");
        }//from  w  ww. j  a  va2  s .c o m
        ps.println();
    }
    sc.close();
    closeQuietly(ps);
}

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

public static ResultTiempo calcularTiempo(Context context, int parada, int linea) {
    ResultTiempo result = new ResultTiempo();
    if (!isConnectionEnabled(context)) {
        Log.d("AlmeriBus", "No hay conexin");
        result.setTiempo(ERROR_IO);//from   w  ww.ja  va  2 s .  c  o m
    }
    try {
        loadCookie();
        URL url = new URL(QUERY_ADDRESS_TIEMPO_PARADA + linea + "/" + parada + "/" + "3B5579C8FFD6");
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(10000);
        connection.setReadTimeout(15000);
        connection.setRequestProperty("REFERER", "http://m.surbus.com/tiempo-espera/");
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        connection.setRequestProperty("Cookie", "ASP.NET_SessionId=" + cookie);
        Scanner scan = new Scanner(connection.getInputStream());
        StringBuilder strBuilder = new StringBuilder();
        while (scan.hasNextLine()) {
            strBuilder.append(scan.nextLine());
        }
        scan.close();
        Log.d("Almeribus", strBuilder.toString());
        JSONObject json = new JSONObject(strBuilder.toString());
        boolean isSuccessful = json.getBoolean("success");
        int type = json.getInt("waitTimeType");
        if (isSuccessful && type > 0) {
            int time = json.getInt("waitTime");
            if (time == Integer.MAX_VALUE) {
                time = NO_DATOS;
            }
            if (time <= 0) {
                time = 0;
            }
            result.setTiempo(time);
            result.setTiempoTexto(json.getString("waitTimeString"));
        } else {
            result.setTiempo(NO_DATOS);
        }
    } catch (Exception e) {
        Log.d("Almeribus", e.toString());
        result.setTiempo(ERROR_IO);
        return result;
    }
    return result;
}

From source file:com.amazonaws.services.dynamodbv2.online.index.ViolationDetector.java

private static void confirmDelete() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Are you sure to delete all violations on the table? y/n ");
    String op = scanner.nextLine().trim();
    if (op.equalsIgnoreCase("n")) {
        logger.info("User decided to not delete. Exiting...");
        scanner.close();
        System.exit(0);/*from   w ww  .j  a v a  2  s  . c  o m*/
    } else if (op.equalsIgnoreCase("y")) {
        logger.info("Confirmed deletion. Will delete all violations in the table...");
        scanner.close();
    } else {
        logger.error("Invalid entry by the user. Will exit.");
        System.out.println("Please type in y/n, exiting...");
        scanner.close();
        System.exit(0);
    }
}

From source file:com.calamp.services.kinesis.events.writer.CalAmpEventWriter.java

public static List<CalAmpEvent> readEventsFromFile(String filePath) throws IOException {
    // Repeatedly send stock trades with a some milliseconds wait in between
    ArrayList<CalAmpEvent> eList = new ArrayList<CalAmpEvent>();
    Scanner scan = new Scanner(new FileReader(filePath));
    int count = 0;
    while (scan.hasNext()) {
        CalAmpEvent cae = CalAmpEvent.fromJsonAsString(scan.nextLine());
        eList.add(cae);//w  w  w .  ja  v a2  s .  c  o m

        if (count % CalAmpParameters.maxRecordsPerPut == 0) {
            System.out.println(cae.toJsonAsString());
        }
        count++;
        //System.out.println( CalAmpEvent.fromJsonAsString( cae.toJsonAsString()) );
    }
    if (scan != null) {
        scan.close();
    }
    return eList;
}

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   ww  w.  j av  a  2s.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();
    }

}