InputStream response = new URL(url).openStream(); // ...
public boolean hasLicensesXml() { try { new URL(baseResourceURL,"WEB-INF/licenses.xml").openStream().close(); return true; } catch (IOException e) { return false; } }
import java.net.*; import java.io.*; URL whatismyip = new URL("http://checkip.amazonaws.com"); BufferedReader in = new BufferedReader(new InputStreamReader( whatismyip.openStream())); String ip = in.readLine(); //you get the IP as a String System.out.println(ip);
public static void main(String[] args) throws Exception { String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; String search = "stackoverflow"; String charset = "UTF-8"; URL url = new URL(google + URLEncoder.encode(search, charset)); Reader reader = new InputStreamReader(url.openStream(), charset); GoogleResults results = new Gson().fromJson(reader, GoogleResults.class); // Show title and URL of 1st result. System.out.println(results.getResponseData().getResults().get(0).getTitle()); System.out.println(results.getResponseData().getResults().get(0).getUrl()); }
@Test @SuppressWarnings("DefaultCharset") public void testCompile() throws Exception { System.out.println("compile"); URL script = CompilerTest.class.getResource("/traces/OnMethodTest.java"); InputStream is = script.openStream(); File f = new File(script.toURI()); File base = f.getParentFile().getParentFile().getParentFile(); String classpath = base.getAbsolutePath() + "/main" + ":" + base.getAbsolutePath() + "/test"; BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = br.readLine()) != null) { sb.append(line).append('\n'); } System.out.println(sb.toString()); Map<String, byte[]> data = instance.compile("traces/OnMethodTest.java", sb.toString(), new PrintWriter(System.err), ".", classpath); Assert.assertNotNull(data); } }
/** * {@code keyStorePath} could be absolute path or path to classpath resource. * * @return File for {@code keyStorePath}. */ private InputStream keyStoreFile() throws IOException { File abs = new File(keyStorePath); if (abs.exists()) return new FileInputStream(abs); URL clsPthRes = KeystoreEncryptionSpi.class.getClassLoader().getResource(keyStorePath); if (clsPthRes != null) return clsPthRes.openStream(); return null; }
InputStream in = new URL( "http://jakarta.apache.org" ).openStream(); try { System.out.println( IOUtils.toString( in ) ); } finally { IOUtils.closeQuietly(in); }
public String call() throws IOException { File f = new File(script); if(f.exists()) return FileUtils.readFileToString(f); URL url; try { url = new URL(script); } catch (MalformedURLException e) { throw new AbortException("Unable to find a script "+script); } try (InputStream s = url.openStream()) { return IOUtils.toString(s); } } }
public <T> T fromYAML(URL url, Class<T> configType) throws IOException { String content = resolveEnvParams(new InputStreamReader(url.openStream())); return yamlMapper.readValue(content, configType); }
protected String getCommandFromManifest() { try { Enumeration<URL> resources = RunCommand.class.getClassLoader().getResources("META-INF/MANIFEST.MF"); while (resources.hasMoreElements()) { InputStream stream = resources.nextElement().openStream(); Manifest manifest = new Manifest(stream); Attributes attributes = manifest.getMainAttributes(); String mainClass = attributes.getValue("Main-Class"); if (main.getClass().getName().equals(mainClass)) { String command = attributes.getValue("Main-Command"); if (command != null) { stream.close(); return command; } } stream.close(); } } catch (IOException e) { throw new IllegalStateException(e.getMessage()); } return null; }
/** * Parses the JSON input file containing the list of features. */ public static List<Feature> parseFeatures(URL file) throws IOException { InputStream input = file.openStream(); try { Reader reader = new InputStreamReader(input); try { FeatureDatabase.Builder database = FeatureDatabase.newBuilder(); JsonFormat.parser().merge(reader, database); return database.getFeatureList(); } finally { reader.close(); } } finally { input.close(); } }
/** * Create a reader from specified {@code source}.<br> * Returned reader should be explicitly closed after use. * * @param uri source URI * @return reader * @throws IOException if the connection cannot be opened */ public static Reader uriReader(URI uri) throws IOException { return new BufferedReader(new InputStreamReader(uri.toURL().openStream(), StandardCharsets.UTF_8)); }
InputStream input = new URL("http://example.com/foo.json").openStream(); Reader reader = new InputStreamReader(input, "UTF-8"); Data data = new Gson().fromJson(reader, Data.class); // ...
URL url = new URL("http://stackoverflow.com"); try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) { for (String line; (line = reader.readLine()) != null;) { System.out.println(line); } }
URL url = new URL ("file://some/path/anImage.png"); InputStream input = url.openStream(); try { //The sdcard directory e.g. '/sdcard' can be used directly, or //more safely abstracted with getExternalStorageDirectory() File storagePath = Environment.getExternalStorageDirectory(); OutputStream output = new FileOutputStream (new File(storagePath,"myImage.png")); try { byte[] buffer = new byte[aReasonableSize]; int bytesRead = 0; while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { output.write(buffer, 0, bytesRead); } } finally { output.close(); } } finally { input.close(); }
private Idl(URL input, Idl parent) throws IOException { this(input.openStream(), "UTF-8"); this.inputDir = "file".equals(input.getProtocol()) ? new File(input.getPath()).getParentFile() : parent.inputDir; this.resourceLoader = parent.resourceLoader; }
String url = "http://stackoverflow.com/questions/3152138"; Document document = new Tidy().parseDOM(new URL(url).openStream(), null); XPath xpath = XPathFactory.newInstance().newXPath(); Node question = (Node) xpath.compile("//*[@id='question']//*[contains(@class,'post-text')]//p[1]").evaluate(document, XPathConstants.NODE); System.out.println("Question: " + question.getFirstChild().getNodeValue()); NodeList answerers = (NodeList) xpath.compile("//*[@id='answers']//*[contains(@class,'user-details')]//a[1]").evaluate(document, XPathConstants.NODESET); for (int i = 0; i < answerers.getLength(); i++) { System.out.println("Answerer: " + answerers.item(i).getFirstChild().getNodeValue()); }
public KaldiTextParser(String path) throws IOException, MalformedURLException { // TODO: rewrite with StreamTokenizer, see ExtendedStreamTokenizer. File modelFile = new File(path, "final.mdl"); InputStream modelStream = new URL(modelFile.getPath()).openStream(); File treeFile = new File(path, "tree"); InputStream treeStream = new URL(treeFile.getPath()).openStream(); InputStream stream = new SequenceInputStream(modelStream, treeStream); scanner = new Scanner(stream); }
private static Manifest loadManifest(String jarUrl) throws Exception { InputStream is = new URL(jarUrl + "!/META-INF/MANIFEST.MF").openStream(); try { return new Manifest(is); } finally { is.close(); } }
@Override public InputStream open(String path) throws IOException { return new URL(path).openStream(); } }