0

Java has cool URL parser

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

public class ParseURL {
public static void main(String[] args) throws Exception {

    URL aURL = new URL("http://example.com:80/docs/books/tutorial"
                       + "/index.html?name=networking#DOWNLOADING");

    System.out.println("path = " + aURL.getPath());
}
}

Here is the output displayed by the program:

path = /docs/books/tutorial/index.html

I would like to take only this part : docs/books/tutorial (or /docs/books/tutorial/) Guessing don`t use string split, I am looking for other better resolution for this task.

Thank you in advance

1
  • You can always write a custom method to get the substring of path. Commented Jun 17, 2016 at 10:36

4 Answers 4

2

There are few ways. One of them is using URI#resolve(".") where . represents current directory. So your code can look like:

URI uri = new URI("http://example.com:80/docs/books/tutorial"
        + "/index.html?name=networking#DOWNLOADING");
System.out.println(uri.resolve(".").getPath());

Output: /docs/books/tutorial/


Other way could involve file system and classes which handle it like File or its improved version introduced in Java 7 Path (and its utility class Paths).
These classes should allow you to parse path

/docs/books/tutorial/index.html

and get its parent location /docs/books/tutorial.

URL aURL = new URL("http://example.com:80/docs/books/tutorial"
        + "/index.html?name=networking#DOWNLOADING");

String path = aURL.getPath();
String parent = Paths.get(path).getParent().toString();

System.out.println(parent);// \docs\books\tutorial

(little warning: depending on your OS you may get path separated with \ instead of /)

Sign up to request clarification or add additional context in comments.

Comments

2
String path = "/docs/books/tutorial/index.html";
path = path.substring(1, path.lastIndexOf("/"));

gives docs/books/tutorial

Comments

2

You can do it with a File Object, rather than split your String:

Working example:

import java.io.File;
import java.net.URL;

public class ParseURL {
    public static void main(String[] args) throws Exception {

        URL aURL = new URL("http://example.com:80/docs/books/tutorial"
                           + "/index.html?name=networking#DOWNLOADING");

        System.out.println("path = " + aURL.getPath());

        File file = new File(aURL.getPath());

        System.out.println("pathOnly = " + file.getParent());
    }
}

Output:

path = /docs/books/tutorial/index.html
pathOnly = /docs/books/tutorial

Comments

1

Take this as a example:

public static String getCustomPath() {
    String path = "/docs/books/tutorial/index.html";
    String customPath = path.substring(0, path.indexOf("index.html"));
    return customPath;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.