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

- 588,226
- 146
- 1,060
- 1,140

- 2,630
- 15
- 48
- 67
-
What's your goal? Do you want the contents of that URL as a String? Mangling a URI (not URL) into a File to pass to FileReader won't get you there, because that class only reads *files* (URIs starting with `file://`), not *web pages* (`http://`). – Philipp Reichart Oct 29 '11 at 14:28
-
@ Philipp Reichart I want to change the contents of a file (when working with applet) on my server using FileWriter. – program-o-steve Oct 29 '11 at 14:49
-
Most answers to your question assumed you would only want to read the contents -- writing a remote location won't work with `FileWriter` (files are local), I suggest you open a new question for that. – Philipp Reichart Oct 29 '11 at 15:00
-
Possible duplicate of [Convert String to Uri](https://stackoverflow.com/questions/3487389/convert-string-to-uri) – james.garriss Sep 25 '17 at 17:15
5 Answers
You can use the new URI(string)
constructor (for an URI) and the new URL(string)
constructor for a URL
.
But that won't work with a FileReader
- it requires the URI scheme to be file:
If you want to read a remote file, you need something like:
Reader reader = new InputStreamReader(new URL(urlString).openStream(), encoding);
The encoding can be taken from HttpURLConnection
obtained by url.openConnection()
, but you can also set it to something specific if you know it in advance. (Btw, In the above example I've omitted all I/O resource management)
Note (thanks to @StephenC): the url string must be a valid URL, which means it must start with http://

- 588,226
- 146
- 1,060
- 1,140
-
+1 for guessing what the OP really wants, but this still ignores any content encoding sent by the server. – Philipp Reichart Oct 29 '11 at 14:40
-
yeah, when using `InputStreamReader` I always set the encoding, and when I give examples with it I always forget :) – Bozho Oct 29 '11 at 14:47
-
java is on one side, a server is on the other side, http is a protocol. You need a different protocol. FTP for example – Bozho Oct 29 '11 at 17:19
-
@ Bozho If i use FTP will i be able to use FileReader and FileWriter to write on the files hosted on the remote server ? – program-o-steve Oct 30 '11 at 15:56
-
-
So jdk's io library can only be used to write/read files to/from system ? – program-o-steve Oct 30 '11 at 16:37
If your goal is to turn that string is to turn that string (and similar ones) into syntactically valid URLs ... like a typical browser's URL bar does ... then the answer is "use heuristics".
Specifically, you will need to figure out what heuristics are most likely to turn the user's input into the URL that the user meant. Then write some code to implement them. I'd start by doing some experiments with a browser whose behavior you like and try to figure out what it is doing.
Your comment is unclear, but I think you are saying that the string is a local file name and you want to turn it into "file:" URL.
File file = new File("www.mywebsite.com/firefox.txt");
URL url = file.toURI().toURL();
If you want to write to a remote file, you can't do that simply using URL object and openStream()
. Writing to remote resources identified by URLs typically requires an http or ftp URL, and use of libraries that implement the respective protocol stack.
Similarly, you can't do it withe the FileWriter API ... unless the file lives in a file system that has been mounted; e.g. as an Windows network share. The FileWriter API only works for files in the unified "file system" that the local operating system makes available to 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 type into a browser's URL bar.

- 698,415
- 94
- 811
- 1,216
-
I want to change the contents of a file (when working with applet) on my server using FileWriter. And the address of the file hosted is a String so i want to change it into a url/uri – program-o-steve Oct 29 '11 at 14:51
If you mean that you need to convert That string to a URL string, you can use the URLEncoder class to decode/encode Strings in Url format.
for example:
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)

- 4,913
- 9
- 47
- 86
new URL("--your url here--") should turn it into a URL, however the File constructor takes 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 e.t.c.) { }
However to get the contents of the url you might want to try
new URL("--your url here--").openStream();
and maybe wrap this in an InputStreamReader ala;
new InputStreamReader( new URL("--your url here--").openStream() );
Not tried any of this but see no reason it won't work :)
It may surprise the unintelligent among you that --your url here-- means you to fill in your own url.

- 306
- 3
- 14
-
1This won't work I think. The URL constructor requires a syntactically valid URL string ... and that string is not syntactically correct. – Stephen C Oct 29 '11 at 14:35
-
-
wow, you really are petty shan't try and help anyone else in the future. I didn't put a protocol as the person asking the question didn't add one I assume they would know what it is. Don't worry I won't try and help anyone ever again. – default_avatar Oct 29 '11 at 15:09
-
Woah, I didn't mean to offend you, I hoped the smiley at the end would give that away. I'm sorry. We're all here to help and learn. – Philipp Reichart Oct 29 '11 at 15:42
-
@RichardJohnson - actually, I think that what the OP wants is something that will turn (non-url) strings like that into valid urls. – Stephen C Oct 29 '11 at 16:33
-
@PhilippReichart sorry bad day. If the OP wants that then maybe he could use Httpclient, what do you guys think? – default_avatar Oct 29 '11 at 16:42
-
The OP actually wants "to change the contents of a file (when working with applet) on [his] server using FileWriter." (from his comment on the question), so we were all guessing in the right direction (getting the contents) but not far enough (writing the contents) ;) – Philipp Reichart Oct 29 '11 at 17:12
If you want to read the contents of a URL like http://www.mywebsite.com/firefox.txt
, FileReader
won't help you even if you mangle a URL (or rather URI) into a java.io.File
somehow.
Using URL.openStream()
and an InputStreamReader
will get you the string contents of an URL, but might mess up the content because you might ignore the content encoding sent by the server.
I suggest Apache HttpClient and something liek 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 plain text file, this should give you the contents without any encoding issues. (HTML files can specifiy their encoding using meta tags, that can't be handled 100% using an HTTP client)

- 20,771
- 6
- 58
- 65