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:BlockingQueueTest.java

/**
 * Searches a file for a given keyword and prints all matching lines.
 * @param file the file to search/*  w  w w  .  j a  v  a2  s .  co  m*/
 */
public void search(File file) throws IOException {
    Scanner in = new Scanner(new FileInputStream(file));
    int lineNumber = 0;
    while (in.hasNextLine()) {
        lineNumber++;
        String line = in.nextLine();
        if (line.contains(keyword))
            System.out.printf("%s:%d:%s%n", file.getPath(), lineNumber, line);
    }
    in.close();
}

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

private String filterEmptyLine(String content) {
    final StringBuilder sb = new StringBuilder();
    Scanner scanner = null;
    try {//  w w w.  j  ava 2 s . co  m
        scanner = new Scanner(content);
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            if (line != null) {
                // remove extra space at line's end
                line = StringUtils.stripEnd(line, " ");
                if (line.isEmpty()) {
                    line = " ";
                }
            }
            sb.append(line).append('\n');
        }
    } finally {
        if (null != scanner) {
            scanner.close();
        }
    }

    return sb.toString();
}

From source file:org.jhk.pulsing.sandbox.timeline.cli.CommandCli.java

private void run() {
    userCache.loadUsers(CommandCli.class.getResourceAsStream(USERS_CSV));
    userCache.loadFollowers(CommandCli.class.getResourceAsStream(FOLLOWS_CSV));

    queue.addConsumer(consumer);// ww w . j  a v  a  2s  .co m
    queue.run();

    // base load of tweets
    Util.readStream(CommandCli.class.getResourceAsStream(TWEETS_CSV), line -> {
        consumer.processMessage(new Message(line));
    });

    LOGGER.info("Ready for input...");
    Scanner input = new Scanner(System.in);
    while (input.hasNextLine()) {
        String command = input.nextLine();
        processCommandLine(command);
    }

    input.close();
    queue.cancel();
}

From source file:org.apache.streams.datasift.serializer.DatasiftInstagramActivitySerializerTest.java

@Test
@Override/*from   w w w.  jav  a2 s.  c om*/
public void testConversion() throws Exception {

    Scanner scanner = StreamsScannerUtil.getInstance("/instagram_datasift_json.txt");

    String line = null;
    while (scanner.hasNextLine()) {
        line = scanner.nextLine();
        Datasift item = MAPPER.readValue(line, Datasift.class);
        testConversion(item);
        String json = MAPPER.writeValueAsString(item);
        testDeserNoNull(json);
        testDeserNoAddProps(json);
    }
}

From source file:org.apache.streams.datasift.serializer.DatasiftTwitterActivitySerializerTest.java

@Test
@Override/*from   w ww  . ja va 2s .co m*/
public void testConversion() throws Exception {

    Scanner scanner = StreamsScannerUtil.getInstance("/twitter_datasift_json.txt");

    String line = null;
    while (scanner.hasNextLine()) {
        line = scanner.nextLine();
        Datasift item = MAPPER.readValue(line, Datasift.class);
        testConversion(item);
        String json = MAPPER.writeValueAsString(item);
        testDeserNoNull(json);
        testDeserNoAddProps(json);
    }
}

From source file:net.bashtech.geobot.BotManager.java

public static String postDataLinkShortener(String postData) {
    URL url;//from  w w  w. j  av  a  2  s.  c  o m
    HttpURLConnection conn;
    postData = "{\"longUrl\": \"" + postData + "\"}";

    try {
        url = new URL("https://www.googleapis.com/urlshortener/v1/url");

        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("User-Agent", "CoeBot");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length));

        PrintWriter out = new PrintWriter(conn.getOutputStream());
        System.out.println(postData);
        out.print(postData);
        out.close();

        String response = "";

        Scanner inStream = new Scanner(conn.getInputStream());

        while (inStream.hasNextLine())
            response += (inStream.nextLine());

        inStream.close();
        return response;

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return "";

}

From source file:net.bashtech.geobot.BotManager.java

public static String putRemoteData(String urlString, String postData) throws IOException {

    URL url;/*from ww w  .  j  a va2 s .c om*/
    HttpURLConnection conn;

    try {
        url = new URL(urlString);

        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("PUT");

        conn.setFixedLengthStreamingMode(postData.getBytes().length);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/vnd.twitchtv.v2+json");
        conn.setRequestProperty("Authorization", "OAuth " + BotManager.getInstance().krakenOAuthToken);
        conn.setRequestProperty("Client-ID", BotManager.getInstance().krakenClientID);
        // conn.setConnectTimeout(5 * 1000);
        // conn.setReadTimeout(5 * 1000);

        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(postData);
        out.close();

        String response = "";

        Scanner inStream = new Scanner(conn.getInputStream());

        while (inStream.hasNextLine())
            response += (inStream.nextLine());

        return response;

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    }

    return "";
}

From source file:org.apache.streams.datasift.serializer.DatasiftEventClassifierTest.java

@Test
public void testTwitterDetection() throws Exception {

    Scanner scanner = StreamsScannerUtil.getInstance("/twitter_datasift_json.txt");

    String line = null;/*from   w  w  w. j  a va  2  s .c om*/
    while (scanner.hasNextLine()) {
        line = scanner.nextLine();
        Datasift datasift = MAPPER.readValue(line, Datasift.class);
        assert (DatasiftEventClassifier.detectClass(datasift) == Twitter.class);
        assert (DatasiftEventClassifier.bestSerializer(datasift) instanceof DatasiftTwitterActivitySerializer);
    }
}

From source file:net.bashtech.geobot.BotManager.java

public static String postRemoteDataTwitch(String urlString, String postData, int krakenVersion) {
    URL url;//from ww w.j a v a2  s  .c o  m
    HttpURLConnection conn;

    try {
        url = new URL(urlString);

        conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        conn.setFixedLengthStreamingMode(postData.getBytes().length);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Accept", "application/vnd.twitchtv.v" + krakenVersion + "+json");
        conn.setRequestProperty("Authorization", "OAuth " + BotManager.getInstance().krakenOAuthToken);
        conn.setRequestProperty("Client-ID", BotManager.getInstance().krakenClientID);
        // conn.setConnectTimeout(5 * 1000);
        // conn.setReadTimeout(5 * 1000);

        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(postData);
        out.close();

        String response = "";

        Scanner inStream = new Scanner(conn.getInputStream());

        while (inStream.hasNextLine())
            response += (inStream.nextLine());

        inStream.close();
        return response;

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return "";
}

From source file:org.apache.streams.datasift.serializer.DatasiftEventClassifierTest.java

@Test
public void testInstagramDetection() throws Exception {

    Scanner scanner = StreamsScannerUtil.getInstance("/instagram_datasift_json.txt");

    String line = null;//  w ww  .  ja v  a2 s. co m
    while (scanner.hasNextLine()) {
        line = scanner.nextLine();
        Datasift datasift = MAPPER.readValue(line, Datasift.class);
        assert (DatasiftEventClassifier.detectClass(datasift) == Instagram.class);
        assert (DatasiftEventClassifier
                .bestSerializer(datasift) instanceof DatasiftInstagramActivitySerializer);
    }
}