When we use File.getAbsolutePath() we get the path of file as C:\BlahBlah\Blah.txt. But I want the path name as C:/BlahBlah/Blah.txt i.e instead of backward slash "\" i want a forward slash "/" in the path. How to get this. I tried to create a parsefile method.

public String parsePath(String path)
    {
        String parsedString="";        
        String[] arr = path.split("\ ");
        System.out.println(arr.length);
        for(int i=0;i<arr.length-1;i++)
            parsedString=arr[i]+"/";
        parsedString=parsedString+arr[arr.length-1];
        return parsedString;
    }

But it gives illegal space character

String[] arr = path.split("\");

What should I do?

I changed your function in the following way:

public String parsePath(String path)
    {
        String parsedString="";        
        String[] arr = path.split("\\\\");
        System.out.println(arr.length);
        for(int i=0;i<arr.length-1;i++)
            parsedString+=arr[i]+"/";
        return parsedString;
    }

The first change I made was to path.split. You will probably think that this looks pretty ridiculous.

This function takes a regular expression. The symbol you're trying to look for is the "\" character... which is special, and needs to be escaped in the regexp. So the regular expression is "\\".

But the compiler needs to have both symbols escaped AGAIN. It's a little unintuitive unless you've played with regexps for a while.

The second change was that you weren't doing anything productive in the For Loop. The code as it stands now works for me.

If anything is unclear, don't hesitate to mention it.

i want a forward slash "/" in the path

Run it on linux. It uses the / for separator

Just for interest, why do you want to do that? The implementation automatically uses a separator that is appropriate for the system upon which it's running.

The conversion of a pathname string to or from an abstract pathname is inherently system-dependent. When an abstract pathname is converted into a pathname string, each name is separated from the next by a single copy of the default separator character. The default name-separator character is defined by the system property file.separator, and is made available in the public static fields separator and separatorChar of this class. When a pathname string is converted into an abstract pathname, the names within it may be separated by the default name-separator character or by any other name-separator character that is supported by the underlying system.

This implies that you could change the file.separator system property to achieve what you ask, although I've never tried that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.