9

The default output of File.toURL() is

file:/c:/foo/bar

These don't appear to work on windows, and need to be changed to

file:///c:/foo/bar

Does the format

file:/foo/bar

work correctly on Unix (I don't have a Unix machine to test on)? Is there a library that can take care of generating a URL from a File that is in the correct format for the current environment?

I've considered using a regex to fix the problem, something like:

fileUrl.replaceFirst("^file:/", "file:///")

However, this isn't quite right, because it will convert a correct URL like:

file:///c:/foo/bar

to:

file://///c:/foo/bar

Update

I'm using Java 1.4 and in this version File.toURL() is not deprecated and both File.toURL().toString() and File.toURI().toString() generate the same (incorrect) URL on windows

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 3
    `file:/c:/foo/bar` won't work on UNIX... a UNIX machine does not have C: drive – Cristian May 07 '10 at 14:25
  • Indeed, you can just use unix-like paths in Windows. I.e. `/foo/bar`. The root will be on the same disk as the working directory of the Java program. – BalusC May 07 '10 at 14:26
  • What are you trying to do with this URL in string form that is not working? – Yishai May 07 '10 at 14:46
  • 2
    @Don then why don't you use `FileReader`? – Yishai May 07 '10 at 14:53
  • 1
    @Don, it only accepts a URL, or it only accepts a String referencing the file? If it only accepts a URL, pass it the URL object, don't wory about the `toString()` method. If it accepts a URL object and can't open the file, it has a bug, in that it relies on the toString method of URL to find the file instead of using the URL directly. – Yishai May 07 '10 at 15:31

3 Answers3

12

The File(String) expects a pathname, not an URL. If you want to construct a File based on a String which actually represents an URL, then you'll need to convert this String back to URL first and make use of File(URI) to construct the File based on URL#toURI().

String urlAsString = "file:/c:/foo/bar";
URL url = new URL(urlAsString);
File file = new File(url.toURI());

Update: since you're on Java 1.4 and URL#toURI() is actually a Java 1.5 method (sorry, overlooked that bit), better use URL#getPath() instead which returns the pathname, so that you can use File(String).

String urlAsString = "file:/c:/foo/bar";
URL url = new URL(urlAsString);
File file = new File(url.getPath());
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
6

The File.toURL() method is deprecated - it is recommended that you use the toURI() method instead. If you use that instead, does your problem go away?


Edit:

I understand: you are using Java 4. However, your question did not explain what you were trying to do. If, as you state in the comments, you are attempting to simply read a file, use a FileReader to do so (or a FileInputStream if the file is a binary format).

aperkins
  • 12,914
  • 4
  • 29
  • 34
  • 3
    +1 - it helps to read the javadocs ... http://java.sun.com/javase/6/docs/api/java/io/File.html#toURL() – Stephen C May 07 '10 at 14:25
  • 1
    @Don: I assumed you were using 1.5 or higher - apologies. I would still attempt to use .toURI(), as it may solve the problem (I seem to remember some problems with .toURL() back when I was using 1.4, but that was a while ago so I could be mistaken) – aperkins May 07 '10 at 14:38
  • Using URI instead of URL doesn't make any difference in Java 1.4 – Dónal May 07 '10 at 14:46
  • 1
    @Don: What exactly are you trying to do here? How are you initializing the File object? Does the File Object actually point to the file you think it does? – aperkins May 07 '10 at 14:53
1

What do you actually mean with "Does the format file:/c:/foo/bar work correctly on Unix"?

Some examples from Unix.

File file = new File("/tmp/foo.txt"); // this file exists
System.out.println(file.toURI()); // "file:/tmp/foo.txt"

However, you cannot e.g. do this:

File file = new File("file:/tmp/foo.txt");
System.out.println(file.exists()); // false

(If you need a URL instance, do file.toURI().toURL() as the Javadoc says.)

Edit: how about the following, does it help?

URL url = new URL("file:/tmp/foo.txt");
System.out.println(url.getFile());  // "/tmp/foo.txt"
File file = new File(url.getFile());
System.out.println(file.exists());  // true

(Basically very close to BalusC's example which used new File(url.toURI()).)

Jonik
  • 80,077
  • 70
  • 264
  • 372
  • 1
    @Don, If this is not helpful at all, can you please elaborate on what you meant about the format working correctly? – Jonik May 07 '10 at 14:40
  • Using URI instead of URL doesn't make any difference in Java 1.4 – Dónal May 07 '10 at 14:43
  • 2
    Right. But to be able to answer your question, what are you actually trying to do? I.e., how do you intend to use the format `file:/foo/bar` in code on Unix? – Jonik May 07 '10 at 14:49