How do i convert paths that contain unc shares - java

If user has not mounted a remote drive and is just using the \\ syntax how do I convert such a path (\\nas) held in a String to a file in Java, sorry not really sure what you call this \\ naming.
Also is this windows specific, can it also be //

You can pass any valid file name to the constructor of a File and Java will handle that for you. e.g.
File input = new File("\\\\nas\\somefile.txt");
will work just fine. Note the escaped backslashs. Java can handle forward slashes just as well, so the above can be written as:
File input = new File("//nas/somefile.txt");
A filename like \\nas\somefile.txt is called a UNC path

Related

How to convert network path to URL in Java

I have literally searched the whole internet for this question but I have not found an answer. I have a file, in the network and I want to create an Itext image with it and for that, I have to convert its path to URL. The problem is when I use path.toURI().toURL() it appends my project path to the URL such that my URL ends up starting with C:/ which will not work.
Is there a way to just convert a string to file URL in java?
I have tried this:
String paths = "‪\\\\DESKTOP-A11F076\\Users\\Benson Korir\\Desktop\\walgotech\\passport.jpg";
String first = "file:" + paths.replaceAll("\\\\", "//").replaceAll("////", "//");
String second = "file://desktop-a11f076//Users//Benson Korir//Desktop//walgotech//passport.jpg";
System.out.println(first);
System.out.println(second);
The second string I have copied directly from the browser and it works fine. Funny this is these two strings output the same thing but the first string brings an error when I use it here:
Image image1 = Image.getInstance(second);
I am getting the error below:
java.io.FileNotFoundException: ‪\DESKTOP-A11F076\Users\Benson Korir\Desktop\walgotech\passport.jpg (The system cannot find the path specified)
If I got your requirement correctly, your path is a UNC file name, and that is the short form of an SMB path, with DESKTOP-A11F076 being the remote machine, and \Users\Benson Korir\Desktop\walgotech\passport.jpg being the path to the file on that machine.
If I am correct with that assumption, my understanding is that your URL have to look like this: smb://‪DESKTOP-A11F076/Users/Benson Korir/Desktop/walgotech/passport.jpg.
As far I remember is a Java java.io.File object capable to handle a UNC file name (this article implies that, too), but when translating it to a URI, it tries to make it absolute first, and there it fails in your case.
I usually avoid working on Windows whenever possible, therefore I have no environment to verify that.

Directory to string, Java

I'm new to java, I got the path from the user, using chooser.getCurrentDirectory(), now i want to use the directory to create a file there, File report = new File(chooser directory + "filename"), but it only accepts string, not file, so how can i get the chooser directory as a string?
You should not use chooser.getCurrentDirectory() to start off with, you should use chooser.getSelectedFile();.
And you should take a look at http://docs.oracle.com/javase/7/docs/api/java/io/File.html, specifically at the get*() methods that involve the filename.

Java - File not found with ö or ü in UNC

I'm working with java.io.File to handle some PDF files on an external storage. I was loading them in and noticed in my logs I got some File NotFoundExceptions. I was looking in the Exception and noticed a weird sign (a black square with a ? inside in = �) the UNC.
This is how I make my files. I get the String from a buffered reader if that makes a difference.
File myPathFile
FileReader fr = new FileReader(myPathFile);
BufferedReader br = new BufferedReader(fr)
String o = br.readLine();
File f = new File(o)
I thought it was the same as:
File f = new File("\\test\ö\myFile.pdf"); //When the UNC looks like this it won't work. (the ö is �)
File f2 = new File("\\test\myFile.pdf"); //This works
First, it's not a URL, it is a UNC.
Then, it's not a correct UNC, because you are forgetting some pretty important parts.
\\ComputerName\SharedFolder\Resource
Where the grouping is
[\\] flag to suggest it's a UNC
[ComputerName] The name of the remote computer
[\] separator character
[SharedFolder] The shared folder name
[\Resource] The path name of the remote file
Finally, you are not escaping your special characters within the string correctly, so you are probably not even getting off of your machine, much less getting to where you want.
To type the \ in Java, you need to know that \ is used for special characters like null, backspace, etc. So to get a single, normal \ you must type \\. That would make your first example.
On non-Microsoft systems, keep in mind that the remote path name probably uses / instead of \, and that / is not an escape character, so there is no need to type it twice.
File f = new File("\\\\test\\ö\\myFile.pdf");
Which won't work because you don't have a shared folder named ö
and your second example
File f2 = new File("\\\\test\\myFile.pdf");
Your second example wouldn't work, because it lacks a resource to be opened.
Assuming you didn't want to walk off of your machine, you are missing parts of a local file name. On windows a full path includes the "volume name" which is typically C: or something similar.
File f = new File("c:\\test\\myFile.pdf");
Sometimes people omit the volume name, in which case you rely upon the operating system's good graces to attempt to find the file for you. Generally speaking, it's not a great idea.
File f = new File("\\test\\myFile.pdf");
But even though this looks similar to your second example, it contains an extra forward slash to escape the forward slash. While your actual example doesn't.

Convert URL to AbsolutePath

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.

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