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
Related
Path name is : /storage/emulated/0/Xender/video/MyVideo.mp4
I am able to get last file name [MyVideo.mp4] from path using
String path="/storage/emulated/0/Xender/video/MyVideo.mp4";
String filename=path.substring(path.lastIndexOf("/")+1);
https://stackoverflow.com/a/26570321/5035015
Now i want to extract path [/storage/emulated/0/Xender/video] from this path.
I have one use of this path in my code so that i want to do this like this.
How can i do this?
Any help will be appreciated.
new File(path).getParentFile().getName() should work.
With regards to your current code, don't implement your own path parser. Use File.
Also note that this has nothing to do with Android specifically; this is a general Java question.
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())
First:
I know, this question was asked about 100 times already.
I know, someone already could have gave the right answer.
But anyway, I have to ask this again. I didn't found a solution working for me. sorry.
I'm writing a game in java. Of course I have many packages (folders) with sounds and pictures and so on. But these folders are each of variable size. So I want to save the content of such a folder dynamically in a list.
Usually, I was making this:
File f[] = new File(getClass.getResource("/home/res/").toURI()).listFiles();
Now I can iterate though this file object and save each file. Perfect. Really?
No. When I extract this Project into a jar archive, this fails. All because a uri isn't "hierarchical" or some stuff like this. See this exception:
C:\Users\Administrator\Desktop>java -jar Homework.jar
java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(Unknown Source)
at homework.moonface.src.Moonface.loadSounds(Moonface.java:94)
at homework.moonface.src.Moonface.<init>(Moonface.java:55)
at control.Overview.main(Overview.java:16)
Ok, I thought, so I need to get this path and add it manual into the file object. (new File("path"); But... this doesn't work. I'm getting the known error that the input wasn't written correctly, or when i try to cut of "file:" from the resource url, it breaks because in a jar its "jar:file:" and not "file:". But also if I cut of jar:file: I'm getting null.
So, please don't mark this as a duplicate, and try to explain this shortly for me. It would help thousand other, who don't understand other solutions who aren't solutions.
Try this :
URL jarResourceURL = getClass().getResource("/home/res/");
JarURLConnection jarURLConnection = (JarURLConnection) jarResourceURL.openConnection();
Enumeration<JarEntry> entries = jarURLConnection.getJarFile().entries();
while (entries.hasMoreElements()){
entries.nextElement(); // iterate over entries and do something
}
UPD: I was thinking about how spring framework's ClassPathXmlApplicationContext resolves the resources from jars. So i investigated the source code and foud that there is an utility class org.springframework.core.io.ClassPathResource which have a convenient interface (moreover there is a possibility to get the corresponding java.io.File instance using it) and can help you to solve the problem. Here is the doc :
http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/core/io/ClassPathResource.html
I have a java app where I'm trying to load a text file that will be included in the jar.
When I do getClass().getResource("/a/b/c/"), it's able to create the URL for that path and I can print it out and everything looks fine.
However, if I try getClass().getResource(/a/b/../"), then I get a null URL back.
It seems to not like the .. in the path. Anyone see what I'm doing wrong? I can post more code if it would be helpful.
The normalize() methods (there are four of them) in the FilenameUtils class could help you. It's in the Apache Commons IO library.
final String name = "/a/b/../";
final String normalizedName = FilenameUtils.normalize(name, true); // "/a/"
getClass().getResource(normalizedName);
The path you specify in getResource() is not a file system path and can not be resolved canonically in the same way as paths are resolved by File object (and its ilk). Can I take it that you are trying to read a resource relative to another path?
Why does ServletContext#getRealPath() not return me correct path if i use ../
This code works :-
System.out.println(context.getRealPath("/"));
This one doesn't :-
System.out.println(context.getRealPath("/.."));
How can i get one level up directory from getRealPath()?
Why does ServletContext#getRealPath() not return me correct path if i use "../":
To help protect you against requests that use ".." tricks to fetch content that they are not supposed to see; e.g. something like "../../../../../etc/passwd".
If you want to refer to a directory outside of the servlet context, you will need to create the path another way.