Open urls in BlueJ - java

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();
}
}

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());
}
}

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

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

How to read local php file from java gui program (not from web)?

this is my php code checker code for java application but i want to scan a new file in my java GUI program...
public class test {
public static void main(String[] args) throws MalformedURLException, IOException {
URL u = new URL("http://www.example.com/my/php/doc.php");
URLConnection c = u.openConnection();
InputStream r = c.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(r));
for (String line;
(
line = reader.
`enter code here`
readLine()) != null;
)
System.out.println(line);
This code opens file from the web but I want to open file from the desktop
You can use classes available in the java.io package to read from files. For e.g like this:
public class TextFileReadingExample {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("C:\\Users\\MyUsername\\Desktop\\MyFile.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Try to use: new File(your_path).toURI().toURL();
i.e.
public class Main {
public static void main(String[] args) throws MalformedURLException, IOException {
URL u = new File("E:\\test_data_014.lst").toURI().toURL();
URLConnection c = u.openConnection();
InputStream r = c.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(r));
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
reader.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.

how to get url html contents to string in java

I have a html file stored on the server. I have the URL path something like this: <https://localhost:9443/genesis/Receipt/Receipt.html >
I want to read the contents of this html file which would contain tags, from the url i.e. the source code of the html file.
How am I supposed to do this? This is a server side code and can't have a browser object and I am not sure using a URLConnection would be a good option.
What should be the best solution now?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLContent {
public static void main(String[] args) {
try {
// get URL content
String a = "http://localhost:8080//TestWeb/index.jsp";
URL url = new URL(a);
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Resolved it using spring
added the bean to the spring config file
<bean id = "receiptTemplate" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="/WEB-INF/Receipt/Receipt.html"></constructor-arg>
</bean>
then read it in my method
// read the file into a resource
ClassPathResource fileResource =
(ClassPathResource)context.getApplicationContext().getBean("receiptTemplate");
BufferedReader br = new BufferedReader(new FileReader(fileResource.getFile()));
String line;
StringBuffer sb =
new StringBuffer();
// read contents line by line and store in the string
while ((line =
br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
import java.net.*;
import java.io.*;
//...
URL url = new URL("https://localhost:9443/genesis/Receipt/Receipt.html");
url.openConnection();
InputStream reader = url.openStream();
For exemple :
URL url = new URL("https://localhost:9443/genesis/Receipt/Receipt.html");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String l;
while ((l=in.readLine())!=null) {
System.out.println(l);
}
You could use the inputstream in other ways, not just printing it.
Of course if you have the path to the local file, you can also do
InputStream in = new FileInputStream(new File(yourPath));
Simplest way in my opinion is to use IOUtils
import com.amazonaws.util.IOUtils;
...
String uri = "https://localhost:9443/genesis/Receipt/Receipt.html";
String fileContents = IOUtils.toString(new URL(uri).openStream());
System.out.println(fileContents);

Categories