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.google.api.ads.adwords.jaxws.extensions.kratu.KratuMain.java

/**
 * Prints the sample properties file on the default output.
 *//*www .j  av  a 2 s.com*/
private static void printSamplePropertiesFile() {

    System.out.println("\n  File: kratubackend-sample.properties example");

    ClassPathResource sampleFile = new ClassPathResource(CLASSPATH_AW_REPORT_MODEL_PROPERTIES_LOCATION);
    Scanner fileScanner = null;
    try {
        fileScanner = new Scanner(sampleFile.getInputStream());
        while (fileScanner.hasNext()) {
            System.out.println(fileScanner.nextLine());
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileScanner != null) {
            fileScanner.close();
        }
    }
}

From source file:org.envirocar.app.util.Util.java

public static CharSequence consumeInputStream(InputStream in) throws IOException {
    Scanner sc = new Scanner(in, "UTF-8");

    StringBuilder sb = new StringBuilder();
    String sep = System.getProperty("line.separator");
    while (sc.hasNext()) {
        sb.append(sc.nextLine());//from w ww  . java 2  s.co  m
        sb.append(sep);
    }
    sc.close();

    return sb;
}

From source file:PostTest.java

/**
 * Makes a POST request and returns the server response.
 * @param urlString the URL to post to//w ww  .j a  v  a 2 s .c om
 * @param nameValuePairs a map of name/value pairs to supply in the request.
 * @return the server reply (either from the input stream or the error stream)
 */
public static String doPost(String urlString, Map<String, String> nameValuePairs) throws IOException {
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);

    PrintWriter out = new PrintWriter(connection.getOutputStream());

    boolean first = true;
    for (Map.Entry<String, String> pair : nameValuePairs.entrySet()) {
        if (first)
            first = false;
        else
            out.print('&');
        String name = pair.getKey();
        String value = pair.getValue();
        out.print(name);
        out.print('=');
        out.print(URLEncoder.encode(value, "UTF-8"));
    }

    out.close();

    Scanner in;
    StringBuilder response = new StringBuilder();
    try {
        in = new Scanner(connection.getInputStream());
    } catch (IOException e) {
        if (!(connection instanceof HttpURLConnection))
            throw e;
        InputStream err = ((HttpURLConnection) connection).getErrorStream();
        if (err == null)
            throw e;
        in = new Scanner(err);
    }

    while (in.hasNextLine()) {
        response.append(in.nextLine());
        response.append("\n");
    }

    in.close();
    return response.toString();
}

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  w  w.jav a2  s . c  o m
        sb.append('\n');
    }
    scanner.close();
    return sb.toString();
}

From source file:com.mycompany.songbitmaven.RecommendationController.java

public static SongDataSet trackLookup(String text) {
    Api api = Api.DEFAULT_API;//from www. j  av a 2s. c  om
    final TrackSearchRequest request = api.searchTracks(text).market("US").build();

    try {
        final Page<com.wrapper.spotify.models.Track> trackSearchResult = request.get();
        String jsonURL = trackSearchResult.getNext();

        URL myurl = null;
        try {
            myurl = new URL(jsonURL);
        } catch (Exception e) {
            System.out.println("Improper URL " + jsonURL);
            return null;
        }

        // read from the URL
        Scanner scan = null;
        try {
            scan = new Scanner(myurl.openStream());
        } catch (Exception e) {
            System.out.println("Could not connect to " + jsonURL);
            return null;
        }

        String str = new String();
        while (scan.hasNext()) {
            str += scan.nextLine() + "\n";
        }
        scan.close();

        Gson gson = new Gson();

        System.out.println(jsonURL);
        SongDataSet dataset = gson.fromJson(str, SongDataSet.class);

        return dataset;
    } catch (Exception e) {
        System.out.println("Something went wrong!" + e.getMessage());
        return null;
    }
}

From source file:com.google.api.ads.adwords.awreporting.server.AwReportingServer.java

/**
 * Prints the sample properties file on the default output.
 *//*from  w w w .  jav a2  s  .  co  m*/
private static void printSamplePropertiesFile() {

    System.out.println("\n  File: aw-reporting-server-sample.properties example");

    ClassPathResource sampleFile = new ClassPathResource(DAFAULT_PROPERTIES_LOCATION);
    Scanner fileScanner = null;
    try {
        fileScanner = new Scanner(sampleFile.getInputStream());
        while (fileScanner.hasNext()) {
            System.out.println(fileScanner.nextLine());
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileScanner != null) {
            fileScanner.close();
        }
    }
}

From source file:com.alibaba.dubbo.qos.textui.TTable.java

/**
 * visible width for the given string.//from  w w w. jav a2  s.  co m
 *
 * for example: "abc\n1234"'s width is 4.
 *
 * @param string the given string
 * @return visible width
 */
private static int width(String string) {
    int maxWidth = 0;
    final Scanner scanner = new Scanner(new StringReader(string));
    try {
        while (scanner.hasNextLine()) {
            maxWidth = max(length(scanner.nextLine()), maxWidth);
        }
    } finally {
        scanner.close();
    }
    return maxWidth;
}

From source file:com.qmetry.qaf.automation.testng.report.ReporterUtil.java

public static String execHostName(String execCommand) {
    InputStream stream;//w  w  w  .  j a  v  a  2  s  .c  o m
    Scanner s;
    try {
        Process proc = Runtime.getRuntime().exec(execCommand);
        stream = proc.getInputStream();
        if (stream != null) {
            s = new Scanner(stream);
            s.useDelimiter("\\A");
            String val = s.hasNext() ? s.next() : "";
            stream.close();
            s.close();
            return val;
        }
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
    return "";
}

From source file:com.google.api.ads.adwords.jaxws.extensions.AwReporting.java

/**
 * Prints the sample properties file on the default output.
 *///from  w  w  w  .j  a v a2s  .c om
private static void printSamplePropertiesFile() {

    System.out.println("\n  File: aw-report-sample.properties example");

    ClassPathResource sampleFile = new ClassPathResource("aw-report-sample.properties");
    Scanner fileScanner = null;
    try {
        fileScanner = new Scanner(sampleFile.getInputStream());
        while (fileScanner.hasNext()) {
            System.out.println(fileScanner.nextLine());
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileScanner != null) {
            fileScanner.close();
        }
    }
}

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

private static LoginDatabase loadDatabase(File file) {
    LoginDatabase db = null;//w w  w. j  av  a 2 s. co m
    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;
}