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.
source share