List of usage examples for java.util Scanner Scanner
public Scanner(ReadableByteChannel source, Charset charset)
From source file:ninja.siden.internal.Testing.java
static String read(HttpResponse response) throws Exception { HttpEntity entity = response.getEntity(); if (entity == null) { return ""; }// w w w.j a va2s . c om try (Scanner scanner = new Scanner(entity.getContent(), StandardCharsets.UTF_8.name())) { return scanner.useDelimiter("\\A").next(); } }
From source file:de.ifgi.mosia.wpswfs.Util.java
public static String readContent(InputStream is, String enc) { if (is == null) { return null; }/*from ww w . j a v a2s . c o m*/ Scanner sc = new Scanner(is, enc == null ? CharEncoding.ISO_8859_1 : enc); StringBuilder sb = new StringBuilder(); String sep = System.getProperty("line.separator"); while (sc.hasNext()) { sb.append(sc.nextLine()); sb.append(sep); } sc.close(); return sb.toString(); }
From source file:de.ifsr.adam.JSONStuff.java
/** * Imports a JSONArray from a JSON file. * * @param filePath The file path to the JSONArray you want to import. * @return//from w w w .j a va2 s. co m */ public final static JSONArray importJSONArray(String filePath) { Path path = Paths.get(filePath); String jsonStr = new String(); JSONArray result = null; try (final Scanner scanner = new Scanner(path, ENCODING.name())) { while (scanner.hasNext()) { jsonStr += scanner.nextLine(); } result = new JSONArray(jsonStr); } catch (IOException e) { JSONStuff.log.error("Failed to find JSON File at: " + filePath, e); } catch (JSONException e) { JSONStuff.log.error("Not a valid JSON file at:" + filePath, e); } return result; }
From source file:org.energyos.espi.thirdparty.mocks.MockRestTemplate.java
@SuppressWarnings("unchecked") public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) throws RestClientException { ClassPathResource sourceFile = new ClassPathResource("/fixtures/test_usage_data.xml"); String inputStreamString;/*from ww w . ja v a 2s.c om*/ try { inputStreamString = new Scanner(sourceFile.getInputStream(), "UTF-8").useDelimiter("\\A").next(); return (T) inputStreamString; } catch (IOException e) { e.printStackTrace(); throw new RestClientException("The file import broke."); } }
From source file:cn.newgxu.android.bbs.util.NewgxuUtils.java
public static final String get(String url, String charset) { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); get.setHeader("Accept", "application/json"); HttpResponse response = null;//from w w w . j a v a 2 s. co m HttpEntity entity = null; Scanner scanner = null; try { response = client.execute(get); entity = response.getEntity(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream in = entity.getContent(); scanner = new Scanner(in, charset == null ? "utf-8" : charset).useDelimiter("\\A"); return scanner.hasNext() ? scanner.next() : null; } } catch (ClientProtocolException e) { Log.wtf(TAG, e); } catch (IOException e) { Log.wtf(TAG, e); } finally { try { if (entity != null) { entity.consumeContent(); } } catch (IOException e) { Log.wtf(TAG, e); } } return null; }
From source file:by.stub.utils.StringUtils.java
public static String inputStreamToString(final InputStream inputStream) { if (inputStream == null) { return "Could not convert empty or null input stream to string"; }/* w ww .ja v a 2s .c o m*/ // Regex \A matches the beginning of input. This effectively tells Scanner to tokenize // the entire stream, from beginning to (illogical) next beginning. return new Scanner(inputStream, StringUtils.UTF_8).useDelimiter("\\A").next().trim(); }
From source file:com.skubit.bitid.loaders.SignInAsyncTaskLoader.java
public static String asString(InputStream inputStream) throws IOException { try {/* w ww .j av a 2s . co m*/ return new Scanner(inputStream, "UTF-8").useDelimiter("\\A").next(); } finally { inputStream.close(); } }
From source file:hu.petabyte.redflags.engine.gear.indicator.hu.ContrDescCartellingIndicator.java
@Override public void beforeSession() throws Exception { keywords.clear();/* ww w .ja v a 2s . c om*/ Scanner s = new Scanner(new ClassPathResource(filename).getInputStream(), "UTF-8"); while (s.hasNextLine()) { keywords.add(s.nextLine()); } s.close(); LOG.debug("Loaded {} expressions for cartelling", keywords.size()); setWeight(0.5); super.beforeSession(); }
From source file:com.adyen.httpclient.HttpURLConnectionClient.java
private static String getResponseBody(InputStream responseStream) throws IOException { //\A is the beginning of the stream boundary Scanner scanner = new Scanner(responseStream, CHARSET); String rBody = scanner.useDelimiter("\\A").next(); scanner.close();/* w w w. ja va 2 s.c o m*/ responseStream.close(); return rBody; }
From source file:com.frankdye.marvelgraphws2.MarvelGraphView.java
/** * Creates a new instance of SimpleGraphView *///from w ww. j av a2 s . c o m public MarvelGraphView() { try { Path fFilePath; /** * Assumes UTF-8 encoding. JDK 7+. */ String aFileName = "C:\\Users\\Frank Dye\\Desktop\\Programming Projects\\MarvelGraph\\data\\marvel_labeled_edges.tsv"; fFilePath = Paths.get(aFileName); // New code // Read words from file and put into a simulated multimap Map<String, List<String>> map1 = new HashMap<String, List<String>>(); Set<String> set1 = new HashSet<String>(); Scanner scan1 = null; try { scan1 = new Scanner(fFilePath, ENCODING.name()); while (scan1.hasNext()) { processLine(scan1.nextLine(), map1, set1); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Creating an URL object. JSONObject newObj = new JSONObject(); newObj.put("Heroes", set1); //Write out our set to a file PrintWriter fileWriter = null; try { fileWriter = new PrintWriter("heroes-json.txt", "UTF-8"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } fileWriter.println(newObj); fileWriter.close(); System.out.println(newObj); // Close our scanner scan1.close(); log("Done."); // Graph<V, E> where V is the type of the vertices // and E is the type of the edges g = new SparseMultigraph<String, String>(); // Add some vertices. From above we defined these to be type Integer. for (String s : set1) { g.addVertex(s); } for (Entry<String, List<String>> entry : map1.entrySet()) { String issue = entry.getKey(); ListIterator<String> heroes = entry.getValue().listIterator(); String firstHero = heroes.next(); while (heroes.hasNext()) { String secondHero = heroes.next(); if (!g.containsEdge(issue)) { g.addEdge(issue, firstHero, secondHero); } } } // Let's see what we have. Note the nice output from the // SparseMultigraph<V,E> toString() method // System.out.println("The graph g = " + g.toString()); // Note that we can use the same nodes and edges in two different // graphs. DijkstraShortestPath alg = new DijkstraShortestPath(g, true); Map<String, Number> distMap = new HashMap<String, Number>(); String n1 = "VOLSTAGG"; String n4 = "ROM, SPACEKNIGHT"; List<String> l = alg.getPath(n1, n4); distMap = alg.getDistanceMap(n1); System.out.println("The shortest unweighted path from " + n1 + " to " + n4 + " is:"); System.out.println(l.toString()); System.out.println("\nDistance Map: " + distMap.get(n4)); } catch (JSONException ex) { Logger.getLogger(MarvelGraphView.class.getName()).log(Level.SEVERE, null, ex); } }