Convert string to URI

How to convert String www.mywebsite.com/firefox.txt to URL? I wanted to use this in a file object, and then in the FileReader constructor.

+4
source share
5 answers

You can use the new URI(string) constructor (for the URI) and the new URL(string) constructor for the URL .

But this will not work with FileReader - this requires the URI scheme to be file:

If you want to read the remote file, you need something like:

 Reader reader = new InputStreamReader(new URL(urlString).openStream(), encoding); 

The encoding can be taken from the HttpURLConnection obtained using url.openConnection() , but you can also set it to something specific if you know it beforehand. (Btw, In the above example, I omitted all I / O resource management)

Note (thanks @StephenC): The URL string must be a valid URL, which means it must start with http://

+4
source

If your goal is to turn this string, to turn that string (and similar ones) into syntactically valid URLs ... for example, a typical browser URL string ... then the answer will be "use heuristics."

In particular, you will need to figure out which heuristics are most likely to turn user input into the URL that the user has. Then write the code to implement them. I would start by experimenting with a browser whose behavior you like and try to figure out what it does.


Your comment is unclear, but I think you say that the string is the local file name, and you want to turn it into the URL "file:".

 File file = new File("www.mywebsite.com/firefox.txt"); URL url = file.toURI().toURL(); 

If you want to write to a remote file, you cannot do this simply by using the URL object and openStream() . Writing to remote resources identified by URLs usually requires an http or ftp URL, as well as using libraries that implement the appropriate protocol stack.

Likewise, you cannot do this using the FileWriter API ... if the file is not in the file system that was mounted; for example, as a Windows network share. The FileWriter API only works for files in a single "file system" that is available to the local program for your application.

Finally, we get back to the fact that you need to use a valid URL ... not just the syntactically invalid nonsense that most users enter into the browser URL string.

+1
source

If you mean that you need to convert this string to a URL string, you can use the URLEncoder class to decode / encode Url format strings.

eg:

 String encodedurlStr = URLEncoder.encode(url.toString(),"UTF-8"); 

if you need to get a URL type, the answer below is correct (using the URL class constructor)

0
source

the new url ("- your url is here--") should turn it into a url, however the file constructor accepts a uri, not a url, so ...

 try { URL url = new URL("--your url here--"); File f = new File( url.toURI() ); FileReader reader = new FileReader(f); } catch (MalformedURL...IOException etc) { } 

However, to get the contents of the url, you can try

 new URL("--your url here--").openStream(); 

and maybe wrap it in InputStreamReader ala;

 new InputStreamReader( new URL("--your url here--").openStream() ); 

I have not tried any of this, but I see no reason why it will not work :)

It may surprise you unreasonable that your address is here - it means that you must fill in your own URL.

0
source

If you want to read the contents of a URL, for example http://www.mywebsite.com/firefox.txt , FileReader will not help even if you somehow kill the URL (or rather the URI) in java.io.File .

Using URL.openStream() and InputStreamReader , you get the contents of the URL string, but can mess up the content because you can ignore the encoding of the content sent by the server.

I suggest Apache HttpClient and something like this:

 String url = "http://www.mywebsite.com/firefox.txt"; HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(new HttpGet(url)); String contents = EntityUtils.toString(response.getEntity()); 

For a text file, this should provide you with the content without any encoding problems. (HTML files can determine their encoding using meta tags that cannot be 100% processed using an HTTP client)

0
source

All Articles