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: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  . j  a va2  s .  co 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.ibm.iotf.sample.client.gateway.devicemgmt.GatewayFirmwareHandlerSample.java

private static InstalStatus ParseInstallLog() throws FileNotFoundException {
    try {//ww w. ja v  a2s.  c  om
        File file = new File(INSTALL_LOG_FILE);
        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line.contains(DEPENDENCY_ERROR_MSG)) {
                scanner.close();
                return InstalStatus.DEPENDENCY_ERROR;
            } else if (line.contains(ERROR_MSG)) {
                scanner.close();
                return InstalStatus.ERROR;
            }
        }
        scanner.close();
    } catch (FileNotFoundException e) {
        throw e;
    }
    return InstalStatus.SUCCESS;
}

From source file:org.hyperic.plugin.vrealize.automation.VRAUtils.java

public static String readFile(String filePath) {
    Scanner scanner = null;
    StringBuilder result = new StringBuilder();
    try {//from   w  w  w. ja va2 s.  c  om
        result = new StringBuilder();
        scanner = new Scanner(new FileInputStream(filePath));

        while (scanner.hasNextLine()) {
            result.append(String.format("%s%n", scanner.nextLine()));
        }
    } catch (Exception e) {
        return null;
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
    return (result == null) ? null : result.toString();
}

From source file:com.evolveum.midpoint.util.MiscUtil.java

public static List<String> splitLines(String string) {
    List<String> lines = new ArrayList<String>();
    Scanner scanner = new Scanner(string);
    while (scanner.hasNextLine()) {
        lines.add(scanner.nextLine());/*from  www .j  ava2 s  .c  o  m*/
    }
    return lines;
}

From source file:org.eclipse.lyo.testsuite.server.util.OSLCUtils.java

public static String readFileAsString(File f) {

    StringBuilder stringBuilder = new StringBuilder();
    Scanner scanner = null;
    try {/*  w ww.j a  v a 2s.  c om*/
        scanner = new Scanner(f);
    } catch (FileNotFoundException e) {
        return null;
    }

    try {
        while (scanner.hasNextLine()) {
            stringBuilder.append(scanner.nextLine() + "\n");
        }
    } finally {
        scanner.close();
    }
    return stringBuilder.toString();
}

From source file:com.all.login.services.LoginDatabaseAccess.java

private static LoginDatabase loadDatabase(File file) {
    LoginDatabase db = null;/*w w  w . j  a  va  2s. c om*/
    if (file.exists()) {
        Scanner scanner = null;
        try {
            StringBuilder text = new StringBuilder();
            String NL = System.getProperty("line.separator");
            scanner = new Scanner(new FileInputStream(file));
            while (scanner.hasNextLine()) {
                text.append(scanner.nextLine() + NL);
            }
            db = JsonConverter.toBean(text.toString(), LoginDatabase.class);
        } catch (Exception e) {
            LOG.error("Could not deserialize LoginDatabase.", e);
        } finally {
            try {
                scanner.close();
            } catch (Exception e) {
                LOG.error(e, e);
            }
        }
    }
    if (db == null) {
        db = new LoginDatabase();
    }
    return db;
}

From source file:com.ibm.iotf.sample.client.gateway.devicemgmt.GatewayFirmwareHandlerSample.java

public static String getInstallLog(String fileName) throws FileNotFoundException {
    File file = new File(fileName);
    Scanner scanner = new Scanner(file);
    StringBuilder sb = new StringBuilder();
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        sb.append(line);/*from w ww.j a va 2s  .c o  m*/
        sb.append('\n');
    }
    scanner.close();
    return sb.toString();
}

From source file:org.apache.solr.analytics.facet.AbstractAnalyticsFacetCloudTest.java

public static ModifiableSolrParams fileToParams(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 w  w  .j av  a2  s  .  c  om
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("q", "*:*");
        params.set("indent", "true");
        params.set("olap", "true");
        params.set("rows", "0");
        while (file.hasNextLine()) {
            String line = file.nextLine();
            line = line.trim();
            if (StringUtils.isBlank(line) || line.startsWith("#")) {
                continue;
            }
            String[] param = line.split("=");
            params.set(param[0], param[1]);
        }
        return params;
    } finally {
        IOUtils.closeWhileHandlingException(file, in);
    }
}

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

