Since Unix reads only '/' for folder structure and windows takes both '\' ,'/' .Is there a way to check if dir exists for both in Java? if folder structure has '\' and we use --> 'f.isDirectory()' , the Unix doesn't read the folder structure. Thanks in advance.
Asked
Active
Viewed 274 times
-2
-
2Does this answer your question? [Platform independent paths in Java](https://stackoverflow.com/questions/3548775/platform-independent-paths-in-java) – Martheen Jul 28 '20 at 06:06
-
no,it doesnt really help, thanks anyways – Noobie Jul 28 '20 at 06:09
-
"If the folder structure has \ ...". Well make sure it doesn't have \ before you try. For example by converting them. – Kayaman Jul 28 '20 at 06:36
-
@Kayaman - I am trying to unzip a file which has folders, files seperated with "\" , so it gets unzipped fine in windows and not in Unix – Noobie Jul 28 '20 at 13:40
-
@Noobie zip files are platform agnostic. If you've managed to somehow create a problem with zip files due to the path separator, you're doing something more complicated than it needs to be, and wrong. – Kayaman Jul 29 '20 at 06:26
-
Could you post the code that you are having a problem with, otherwise it is hard to help you. – DuncG Jul 29 '20 at 08:54
-
Very good question. I struggle with this my self. Please let me know if you found a solution. Those who gives negative credit are not reading what you actually asked. It's simple and clear. – Lars Hansen Aug 01 '22 at 09:11
2 Answers
1
You could look into using the Path API from java.nio.file
:
Path rootDirectory = Path.of(System.getProperty("user.home")); // Let's say /home/noobie
Path subDirectory = rootDirectory.resolve("sub"); // home/noobie/sub
Path subSubDirectory = subDirectory.resolve("subsub"); // /home/noobie/sub/subsub
Path subSubDirectoryMethod2 = rootDirectory.resolve("sub").resolve("subsub"); // /home/noobie/sub/subsub
This API works regardless of platform. To check if a path is a directory you can do:
boolean isDir = Files.isDirectory(path);

Jeroen Steenbeeke
- 3,884
- 5
- 17
- 26
0
I found this post, it mentions the difference between Linux and Windows and how to fix it with File.seperator

Matteo Valentini
- 46
- 2