STIW3054-A182 / Main-Issues

5 stars 1 forks source link

Check Url #9

Open zhamri opened 5 years ago

zhamri commented 5 years ago

Instruction:

Write a Java program to:

  1. Read a list of URLs from a file (eg url.txt).
  2. If the URL exists then print "Yes".
  3. If the URL does not exist then print "Not Exist".
  4. The number of threads will depend on the number of URLs in the file.

Submission:

  1. Sample of the text file.
  2. Java code.
  3. Screenshot of the output.
muhamaddarulhadi commented 5 years ago

TESTING

1. Sample of the text file.

sample of 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.

result URL

shyyahcheang commented 5 years ago

1. Sample of the text file.

image

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.

issue8 - output

SINHUI commented 5 years ago

1. Sample of the text file.

notePad

2. JAVA Code

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

3. Screenshot of the output.

result(redo issue9)

mimiothman commented 5 years ago
  1. Sample of the text file. image

  2. 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)
ChongMeiYong commented 5 years ago
  1. Sample of the text file image

  2. 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)
AhKitt commented 5 years ago

1. Sample of the text file :

sample text

2. Java code :

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

3. Screenshot of the output :

issue9

roflinasuha commented 5 years ago
  1. Sample of Text image

  2. 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)
Yvevonwen commented 5 years ago
  1. Sample of Text image
  2. JavaCode
    
    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)
mwng97 commented 5 years ago

Text File:

TextFile

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: output

lancelot9927 commented 5 years ago

1.

txt

2.

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

3.

output

limxinyii commented 5 years ago

1. sample of text file

image

2. java code

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

    }
}

3. screenshot of output

image

ChanJunLiang commented 5 years ago
  1. Text i9 txt

  2. 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. output i9 op
HENGZHIYONG commented 5 years ago

1 . Sample of the text file.

image

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();
        }
    }
}
  1. Screenshot of the output.

image

muhamaddarulhadi commented 5 years ago

TESTING AGAIN

TESTING

1. Sample of the text file.

sample of 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.

result URL

1. Sample text file Capture

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 Capture1

prevenkumar commented 5 years ago

TEXT FILE

i9text

CODE

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

OUTPUT

outputi9

lingzhongli commented 5 years ago
  1. Text File image

  2. 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)
tanyujia commented 5 years ago

1. Sample of the text file Snipaste_2019-03-28_02-32-29

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 Snipaste_2019-03-28_02-31-42

WongKinSin13 commented 5 years ago

Text File Content

image Download: url.txt

Source Code(No Threads)

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

Sample Output

image

lishaobin commented 5 years ago

1.Sample of the text file:

image

Java code:

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

}

3.Screenshot of the output

image

trinanda98 commented 5 years ago

Code

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

    }

}

Sample Url

https://stackoverflow.com https://stackoverfl99ow.com https://imgur.com https://imgueer.com https://www.google.com/ https://learning78.uum.edu.my

Output

Screenshot (66)
TanShiJet commented 5 years ago

txt

image

Code

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

  }
}

output

image

raihanwidia commented 5 years ago

image

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

}

image

rahimahsahar commented 5 years ago

issues9 notepad

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

issues9

alfmrza commented 5 years ago

image

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

image

ShahifahYahya commented 5 years ago
  1. Sample of the text image

  2. 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)