I want to clean the path I use in my App. The path can be modified and sometimes I got something like that:
C:/users/Username/Desktop/\..\..\..\Windows\Web\..\..\Program Files\..\Program Files\..\Python27\
But I would like to have something like:
C:\Python27\
That's an example!
How can I clean the path to get only the necessary part?
Thanks.
If fileName is your filename string, then something like:
String cleanedFilename = new File(fileName).getCanonicalPath();
should do it...
Se also the API description.
Here is the code I have just tried.
new File("c:/temp/..").getCanonicalPath();
It returns 'C:\', that is right. The parent of c:/temp is indeed c:\
You could try using the File.getCanonicalPath() method:
File file = new File("my/init/path");
String path = file.getCanonicalPath();
I haven't test though, tell us back!
EDIT:
#MathiasSchwarz is right, use getCanonicalPath() instead of getAbsolutePath() (link)
Related
I want to get the full path for a FileSystem which I created like this.
FileSystem fs = FileSystems.newFileSystem(Paths.get(folder.getRoot().getAbsolutePath(), "test.zip"), null);
Whatever I tried until now only got me an output like / and thats it, but what i really need to get is:
C:/Users/username/AppData/Local/Temp/junit9210120109362016454/Parent/test.zip/
Is there any way to get this information from the FileSystem object? I know I could just take the parameter I used to create it in the first place, but I want to be sure I don't mix anything up and I think it's better like that for a unit-test
Based on the accepted answer I came up with this to solve my problem:
Paths.get(fs.toString(), "file.txt").toString()
Use
Path from java.nio.file.Path;
Path path = Paths.get(directory.toString());
String fullpath=path.toUri().toString()
which will give full path like file:///F:/somedir1/somdir2/17f5b00a-bd6e-4109-8ce5-85df79b51a00.jpg
How can I check if a given file is under other file? For example, I have new Paths.get("/foo/bar/") and new Paths.get("./abc/def.jar").
And I want to check whether the second is under /foo/bar.
I can figure out some string based comparisons like path.toFile().getAbsolutePath().startsWith(path2.toFile().getAbsolutePath()), but that looks fragile and not right.
I've found this useful Path.startsWith( Path other ) method, so this worked for me:
inputPath.toAbsolutePath().startsWith(outputDirectory.toAbsolutePath())
My xml file is place in this path "C/users/input/abc.xml".
I am executing code from "c/users/prg/abc.java"
Using getAbsolutePath function on Xml file object but i am getting this"c/users/prg/abc.xml" as result.
i have already tried using the below code:
File file = new File("C:\\users\\Input\\abc.xml");
String absPath = file.getAbsolutePath();
System.out.print(absPath);
output: C\users\prg\abc.xml
is it Classpath issue? or Am i doing something wrong?
The absolute path is like below
C:\users\Input\abc.xml
Once again rebuild ur application. What your expecting from us.
Regards,
Sekhar
I Think the error is occurring due to wrong class path.
Check the class path if it is right and let me know.
I'm trying to set the system property "java.security.policy" programmatically.
It works, as long as the path to the security policy file has no spaces.
File myFileReference = new File("C:\folder_name\security.policy")
System.setProperty("java.security.policy", myFileReference.getAbsolutePath());
System.setSecurityManager(new RMISecurityManager());
If there are spaces int the file path, they get escaped with a %20, like this
"C:\folder%20name\security.policy".
The code above executes fine, but then all security checks fail. I assume setProperty doesn't really find the file.
On Windows, writing the file name without that escaping for spaces works.
System.setProperty("java.security.policy", "C:\\some folder\\wideopen.policy");
So, the problem seems to be that %20 space escaping. I could replace it using a regex, but maybe that would make it work just on Windows, and fails somewhere else.
Also, I don't want to hard-code the file path like that.
I looked at the Java doc for a File function that returns a "System.setProperty" compatible path name that works on any platform. I also tried things like toURI().toString(), to no avail.
Is there an elegant way to get a working file path String from a File reference in 1 line of code?
EDIT:
This was simplified code, I construct the file like this
URL policyURL = Class.class.getResource("/sub local folder/wideopen.policy");
new File(policyURL.getFile())
I needed a relative path, so I used that little getResource trick, which happens to return an URL, with the nasty %20 escapings.
I can use an URLDecoder to strip them away now that I know what the problem is.
But is there a less error prone way?
I solved using the URLDecoder class. Here is a complete example of what I was trying to do, and the solution that made it work.
//here is the trick
URL policyFileURL = Class.class.getResource("/server/model/easy.policy");
String policyFilePath = "";
try {
policyFilePath = URLDecoder.decode(policyFileURL.getFile(), "UTF-8");
}
catch (UnsupportedEncodingException e) {}
//here what I wanted to do (now it works)
server.activate(port, new File(policyFilePath));
I've this brief snippet:
String target = baseFolder.toString() + entryName;
target = target.substring(0, target.length() - 1);
File targetdir = new File(target);
if (!targetdir.mkdirs()) {
throw new Exception("Errore nell'estrazione del file zip");
}
doesn't mattere if I leave the last char (that is usually a slash). It's done this way to work on both unix and windows. The path is actually obtained from the URI of the base folder. As you can see from baseFolder.toString() (baseFolder is of type URI and is correct). The base folder actually exists. I can't debug this because all I get is true or false from mkdir, no other explanations.The weird thing is that baseFolder is created as well with mkdir and in that case it works.
Now I'm under windows.
the value of target just before the creation of targetdir is "file:/C:/Users/dario/jCommesse/jCommesseDB"
if I cut and paste it (without the last entry) in windows explore it works...
The path you provide is not a file path, but a URI.
I suggest you try the following :
URI uri = new URI("file://c:/foo/bar");
File f = new File(uri).
It looks, to me, as if the "file:/" at the beginning is the problem... Try getAbsolutePath() instead of toString().
The File constructor taking a String expects a path name. A path name is not an URI.
Remove the file:/ from the front of the String (or better yet, use getPath() instead of toString()) to get to the path you need.