Convert URL to AbsolutePath - java

Is there any easy way to convert a URL that contains to two-byte characters into an absolute path?
The reason I ask is I am trying to find resources like this:
URL url=getClass().getResources("/getresources/test.txt");
String path=url.toString();
File f=new File(path);
The program can't find the file. I know the path contain '%20' for all spaces which I could convert but my real problem is I'm using a japanese OS and when the program jar file is in a directory with japanese text (for example デスクトップ) I get the URL-encoding of the directory name,
like this:
%e3%83%87%e3%82%b9%e3%82%af%e3%83%88%e3%83%83%e3%83%97
I think I could get the UTF-8 byte codes and convert this into the proper characters to find the file, but I'm wondering if there is an easier way to do this. Any help would be greatly appreciated.
nt

URL url = getClass().getResource("/getresources/test.txt");
File f = new File(url.toURI());

If you were interested in getting Path from URL, you can do:
Path p = Paths.get(url.toURI());

File has a constructor taking an argument of type java.net.URI for this case:
File f = new File(url.toURI());

Another option for those who use Java 11 or later:
Path path = Path.of(url.toURI());
or as a string:
String path = Path.of(url.toURI()).toString();
Both methods above throw a URISyntaxException that can be safely ignored if the URL is guaranteed to be a file URL.

Related

Java.io.file constructor to deal with UNC file path

When I try to use the JAR file in the UNC path, I find I met a problem. The constructor of java.io.file will always convert a UNC file path to local path.
For example, I try
String dirStr = "file:\\\\dir1\dir2\file.jar!Myclass";
File ff = new File(dirStr);
System.out.println(ff.toString());
I'll get output like: file:\dir1\dir2\file.jar!Myclass. But what I expect to get is file:\\dir1\dir2\file.jar!MyClass.
I tried to add more slashes in the dirStr, but it can't work. Because in the java.io.file, it'll call method to remove duplicated slashes.
And I try to use the URI to create the ff. But the output will be \dir1\dir2\file.jar!Myclass, which is not available to use JAR file successfully. I think the form of JAR must be start with the file: protocol to use parse the string ending with ! in above string \dir1\dir2\file.jar!Myclass.
Is there any way can new File() to get the pathname of File, i.e. ff, like file:\\dir1\dir2\file.jar!MyClass.
Since your input dir String is UNC type, i think you should use Java's URI.
Example code:
URI uri = new URI(dirStr);
System.out.println(uri.toString()); // If you want to get the path as URI
File ff = new File(uri.getPath()); // If you want to access the file.
The other better way is using Path:
URI uri = new URI(dirStr);
Path path = Paths.get(uri); // Or directly Path path = Paths.get(dirStr);
File ff = path.toFile(); // << your file here
path.toUri(); // << your uri path here
The constructor File(String) takes a path, not a URL. Remove the file: part and use two backslashes for every one in the actual filename, to satisfy the compiler's escaping rules. Or use the correct number of forward slashes.

Get path directory only and discard file in Java

How do we actually discard last file from java string and just get the file path directory?
Random Path input by user:
C:/my folder/tree/apple.exe
Desired output:
C:/my folder/tree/
closest solution i found is from here . The answer from this forum only display last string acquired not the rest of it. I want to display the rest of the string.
The easiest and most failsafe (read: cross-platform) solution is to create a File object from the path.
Like this:
File myFile = new File( "C:/my folder/tree/apple.exe" );
// Now get the path
String myDir = myFile.getParent();
Try that:
String path = "C:/my folder/tree/apple.exe";
path = path.substring(0, path.lastIndexOf("/")+1);

Convert URL to normal windows filename Java

Is there a way to convert this:
/C:/Users/David/Dropbox/My%20Programs/Java/Test/bin/myJar.jar
into this?:
C:\Users\David\Dropbox\My Programs\Java\Test\bin\myJar.jar
I am using the following code, which will return the full path of the .JAR archive, or the /bin directory.
fullPath = new String(MainInterface.class.getProtectionDomain()
.getCodeSource().getLocation().getPath());
The problem is, getLocation() returns a URL and I need a normal windows filename.
I have tried adding the following after getLocation():
toString() and toExternalForm() both return:
file:/C:/Users/David/Dropbox/My%20Programs/Java/Test/bin/
getPath() returns:
/C:/Users/David/Dropbox/My%20Programs/Java/Test/bin/
Note the %20 which should be converted to space.
Is there a quick and easy way of doing this?
The current recommendation (with JDK 1.7+) is to convert URL → URI → Path. So to convert a URL to File, you would say Paths.get(url.toURI()).toFile(). If you can’t use JDK 1.7 yet, I would recommend new File(URI.getSchemeSpecificPart()).
Converting file → URI: First I’ll show you some examples of what URIs you are likely to get in Java.
-classpath URLClassLoader File.toURI() Path.toUri()
C:\Program Files file:/C:/Program%20Files/ file:/C:/Program%20Files/ file:///C:/Program%20Files/
C:\main.c++ file:/C:/main.c++ file:/C:/main.c++ file:///C:/main.c++
\\VBOXSVR\Downloads file://VBOXSVR/Downloads/ file:////VBOXSVR/Downloads/ file://VBOXSVR/Downloads/
C:\Résume.txt file:/C:/R%c3%a9sume.txt file:/C:/Résume.txt file:///C:/Résume.txt
\\?\C:\Windows (non-path) file://%3f/C:/Windows/ file:////%3F/C:/Windows/ InvalidPathException
Some observations about these URIs:
The URI specifications are RFC 1738: URL, superseded by RFC 2396: URI, superseded by RFC 3986: URI. (The WHATWG also has a URI spec, but it does not specify how file URIs should be interpreted.) Any reserved characters within the path are percent-quoted, and non-ascii characters in a URI are percent-quoted when you call URI.toASCIIString().
File.toURI() is worse than Path.toUri() because File.toURI() returns an unusual non-RFC 1738 URI (gives file:/ instead of file:///) and does not format URIs for UNC paths according to Microsoft’s preferred format. None of these UNC URIs work in Firefox though (Firefox requires file://///).
Path is more strict than File; you cannot construct an invalid Path from “\.\” prefix. “These prefixes are not used as part of the path itself,” but they can be passed to Win32 APIs.
Converting URI → file: Let’s try converting the preceding examples to files:
new File(URI) Paths.get(URI) new File(URI.getSchemeSpecificPart())
file:///C:/Program%20Files C:\Program Files C:\Program Files C:\Program Files
file:/C:/Program%20Files C:\Program Files C:\Program Files C:\Program Files
file:///C:/main.c++ C:\main.c++ C:\main.c++ C:\main.c++
file://VBOXSVR/Downloads/ IllegalArgumentException \\VBOXSVR\Downloads\ \\VBOXSVR\Downloads
file:////VBOXSVR/Downloads/ \\VBOXSVR\Downloads \\VBOXSVR\Downloads\ \\VBOXSVR\Downloads
file://///VBOXSVR/Downloads \\VBOXSVR\Downloads \\VBOXSVR\Downloads\ \\VBOXSVR\Downloads
file://%3f/C:/Windows/ IllegalArgumentException IllegalArgumentException \\?\C:\Windows
file:////%3F/C:/Windows/ \\?\C:\Windows InvalidPathException \\?\C:\Windows
Again, using Paths.get(URI) is preferred over new File(URI), because Path is able to handle the UNC URI and reject invalid paths with the \?\ prefix. But if you can’t use Java 1.7, say new File(URI.getSchemeSpecificPart()) instead.
By the way, do not use URLDecoder to decode a file URL. For files containing “+” such as “file:///C:/main.c++”, URLDecoder will turn it into “C:\main.c  ”! URLDecoder is only for parsing application/x-www-form-urlencoded HTML form submissions within a URI’s query (param=value&param=value), not for unquoting a URI’s path.
2014-09: edited to add examples.
String path = "/c:/foo%20bar/baz.jpg";
path = URLDecoder.decode(path, "utf-8");
path = new File(path).getPath();
System.out.println(path); // prints: c:\foo bar\baz.jpg
The current answers seem fishy to me.
java.net.URL.getFile
turns a file URL such as this
java.net.URL = file:/C:/some/resource.txt
into this
java.lang.String = /C:/some/resource.txt
so you can use this constructor
new File(url.getFile)
to give you the Windows path
java.io.File = C:\some\resource.txt
As was mentioned - getLocation() returns an URL. File can easily convert an URI to a path so for me the simpliest way is just use:
File fullPath = new File(MainInterface.class.getProtectionDomain().
getCodeSource().getLocation().toURI());
Of course if you really need String, just modify to:
String fullPath = new File(MainInterface.class.getProtectionDomain().
getCodeSource().getLocation().toURI()).toString();
You don't need URLDecoder at all.
The following code is what you need:
String path = URLDecoder.decode("/C:/Users/David/Dropbox/My%20Programs/Java/Test/bin/", "UTF-8");
System.out.println(new File(path).getPath());
Hello confused people from the future. There is a nuance to the file path configuration here. The path you are setting for TESSDATA_PREFIX is used internally by the C++ tesseract program, not by the java wrapper. This means that if you're using windows you will need to replace the leading slash and replace all other forward slashes with backslashes. A very hacky workaround looks like this:
URL pathUrl = this.getClass().getResource(TESS_DATA_PATH);
String pathStr = pathUrl.getPath();
// hack to get around windows using \ instead of /
if (SystemUtils.IS_OS_WINDOWS) {
pathStr = pathStr.substring(1);
pathStr = pathStr.replaceAll("/", "\\\\");
}

Generate URL for File

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
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());
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).
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()).)

Java: Does FileReader support finding files using strings with %20?

My apologies if this is too simple a question, I was unable to google it as it did not like searching for %20.
If I have a URL on which I use the getFile() method to obtain the path for a file I would like to open for processing. If the particular file resides in a directory that contains spaces, the path returned would contains %20 where the space should be.
Will a FileReader then be able to use the path as provided, or will I need to replace the %20 with a space?
You will need to use URLDecoder yourself. FileReader just uses the String it's handed, and rightly so - %20 is a perfectly valid character sequence in a file name, and if it were automatically converted you could not access files containing it.
Use URLDecoder.decode() to decode a path
If you downloaded the file, and saved it on local file system.
And you are using FileReader to read it
as
FileReader fr = new FileReader(new File(url.getFile()));
Yes File, can understand URL encoding. So you don't need to decoded it.
If you decoded it as others have suggested it will be more readable when
you print the file path.

Categories