Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the path to access a file in both windows and linux uniquely using slash seperator

I have an application written in java in which forward slash to specify the file and directory path . The file and directory can access when the application run on linux. But when it run on windows it says an error that the specified path is incorrect.How to uniquely specify the path of the file.

In java iam using this command:

public static final String WD ="/qark-master/qark/qarkMain.py";
public static final String MANIFESTPATH="/apktool/AndroidManifest.xml";

Please help me here!

like image 805
Maria Avatar asked Jan 18 '17 06:01

Maria


People also ask

Can I use forward slash in Windows path?

You can use forward slashes ( / ) instead of backward slashes ( \ ) on Windows (as is the case with Linux® and UNIX). If you use backward-slashes, the file name on Windows might be treated as a relative-path instead of an absolute-path.

What does \\ mean in Windows path?

They indicate that the path should be passed to the system with minimal modification, which means that you cannot use forward slashes to represent path separators, or a period to represent the current directory, or double dots to represent the parent directory.

What method can be used to create file paths in programs that need to work both in Windows and Linux?

On Windows, paths are written using backslashes (\) as the separator between folder names. OS X and Linux, however, use the forward slash (/) as their path separator. If you want your programs to work on all operating systems, you will have to write your Python scripts to handle both cases.

What are the 2 types of paths in Linux?

There are two basic types of paths: relative paths and absolute paths. A relative path is the location of a file relative to the current directory. The current directory is the directory in which the user is currently working.


1 Answers

As mentioned by Jim Garrison, forward slash works in Windows as well as in Unix. Problem is with drive letter or root directory. When in Windows path defined from root like /qark-master it is a root directory of the current drive. But... use absolute path in the code either in Windows with drive letter or from root in Linux is not a good idea. Much better is to use relative path either from current running directory or special environment variable. Then you can use forward slash and do not care about path separator.

From other hands - there is a System property in JVM called "file.separator" and it is possible to construct a path with it according to OS. Of course problem for absolute path with drive letter for Windows is there anyway.

like image 195
Vadim Avatar answered Oct 16 '22 13:10

Vadim