Skip to content
ELECTRONICS ENGINEERINGelectronics-engineering.com
Java

Write a Program That Reads a List of URLs from an ASCII File and Checks Whether They Are Online, Giving an Error Message in Case of Problems

Introduction In the digital age, maintaining the integrity and accessibility of web resources is crucial for any application that relies on external URLs. Whether to validate links in a database, ensure the availability of resources…

https://www.electronics-engineering.com/en/write-a-program-that-reads-a-list-of-urls-from-an-ascii-file-and-checks-whether-they-are-online/

Was this page useful? Rate it:
average 5.0 from 1 rating

Introduction

In the digital age, maintaining the integrity and accessibility of web resources is crucial for any application that relies on external URLs. Whether it is to validate links in a database, ensure the availability of resources or simply test the reachability of various web addresses, checking the status of URLs programmatically is a common task.

The Java program provided below shows how to check the validity and availability of the URLs listed in a file called list.txt. This utility reads each URL from the file, tries to establish a connection and then checks the response to determine whether the URL is accessible. If an error is found, such as a malformed URL or an inaccessible resource, the program flags the URL as an “invalid link”.

Explanation of the Code

The code is structured to perform the following operations:

  1. Reading the File: Reads the URLs line by line from a file called list.txt.
  2. URL Validation: For each URL, checks whether the format is correct.
  3. Connection Test: Tries to establish a connection to the URL.
  4. Response Analysis: Checks the header response to determine whether the URL is valid or results in a “Not Found” error.
  5. Error Handling: Catches and handles exceptions related to malformed URLs and I/O problems, marking problematic URLs accordingly.

Here is the Java code for the URL checking utility:

import java.net.*;
import java.io.*;

class CheckURL {
    static BufferedReader myInput;
    static FileReader myFile;
    
    public static void main(String[] args) {
        try {
            String nomeurl;
            myFile = new FileReader("list.txt");
            myInput = new BufferedReader(myFile);
            
            while (myInput.ready()) {
                nomeurl = myInput.readLine();
                
                try {
                    URL theURL = new URL(nomeurl);
                    System.out.println("Testing " + nomeurl);
                    
                    try {
                        URLConnection con = theURL.openConnection();
                        con.getContent();
                        String temp = con.getHeaderField(0);
                        System.out.println("Header " + temp);
                        int fileNotFound = temp.indexOf("Not Found");
                        
                        if (fileNotFound >= 0) {
                            System.out.println("***********Bad link***************");
                        }
                    } catch (IOException e) {
                        System.out.println("***********Bad link***************");
                    }
                } catch (MalformedURLException e) {
                    System.out.println("Malformed URL Exception " + e);
                }
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
}

Key Points

  • FileReader and BufferedReader: Used to read the URLs from the list.txt file.
  • URL and URLConnection: Main classes for handling and testing URLs in Java.
  • Exception Handling: Ensures that the program keeps working even if errors are encountered with specific URLs.

This program is a useful tool for developers and administrators who need to make sure that the links in their applications or databases are valid and accessible, thus maintaining an uninterrupted user experience and avoiding broken links.

 

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
1
0
Would love your thoughts, please comment.x
()
x