Open zhamri opened 5 years ago
1. Sample of the text file.
2. Java code.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
public class check
{
public static boolean isValid(String url)
{
try
{
new URL(url).toURI();
return true;
}
catch (Exception e)
{
return false;
}
}
public static void main(String[] args)
{
try
{
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\robber_hadi\\Desktop\\URL.txt"));
String line;
while((line = in.readLine()) != null)
{
if (isValid(line))
{
System.out.println("Yes");
}
else
{
System.out.println("Not Exist");
}
}
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
3. Screenshot of the output.
1. Sample of the text file.
2. Java code.
package checkURL;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class CheckUrl extends Thread {
public static void main (String[] args) {
new Thread(new CheckUrl()).start();
}
@Override
public void run() {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("C:\\Users\\user\\Desktop\\checkUrl.txt"));
String line = reader.readLine();
int a = 1;
while(line != null) {
if (urlValidator(line)) {
System.out.println("(Thread " + a + ") " + line + " --> Yes");
}
else {
System.out.println("(Thread " + a + ") " + line + " --> Not Exist");
}
line = reader.readLine();
a++;
}
reader.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public static boolean urlValidator(String url) {
try {
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection check= (HttpURLConnection) new URL(url).openConnection();
check.setConnectTimeout(1000);
check.setReadTimeout(1000);
check.setRequestMethod("GET");
return (check.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
3. Screenshot of the output.
import java.net.*;
import java.io.IOException;
public class Thread1 implements Runnable{
String link;
public Thread1(String link){
this.link=link;
}
public void run(){
try {
if(valid(link)){
System.out.println(link + " (Yes)");
}
else{
System.out.println(link + " (Not Exist)");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean valid(String url) throws MalformedURLException, IOException {
try {
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection checkURL= (HttpURLConnection) new URL(url).openConnection();
checkURL.setRequestMethod("GET");
checkURL.connect();
return (checkURL.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
import java.io.*;
public class Issue9{
public static void main(String[]args) throws IOException{
File file = new File("C:\\Users\\Asus\\Documents\\sem 4\\REAL TIME\\issue9.txt");
BufferedReader scan = new BufferedReader(new FileReader(file));
String read;
int i=1;
while ((read= scan.readLine()) != null) {
Thread thread = new Thread(new Thread1(read), "Thread"+i);
thread.start();
i++;
}
scan.close();
}
}
Sample of the text file.
Java code.
package test1;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
public class issue8 { public static boolean validWord(String test_url) { try { new URL(test_url).toURI(); return true; } catch (Exception e) { return false; } }
public static void main(String[] args)
{
try
{
BufferedReader link = new BufferedReader(new FileReader("C:\\Users\\user\\Desktop\\isu8.txt"));
String text;
while((text = link.readLine()) != null)
{
if (validWord(text))
{
System.out.println("Yes");
}
else
{
System.out.println("Not Exist");
}
}
link.close();
}
catch (IOException e)
{
e.printStackTrace();
System.out.println("Please try again");
}
}
}
3. Screenshot of the output.
![image](https://user-images.githubusercontent.com/37438543/54975583-7b304c00-4fd2-11e9-98b0-21b903f741d0.png)
Sample of the text file
Java Code
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
public class Issue9CheckUrl {
public static boolean isValid(String url)
{
try
{
new URL(url).toURI();
return true;
}catch (Exception e) {
return false;
}
}
public static void main(String[] args)
{
try
{
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\asus\\Desktop\\Sem 4\\Real-Time Programming\\Assignment\\Issue9\\text.txt"));
String text;
while((text = in.readLine()) != null)
{
if (isValid(text)){
System.out.println("Yes");
}
else{
System.out.println("Not Exist");
}
}
in.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
3. Screenshot of the output
![image](https://user-images.githubusercontent.com/47626352/54978264-c8182080-4fda-11e9-83f8-3cc986d6eefc.png)
import java.io.*;
import java.net.*;
public class Issue9{
public static void main(String[]args) throws IOException{
File file = new File("C:\\Users\\Asus\\Desktop\\issue9.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
int i=1;
while ((st = br.readLine()) != null) {
Thread thread = new Thread(new MyThread(st), "Thread"+i);
thread.start();
i++;
}
br.close();
}
}
import java.net.*;
public class MyThread implements Runnable{
String web;
public MyThread(String web){
this.web=web;
}
public void run(){
if(exists(web)){
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Yes)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Not Exist)");
}
}
public static boolean exists(String URLName) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
Sample of Text
Java code
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
public class issues9 {
public static void main(String[] args)
{
try
{
BufferedReader URLlink = new BufferedReader(new FileReader("C:\\Users\\BCC\\Desktop\\REAL TIME PROGRAMMING\\issues9.txt"));
String text;
int a=1;
while ((text = URLlink.readLine()) != null) {
Thread thread = new Thread(new thread(text), "Thread "+a);
thread.start();
a++;
}
URLlink.close();
}
catch (IOException e)
{
e.printStackTrace();
System.out.println("Please try again");
}
}
}
import java.net.HttpURLConnection; import java.net.URL;
public class thread implements Runnable {
String web;
public thread(String web){
this.web=web;
}
public void run(){
if(exists(web)){
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Yes)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Not Exist)");
}
}
public static boolean exists(String URLName) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
3. Output
![image](https://user-images.githubusercontent.com/37438580/55000387-5dcca380-500d-11e9-8944-2fa8eced556f.png)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
public class Issues9 {
public static void main(String[] args)
{
try {
BufferedReader read = new BufferedReader(new FileReader("C:\Users\Lenovo\OneDrive\Desktop\issue9\Issues_9.txt"));
String text;
while ((text = read.readLine()) !=null ){
if (Valid(text)) {
System.out.println("Yes!");}
else{
System.out.println("No Exist!");
}
}
read.close();
}catch (IOException e) {
e.printStackTrace();
}
}
public static boolean Valid(String url) {
try {
new URL(url).toURI();
return true;
}catch(Exception e) {
return false;
}
}
}
3.Output
![image](https://user-images.githubusercontent.com/43850170/55001599-e9dfca80-500f-11e9-93cf-6f3b49ab5443.png)
Text File:
Java Code:
import java.io.*;
public class ReadFile{
public static void main(String[]args){
BufferedReader read;
int i=0;
String line;
File file = new File("C:\\Users\\USER\\Desktop\\Sem 4\\realtime\\Issue\\Issue 9\\URL.txt");
CheckUrl checkUrl = new CheckUrl();
try {
read = new BufferedReader(new FileReader(file));
while ((line = read.readLine()) != null) {
if (checkUrl.checkURL(line)) {
System.out.println((i + 1) + ".) " + line + "\t -> Exists ");
i++;
} else {
System.out.println((i + 1) + ".) " + line + "\t -> Not exists ");
i++;
}
}
read.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
public class CheckUrl{
public boolean checkURL(String URL) {
try {
new URL(URL).toURI();
return true;
} catch (URISyntaxException e) {
return false;
} catch (MalformedURLException e) {
return false;
}
}
}
Output:
package issue9;
import java.net.*;
import java.io.*;
public class issue9{
public static void main(String[] args) throws Exception {
try {
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\issue9.txt"));
String line;
while ((line = reader.readLine()) != null) {
if(check(line)){
System.out.println(line+" (Yes)");
}else {
System.out.println(line+" (Not Exist)");
}
}
reader.close();
}catch (IOException e) {
e.printStackTrace();
}
}
public static boolean check(String url) {
try {
new URL(url).toURI();
return true;
}
catch (Exception e) {
return false;
}
}
}
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class CheckURL implements Runnable
{
public static void main (String [] args) {
new Thread(new CheckURL()).start();
}
@Override
public void run() {
try
{
File inFile = new File("url.txt");
Scanner scanner = new Scanner (inFile);
int i =1;
while (scanner.hasNextLine()) {
String url = scanner.nextLine();
if (checkIfURLExists(url)) {
System.out.println("Thread "+ i + " : Yes");
}
else {
System.out.println("Thread " + i + " : Not Exist");
}
i++;
}
scanner.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
public static boolean checkIfURLExists (String urlString )
{
HttpURLConnection httpUrlConn;
try {
HttpURLConnection.setFollowRedirects(true);
httpUrlConn = (HttpURLConnection) new URL (urlString).openConnection();
httpUrlConn.setRequestMethod("HEAD");
httpUrlConn.setConnectTimeout(1000);
httpUrlConn.setReadTimeout(1000);
return (httpUrlConn.getResponseCode()== HttpURLConnection.HTTP_OK);
}catch (Exception e) {
return false;
}
}
}
Text
Code
import java.io.*;
import java.net.URL;
public class Issue9{
public static void main(String[]args) throws IOException{
File file = new File("C:\\Users\\Liang\\Desktop\\realtime\\issue9.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
int i=1;
while ((st = br.readLine()) != null) {
Thread checkurl = new Thread(new CheckURL(st), "Thread"+i);
checkurl.start();
i++;
}
br.close();
}
}
```java
import java.net.*;
public class CheckURL implements Runnable{
String url;
public CheckURL(String url){
this.url = url;
}
public void run(){
if(exists(url)){
System.out.println(Thread.currentThread().getName()+ " - " + url + " (Yes)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " + url + " (Not Exist)");
}
}
public static boolean exists(String URLName) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
1 . Sample of the text file.
2.Java code.
package week6;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.MalformedURLException;
import java.net.URL;
public class chceckURL {
public static boolean checkURL(String url)
{
try {
new URL(url).toURI();
return true;
}
catch (URISyntaxException exception) {
return false;
}
catch (MalformedURLException exception) {
return false;
}
}
public static void main(String[] args) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
"/Users/HP/Desktop/url.txt"));
String line;
while ((line = reader.readLine()) != null) {
if (checkURL(line)) {
System.out.println(line + " ---> Yes");
}
else {
System.out.println(line + " ---> No Exist!");
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TESTING
1. Sample of the text file.
2. Java code.
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.net.URL; public class check { public static boolean isValid(String url) { try { new URL(url).toURI(); return true; } catch (Exception e) { return false; } } public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\robber_hadi\\Desktop\\URL.txt")); String line; while((line = in.readLine()) != null) { if (isValid(line)) { System.out.println("Yes"); } else { System.out.println("Not Exist"); } } in.close(); } catch (IOException e) { e.printStackTrace(); } } }
3. Screenshot of the output.
1. Sample text file
2. Java code
package javaapplication22;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
public class JavaApplication22
{
public static boolean isValid(String url)
{
try
{
new URL(url).toURI();
return true;
}
catch (Exception e)
{
return false;
}
}
public static void main(String[] args)
{
try
{
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\master lab\\Desktop\\url.txt"));
String line;
while((line = in.readLine()) != null)
{
try
{
if (isValid(line))
{
//check whether the url can connect to the internet
URL myURL = new URL(line);
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.getContentLength();
System.out.println("Yes");
//check the url by open the browser
/*Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI(line);
desktop.browse(oURL);
System.out.println("Yes");*/
}
else
{
System.out.println("Not Exist");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
3. Output
package issue9;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Issue_9 {
public static void main(String[]args){
BufferedReader r;
String line;
File file = new File("D:\\UUM\\SEM 4\\Real-time\\Issue9.txt");
Url u = new Url();
try {
r = new BufferedReader(new FileReader(file));
while ((line = r.readLine()) != null) {
if (u.c_URL(line)) {
System.out.println("Yes");
} else {
System.out.println("Not Exist");
}
}
r.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package issue9;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
public class Url {
public boolean c_URL(String URL) {
try {
new URL(URL).toURI();
return true;
} catch (URISyntaxException e) {
return false;
} catch (MalformedURLException e) {
return false;
}
}
}
Text File
Code
import java.net.URL;
import java.io.*;
class Test {
/* Returns true if url is valid */
public static boolean isValid(String url)
{
try {
new URL(url).toURI();
return true;
}
catch (Exception e) {
return false;
}
}
/*driver function*/
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\user\\Downloads\\Real Time\\link.txt"));
String url;
while ((url = reader.readLine()) != null){
if (isValid(url))
System.out.println("Yes");
else
System.out.println("No Exist");
}
}
}
3. Output
![image](https://user-images.githubusercontent.com/37300365/55084744-6a6dfc00-50e0-11e9-9f96-1507a90dba8b.png)
1. Sample of the text file
2. Java code
import java.io.*;
public class issue9 {
public static void main(String[] args) {
try {
BufferedReader read = new BufferedReader(new FileReader("C:\\Users\\Tyjia\\Desktop\\url.txt"));
String line;
while ((line = read.readLine()) != null) {
Thread t = new Thread(new uRL(line));
t.start();
}
read.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.net.*;
public class uRL extends Thread {
private String link;
public uRL(String l) {
link = l;
}
@Override
public void run() {
if (checkURL(link)) {
System.out.println(link + ": Yes");
} else {
System.out.println(link + ": Not Exist");
}
}
public boolean checkURL(String url) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
connection.setRequestMethod("HEAD");
return (connection.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
3. Screenshot of the output
Download: url.txt
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;
public class URL_check {
public static void main (String []args) {
String fileName = "url.txt";
String line = "null";
int lineCount = 0;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
lineCount ++ ;
try {
new URL(line).openStream().close();
System.out.println("Yes");
}catch(MalformedURLException ex) {
System.out.println("No");
System.out.println(ex);
}catch(IOException ex) {
System.out.println("No");
System.out.println(ex);
}
}
System.out.println("\nTotal line in text: "+ lineCount);
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
package issuse9;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
public class issuse9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader b;
try {
b = new BufferedReader(new FileReader("D:\\课件\\mp\\eg url.txt"));
String url;
while((url = b.readLine()) != null)
{
if (isValid(url))
{
System.out.println(url+"Yes");
}
else
{
System.out.println(url+"Not Exist");
}
}
b.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static boolean isValid(String url) {
// TODO Auto-generated method stub
try
{
new URL(url).toURI();
return true;
}
catch (Exception e)
{
return false;
}
}
}
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.rmi.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class URLReader implements Runnable{
public static void main(String[] args) throws UnknownHostException {
long startTime = System.nanoTime();
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("/Users/trina/Desktop/URL.txt"));
String text = reader.readLine();
int a = 0;
while(text!=null) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(text).openConnection();
con.setRequestMethod("HEAD");
int responseCode = con.getResponseCode();
if(responseCode == 200) {
System.out.println("Yes" );
text = reader.readLine();
a++;
Thread t = new Thread(new URLReader());
t.start();
}
}catch(Exception e) {
System.out.println("Not Exist");
text = reader.readLine();
continue;
}
}
reader.close();
}catch(IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Thread Name: " + Thread.currentThread().getName());
}
}
https://stackoverflow.com https://stackoverfl99ow.com https://imgur.com https://imgueer.com https://www.google.com/ https://learning78.uum.edu.my
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.net.*;
public class lol {
public static void main(String[] args) {
Path path = Paths.get("C:\\Users\\User\\Desktop\\lol.txt");
try {
List<String> lines = Files.readAllLines(path);
for (String line : lines) {
try {
HttpURLConnection check= (HttpURLConnection) new URL(line).openConnection();
check.getResponseCode();
System.out.println(line +"\t true");
} catch (IOException e) {
System.out.println(line +"\t false");
}
}
} catch (IOException e) {
System.out.println(e);
}
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.stream.Collectors;
public class Mainclass {
static String [] urls ;
public static void main (String[]args) throws IOException, InterruptedException {
BufferedReader reader = new BufferedReader(new FileReader("E:\\Project GITHUB\\Issues9\\testing.txt"));
String text =reader.lines().collect(Collectors.joining("\n"));
urls = text.split("\n");
anotherclass [] t = new anotherclass[urls.length];
for (int i = 0; i < urls.length; i++) {
t[i] = new anotherclass(urls[i]);
t[i].start();
t[i].join();
}
}
}
import java.net.HttpURLConnection;
import java.net.URL;
public class anotherclass extends Thread{
String URL;
public anotherclass(String w ) {
this.URL = w;
}
@Override
public void run() {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URL).openConnection();
con.setRequestMethod("HEAD");
int responseCode = con.getResponseCode();
if(responseCode>0)
System.out.println(Thread.currentThread().getName()+" is Exist");
}catch(Exception e) {
System.out.println(Thread.currentThread().getName()+" Not Exist");
}
}
}
Thread 1
import java.net.HttpURLConnection;
import java.net.URL;
public class Thread1 implements Runnable {
String web;
public Thread1 (String web){
this.web=web;
}
public void run(){
if(exists(web)){
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Yes)");
}
else{
System.out.println(Thread.currentThread().getName()+ " - " +web + " (Not Exist)");
}
}
public static boolean exists(String URLName) {
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
Issues 9
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
public class Issues9
{
public static void main(String[] args)
{
try
{
BufferedReader URLlink = new BufferedReader(new FileReader("C:\\Users\\Rahimah\\Documents\\Issue9\\issues9.txt"));
String text;
int a=1;
while ((text = URLlink.readLine()) != null) {
Thread thread = new Thread(new Thread1(text), "Thread "+a);
thread.start();
a++;
}
URLlink.close();
}
catch (IOException e)
{
e.printStackTrace();
System.out.println("Please try again");
}
}
}
package COM.STIW3054.Test;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.FileReader;
public class issue4 extends Thread {
public static void main (String[] args) {
new Thread(new issue4()).start();
}
@Override
public void run() {
BufferedReader read;
try {
read = new BufferedReader(new FileReader("C:\\Users\\user\\Desktop\\checkUrl.txt"));
String url = read.readLine();
int a = 1;
while(url != null) {
if (urlValidator(url)) {
System.out.println("(Thread " + a + ") " + url + " Yes");
}
else {
System.out.println("(Thread " + a + ") " + url + " Not Exist");
}
url = read.readLine();
a++;
}
read.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public static boolean urlValidator(String url) {
try {
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection check= (HttpURLConnection) new URL(url).openConnection();
check.setConnectTimeout(500);
check.setReadTimeout(500);
check.setRequestMethod("GET");
return (check.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
Sample of the text
Java Code
import java.net.*;
import java.io.IOException;
public class Issue9 implements Runnable{
String link;
public Issue9(String link){
this.link=link;
}
public void run(){
try {
if(valid(link)){
System.out.println(link + " (Yes)");
}
else{
System.out.println(link + " (Not Exist)");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean valid(String url) throws MalformedURLException, IOException {
try {
HttpURLConnection.setFollowRedirects(true);
HttpURLConnection checkURL= (HttpURLConnection) new URL(url).openConnection();
checkURL.setRequestMethod("GET");
checkURL.connect();
return (checkURL.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
return false;
}
}
}
import java.io.*;
public class TestIssue9 {
public static void main(String[]args) throws IOException{
File file = new File("C:\\Users\\ASUS\\Desktop\\Issue9.txt");
BufferedReader scan = new BufferedReader(new FileReader(file));
String read;
int i=1;
while ((read= scan.readLine()) != null) {
Thread thread = new Thread(new Issue9(read), "Thread"+i);
thread.start();
i++;
}
scan.close();
}
}
3.Output
![image](https://user-images.githubusercontent.com/47625835/58371174-ad621980-7f41-11e9-8a89-8668b785145f.png)
Instruction:
Write a Java program to:
Submission: