try (Scanner scanner = new Scanner(response)) { String responseBody = scanner.useDelimiter("\\A").next(); System.out.println(responseBody); }
/** * Returns true if this input stream has more input (including whitespace). * Use this method to know whether the next call to {@link #readChar()} will succeed. * This method is functionally equivalent to {@link #hasNextLine()}. * * @return {@code true} if this input stream has more input (including whitespace); * {@code false} otherwise */ public boolean hasNextChar() { scanner.useDelimiter(EMPTY_PATTERN); boolean result = scanner.hasNext(); scanner.useDelimiter(WHITESPACE_PATTERN); return result; }
/** * Reads and returns the remainder of this input stream, as a string. * * @return the remainder of this input stream, as a string */ public String readAll() { if (!scanner.hasNextLine()) return ""; String result = scanner.useDelimiter(EVERYTHING_PATTERN).next(); // not that important to reset delimeter, since now scanner is empty scanner.useDelimiter(WHITESPACE_PATTERN); // but let's do it anyway return result; }
String text = "row1\tblah\tblah\tblah\n" + "row2\t1\t2\t3\t4\r\n" + "row3\tA\tB\tC\r" + "row4"; System.out.println(text); // row1 blah blah blah // row2 1 2 3 4 // row3 A B C // row4 List<List<String>> input = new ArrayList<List<String>>(); Scanner sc = new Scanner(text); while (sc.hasNextLine()) { Scanner lineSc = new Scanner(sc.nextLine()).useDelimiter("\t"); List<String> line = new ArrayList<String>(); while (lineSc.hasNext()) { line.add(lineSc.next()); } input.add(line); } System.out.println(input); // [[row1, blah, blah, blah], [row2, 1, 2, 3, 4], [row3, A, B, C], [row4]]
Scanner scanner = new Scanner("1|2|3|||6|"); scanner.useDelimiter("\\|"); while (scanner.hasNextInt()) { int x = scanner.nextInt(); System.out.println(x); }
Scanner sc = null; try { sc = new Scanner( lastLine ); sc = sc.useDelimiter( "\\D+" ); int i = 0; while ( sc.hasNext() ) { result[i++] = sc.nextBigInteger().longValue(); return null; File errorFile = new File( meta.getErrorFileName() ); if ( !errorFile.exists() ) { return null;
private void downloadSchema(URL url) throws IOException { String result; logger.debug("Downloading Solr schema info from: " + url); Scanner scanner = new Scanner(url.openStream()); result = scanner.useDelimiter("\\Z").next(); logger.debug("JSON Schema: " + result); Gson gson = new Gson(); Schema.SchemaWrapper schemaWrapper = gson.fromJson(result, Schema.SchemaWrapper.class); this.schema = schemaWrapper.getSchema(); }
@Override public int compareTo(BuildVersion other) { try (Scanner s1 = new Scanner(this.value); Scanner s2 = new Scanner(other.value)) { s1.useDelimiter("[.\\-+]"); s2.useDelimiter("[.\\-+]"); while (s1.hasNext() && s2.hasNext()) { int c; if (s1.hasNextInt() && s2.hasNextInt()) { c = Integer.compare(s1.nextInt(), s2.nextInt()); } else { c = s1.next().compareTo(s2.next()); } if (c != 0) { return c; } } if (s1.hasNext()) { return 1; } else if (s2.hasNext()) { return -1; } } return 0; } }
public JavaVersion(String version) { versions = new ArrayList<>(); Scanner s = new Scanner(version).useDelimiter("[^\\d]+"); while (s.hasNext()) { versions.add(s.nextInt()); } }
static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; }
fileQuickSort( new File("input"), new File("output")); System.out.println(); Scanner scanner = new Scanner( new BufferedInputStream( new FileInputStream( inputFile ), MAX_SIZE)); scanner.useDelimiter(","); File lowerFile = File.createTempFile("quicksort-","-lower.tmp",new File(".")); File greaterFile = File.createTempFile("quicksort-","-greater.tmp", new File(".")); PrintStream lower = createPrintStream(lowerFile);
public Object readObject(InputStream stream, String encoding, String expectedType) throws Exception { return readObject(new Scanner(stream, encoding).useDelimiter("\\A").next(), expectedType); }
/** * Returns true if standard input has more input (including whitespace). * Use this method to know whether the next call to {@link #readChar()} will succeed. * This method is functionally equivalent to {@link #hasNextLine()}. * * @return {@code true} if standard input has more input (including whitespace); * {@code false} otherwise */ public static boolean hasNextChar() { scanner.useDelimiter(EMPTY_PATTERN); boolean result = scanner.hasNext(); scanner.useDelimiter(WHITESPACE_PATTERN); return result; }
Scanner s = new Scanner(inputStream).useDelimiter("\\A"); String result = s.hasNext() ? s.next() : "";
@Override public Object parse(final InputStream jsonStream, final String charset) throws InvalidJsonException { Scanner sc = null; try { sc = new Scanner(jsonStream, charset); return parse(sc.useDelimiter("\\A").next()); } finally { if (sc != null) { sc.close(); } } }
String content = new Scanner(new File("filename")).useDelimiter("\\Z").next(); System.out.println(content);
protected String getResourceAsText(String resourceName) throws IOException { return new Scanner(BaseFunctionTest.class.getResourceAsStream(resourceName), "UTF-8").useDelimiter("\\A").next(); } }
public String getVersion() { try (InputStream is = getClass().getClassLoader().getResourceAsStream("META-INF/vertx/vertx-version.txt")) { if (is == null) { throw new IllegalStateException("Cannot find vertx-version.txt on classpath"); } try (Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A")) { return scanner.hasNext() ? scanner.next() : ""; } } catch (IOException e) { throw new IllegalStateException(e.getMessage()); } }
public static String convertStreamToString(java.io.InputStream is) { java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } }