-1

I want to create URL using : existing url and 2 strings

I found

Create URL from a String
and
File.separator or File.pathSeparator

but I want to create it without typing '/'

String part1 = "txt1";

String part2 = "txt2";

URL urlBase = new URL(some adress);

now I can write

URL urlNew = urlBase.toURI().resolve(part1).toURL();

but how to add part2 goal is to get adress/txt1/txt2

6
  • Your question is difficult to read. Is what you want: URL urlNew = urlBase.toURI().resolve(part1 + "/" + part2).toURL();? Commented Jul 14, 2014 at 18:46
  • Hi, thanks for reply, solution is correct but I want to achieve same result without usage of '/'. Do you know how to do that ? Commented Jul 15, 2014 at 19:29
  • URL urlNew = urlBase.toURI().resolve(part1 + (char)47 + part2).toURL(); Commented Jul 15, 2014 at 19:34
  • That was a joke comment btw. http://docs.oracle.com/javase/7/docs/api/java/net/URI.html#relativize(java.net.URI) might be useful. But I don't know this API very well. Commented Jul 15, 2014 at 19:37
  • does URL urlNew = urlBase.toURI().resolve(part1).resolve(part2).toURL(); work? Commented Jul 15, 2014 at 19:38

2 Answers 2

0

Will this help?

public static void main(String[] args) throws MalformedURLException {
        URL domain = new URL("http://google.com");
        URL url = new URL(domain, "test");
        System.out.println(url.toString());
    }

Or

public static void main(String[] args) throws MalformedURLException {
        URL domain = new URL("http://google.com/sample/text/a");
        URL url = new URL(domain, "test");
        System.out.println(url.toString());
    }

Notice that it will add the string to your domain.

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

1 Comment

Thanks for reply, result : google.com/sample/text/test, but I want to get google.com/sample/text/a/test. 'Cruncher' solution is correct but it involves usage of '/' that like I wrote I'm trying to avoid (assumption: we can do this without typing '/')
0

What about this?

public static void main(String[] args) throws Exception {
    URL urlBase = new URL("http://google.com/2");
    String part1 = "test1/3";
    String part2 = "test2";
    System.out.println(
            urlBase.toString()
            + new URL(urlBase, part1).getPath()
            + new URL(urlBase, part2).getPath()
        );
}

No character is required :)

If you have an empty string, it won't work, so this is the solution for it:

public static void main(String[] args) throws Exception {
        URL urlBase = new URL("http://google.com/2");
        String part1 = "test1/3";
        String part2 = "test2";
        System.out.println(
                urlBase.toString()
                + (part1.isEmpty() ? "" : new URL(urlBase, part1).getPath())
                + (part2.isEmpty() ? "" : new URL(urlBase, part2).getPath())
            );
    }

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.