Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URL toURI() Method with Examples



Description

The Java URL toURI() method returns a URI representation of this URL.

Declaration

Following is the declaration for java.net.URL.toURI() method

public URI toURI()

Parameters

NA

Return Value

a URI instance equivalent to this URL.

Exception

URISyntaxException − if this URL is not formatted strictly according to to RFC2396 and cannot be converted to a URI.

Example 1

The following example shows the usage of Java URL toURI() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Now using toURI() method, we're getting the URI representation of the URL object and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;

public class UrlDemo {
   public static void main(String [] args) throws URISyntaxException {
      try {
         URL url = new URL("https","www.tutorialspoint.com","/index.htm");
         URI result = url.toURI();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

https://www.tutorialspoint.com/index.htm

Example 2

The following example shows the usage of Java URL toURI() method for a valid url with http protocol. In this example, we're creating an instance of URL class with a port number as 80. Now using getURI() method, we're getting the URI representation of the URL object and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URI;

import java.net.URISyntaxException;

public class UrlDemo {
   public static void main(String [] args) throws URISyntaxException {
      try {
         URL url = new URL("http","www.tutorialspoint.com",80,"/index.htm");
         URI result = url.toURI();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

http://www.tutorialspoint.com:80/index.htm

Example 3

The following example shows the usage of Java URL toURI() method for a valid url with file protocol. In this example, we're creating an instance of URL class. Now using toURI() method, we're getting the URI representation of the URL object and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URI;

import java.net.URISyntaxException;

public class UrlDemo {
   public static void main(String [] args) throws URISyntaxException {
      try {
         URL url = new URL("file","www.tutorialspoint.com","/index.htm");
         URI result = url.toURI();
         System.out.println(result);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

file://www.tutorialspoint.com/index.htm
java_url.htm
Advertisements