Is there a FileReader equivalent for a URL in Java? [duplicate] - java

This question already has answers here:
Read url to string in few lines of java code
(11 answers)
Closed 5 years ago.
Consider the following.
val br = new BufferedReader(new FileReader(inputFileName))
But instead of a file, I want to read a file directly from the web, that is, from ftp or http. So, what would be the equivalent for reading from a URL?

It's URLConnection.
Here's an example from Java documentation : Reading from and Writing to a URLConnection :
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

you can use java.io.InputStream
import java.net.;
import java.io.;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
for more info see Reading Directly from a URL

Related

cannot print webpage contents to a file in my local system using java

I cannot print contents of a webpage to a file in my local system.please help me to solve this problem
import java.net.*;
import java.io.*;
public class bpart
{
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
OutputStream os = new FileOutputStream("my file location");
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
I would suggest using a try-with-resources structure for you I/O and take a look a java-naming conventions. If you want to write the output of your http-call to a file you can do the following:
public static void main(String[] args) throws Exception {
URL googleUrl = new URL("http://www.google.com/");
try (BufferedReader in = new BufferedReader(new
InputStreamReader(googleUrl.openStream()));
PrintWriter out = new PrintWriter(new FileWriter("path/to/desired/file"))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
out.println(inputLine);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

Java reading URL from anapioficeandfire returns 403

I want to get Json from https://anapioficeandfire.com/api/characters/583 with native Java
Here is the code:
import java.net.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws Exception {
URL oracle = new URL("https://anapioficeandfire.com/api/characters/583");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
What I get is this error:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://anapioficeandfire.com/api/characters/583
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1045)
at sprint2.fireandice.Main.main(Main.java:17)
This code works with example.com, google.com etc...
The problem is with openStream(). The server rejects this Connection and sends 403 Forbidden. You can "fool" the server and act like a normal browser by setting a user-agent.
public static void main(String[] args) throws Exception{
URL oracle = new URL("https://anapioficeandfire.com/api/characters/583");
HttpURLConnection httpcon = (HttpURLConnection) oracle.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}

How to get the contents of API using Java?

I want to get the values of a weather api from
http://www.dataweave.in/apis/usage/13/Indian-Weather-Data
Like in PHP, the syntax to get the values from the api is:
$String=file_get_contents("url of the api");
But i would like to do the same in java. How can i get the file contents from the api in Java?
You can use something like below:
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://api.dataweave.in/v1/indian_weather/findByCity/?api_key=b20a79e582ee4953ceccf41ac28aa08d&city=Agartala&date=20120501");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
inputLine has your data.

Exception in thread "main" java.net.ConnectException: Connection timed out: connect

Just I am trying to read the HTML file using URL. But getting the timed out exception.
source code:
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
System.setProperty("java.net.useSystemProxies","true");
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
This code works fine for me, please check your internet connection..

Open urls in BlueJ

is it possible to open urls in BlueJ.
For example in PHP I can use the function file_get_contents()
Is there something equal in BlueJ?
You need to look at using a buffered reader and using the URL object in Java.
It's basically something like this:
URL url = new URL("http://myurl.com");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String input = "";
while((input = reader.readLine())!=null) {
System.out.println(input);
}
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

Categories