File and URL and String paths to files in URL string form [message #306706] |
Thu, 03 August 2006 13:11  |
Eclipse User |
|
|
|
Originally posted by: PlanetTen.comcast.net
Hello not sure I'm posting to the right eclipse forum, but wanted to
really hear from the community on how they are dealing with java URL and
File objects and to ask about best practices when dealing with paths to
files.
So some background, one thing I have ran into are numerous cases where a
URL string can be passed into API (including eclipse api), this API will
then try to use the URL as a java FILE object for file operations such
as File.exists() and in many cases fail.
for example heres a java example of File and URL problem
---------------------------------
String startPath = "c:\\temp\\bug\\myfile.txt"; //$NON-NLS-1$
//start with a regular file
File startFile = new File(startPath);
if (startFile.exists()){
System.out.println("File Exists");
}
//have File convert to URL
URL url = startFile.toURL();
String urlAsString = url.toString();
//create a new File object with the url string
//**** "file:/c:\\temp\\bug\\myfile.txt" *******
File f = new File(urlAsString);
System.out.println(urlAsString);
//This fails..though it is a file, and does exist on the local machine
if (f.isFile()){
System.out.println("Never get here");
}
//Never get in here, though this is a valid file path on this machine
if (!f.exists()){
System.out.println("This always says file does not exist though it
actually does exist");
}
-----------------------------------
What are the best practices for dealing with Strings to files that
actually may be in URL form?
|
|
|
Re: File and URL and String paths to files in URL string form [message #306710 is a reply to message #306706] |
Thu, 03 August 2006 13:42  |
Eclipse User |
|
|
|
PlanetTen wrote:
> //have File convert to URL
> URL url = startFile.toURL();
> String urlAsString = url.toString();
>
> //create a new File object with the url string
> //**** "file:/c:\\temp\\bug\\myfile.txt" *******
> File f = new File(urlAsString);
public File(String) expects a filename, not a URL ... you can convert
your string back into a URI and give it to the File(URI) constructor, if
you really want.
Later,
PW
|
|
|
Powered by
FUDForum. Page generated in 0.03265 seconds