0

I am using Windows 10, latest JDK.

System.out.println(new File("F:\\some\\path\\to\\file.wav").getAbsolutePath());

This prints:

F:\some\path\project_folder\F:\some\path\to\file.wav !!!

Can you please explain what am I mistaking here? I tried even

new File(URI.create("file:///F:/some/path/to/file.wav")) but gives me

/F:/some/path/to/file.wav (a leading / makes it wrong)

Trying with only 2 slashes file://F:/some/path/to/file.wav I get an exception java.lang.IllegalArgumentException: URI has an authority component

UPDATE

This only occurs when I am referring to files in partition F:\ (by the way, the project itself is in F:\ also). Trying to refer to the C:\ which is the system drive, works normally.

UPDATE 2 Turns out that there are some parent directories shared between the file and project (F:\some\path\ in my example). Maybe this is the reason?! I tried new File("..\\to\\file.wav") but also printing wrong F:\some\path\project_folder\..\to\file.wav

FindOutIslamNow
  • 1,169
  • 1
  • 14
  • 33
  • Do you have the permissions to create a file in `F:\\\`? – Nicholas K Jan 12 '19 at 06:45
  • Yes, creating a file `F://some_file.wav` works – FindOutIslamNow Jan 12 '19 at 06:46
  • The problem could be related to [`File.isAbsolute()`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/File.html#isAbsolute()). The documentation says a file is absolute on Windows if it is prefixed with a drive specifier _followed by `"\\"`_. As `"F://"` works see if `"F:\\\\"` also works. (_Note: Using `"D:\\"` (non system drive) works for me._) – Slaw Jan 12 '19 at 07:00
  • @Slaw Good point. The bad news is that it is also printing `F:\some\path\project_folder\F:\some\path\to\file.wav` even when putting `F:\\\\\` – FindOutIslamNow Jan 12 '19 at 07:03
  • What happens when you try NIO? `Paths.get("F:", "some", "path", "to", "file.wav").toAbsolutePath()`. And does `File.isAbsolute` return true for your original `File` object (before `getAbsolutePath()`)? – Slaw Jan 12 '19 at 07:43
  • can you verify that `System.out.println("F:\\some\\path\\to\\file.wav".length());` returns 24? I suspect that there might be an invisible letter (like Unicode Left-To-Right-Mark) before or after the `F` and that because of this your path is not recognized as an absolute path – Thomas Kläger Jan 12 '19 at 09:26

1 Answers1

-2

you may copied path from windows file explorer use / in your path string instead of \

Amol Barge
  • 61
  • 1
  • 9