public static int cargarParadasLinea(Context context, int numeroLinea) {
    if (!isConnectionEnabled(context)) {
        return SIN_CONEXION;
    }//from   w w  w.  j av a  2s.c  o m
    try {
        if (cookie == null) {
            if (loadCookie() == MANTENIMIENTO) {
                return MANTENIMIENTO;
            }
        }
        URL url = new URL(QUERY_ADDRESS_PARADAS_LINEA + numeroLinea);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(15000);
        connection.setReadTimeout(30000);
        connection.setRequestProperty("Cookie", "ASP.NET_SessionId=" + cookie);
        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.connect();
        Scanner scan = new Scanner(connection.getInputStream());
        StringBuilder strBuilder = new StringBuilder();
        while (scan.hasNextLine()) {
            strBuilder.append(scan.nextLine());
        }
        scan.close();
        JSONObject json = new JSONObject(strBuilder.toString());
        Log.d("Almeribus", strBuilder.toString());
        boolean isSuccessful = json.getBoolean("success");
        if (isSuccessful) {
            DataStorage.DBHelper.eliminarParadasLinea(numeroLinea);
            Linea l = new Linea(numeroLinea);
            JSONArray list = json.getJSONArray("list");
            Parada primeraParada = null;
            Parada paradaAnterior = null;
            for (int i = 0; i < list.length(); i++) {
                JSONObject paradaJSON = list.getJSONObject(i);
                int numeroParada = paradaJSON.getInt("IdBusStop");
                String nombreParada = paradaJSON.getString("Name");
                Parada p = null;
                if (DataStorage.paradas.containsKey(numeroParada)) {
                    p = DataStorage.paradas.get(numeroParada);
                    p.setNombre(nombreParada);
                } else {
                    p = new Parada(numeroParada, nombreParada);
                }
                synchronized (DataStorage.DBHelper) {
                    DataStorage.DBHelper.addInfoParada(numeroParada, nombreParada);
                }
                p.addLinea(l.getNumero());
                if (paradaAnterior != null) {
                    p.setAnterior(paradaAnterior.getId(), numeroLinea);
                }

                if (i == 0) {
                    primeraParada = p;
                } else if (i == list.length() - 1) {
                    primeraParada.setAnterior(p.getId(), numeroLinea);
                    p.setSiguiente(primeraParada.getId(), numeroLinea);
                }

                if (paradaAnterior != null) {
                    paradaAnterior.setSiguiente(p.getId(), numeroLinea);
                }

                paradaAnterior = p;
                synchronized (DataStorage.paradas) {
                    if (DataStorage.paradas.containsKey(numeroParada)) {
                        DataStorage.paradas.remove(numeroParada);
                    }
                    DataStorage.paradas.put(numeroParada, p);
                }

                l.addParada(p);
            }
            DataStorage.lineas.put(numeroLinea, l);
            for (Parada parada : l.getParadas()) {
                synchronized (DataStorage.DBHelper) {
                    try {
                        DataStorage.DBHelper.addParadaLinea(parada.getId(), numeroLinea,
                                parada.getSiguiente(numeroLinea));
                    } catch (ParadaNotFoundException e) {
                        DataStorage.DBHelper.addParadaLinea(parada.getId(), numeroLinea, 0);
                    }
                }
            }
            return TODO_OK;
        } else {
            return ERROR_IO;
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        return ERROR_IO;
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ERROR_IO;
}

From source file:com.crowsofwar.gorecore.config.ConfigLoader.java

/**
 * Load a Map containing the YAML configurations at that path.
 * /* w  w  w .j  ava2s  . c o  m*/
 * @param path
 *            Path starting at ".minecraft/config/"
 * 
 * @throws ConfigurationException
 *             when an error occurs while trying to read the file
 */
private static Map<String, Object> loadMap(String path) {

    try {

        String contents = "";

        File file = new File("config/" + path);
        file.getParentFile().mkdirs();
        file.createNewFile();

        Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine())
            contents += scanner.nextLine() + "\n";
        scanner.close();

        Yaml yaml = new Yaml();
        Map<String, Object> map = (Map) yaml.load(contents);

        return map == null ? new HashMap<>() : map;

    } catch (IOException e) {
        throw new ConfigurationException.LoadingException(
                "Exception trying to load config file at " + new File("config/" + path).getAbsolutePath(), e);
    } catch (ClassCastException e) {

        System.out.println("ConfigLoader- warning: File at " + path + " was not a map; ignored contents.");
        return new HashMap<>();

    } catch (ScannerException e) {

        throw new ConfigurationException.LoadingException("Malformed YAML file at config/" + path, e);

    } catch (Exception e) {

        // TODO use a logger
        System.err.println("Error while loading config at 'config/" + path + "':");
        throw e;

    }

}