4

I have the following path: com/teama/merc/test/resTest And I want to convert it to this: com\teama\merc\test\resTest

I am trying to append the above path to this path: C:\Users\Toby\git\MERCury\MERCury\ With str.replace('/', '\\'); But when I append both of the strings together, this is the output: C:\Users\Toby\git\MERCury\MERCury\com/teama/merc/test/resTest

Here is the code in question:

    String home = System.getProperty("user.dir");
    path.replace('/', '\\');
    System.out.println(path);

    String folder = home + File.separatorChar + path;
    System.out.println(folder);

    File file = new File(folder);
    if(file.isDirectory())
    {
        System.out.println(file.getPath() + " is a directory");
    }

The appended path is not seen as a folder because of the slashes. Any help?

Edit: Just to clarify, the full path (both strings appended) is infact a folder.

user3316633
  • 137
  • 5
  • 13
  • Unless for some reason you specifically want '\\', you might want to consider using `System.getProperty("file.separator");`. [See File docs](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#separatorChar) – Java Devil Mar 10 '14 at 01:06
  • 1
    Why? It will work as a filename in Java either way. NB your title is back to front from your question. – user207421 Mar 10 '14 at 01:09
  • No, it did not work. I have no idea why would say it does work after I have clearly tried to make it work. – user3316633 Mar 10 '14 at 02:12
  • Didn't work *how?* It's worked for me for 17 years. – user207421 Mar 10 '14 at 11:03
  • The full appeneded path when opened using a File does not print out that it is a folder. I have no idea why, but it doesn't. Trying to convince me you are right isn't helping, I'm not saying you're wrong, I'm just saying it isn't working how you are saying it should. – user3316633 Mar 10 '14 at 18:59
  • Telling us it isn't working without evidence isn't helping either. If user.dir + path doesn't register as a directory, presumably it isn't a directory. You haven't produced any counter-evidence. Try 'new File(home, path).isDirectory()'. – user207421 Mar 11 '14 at 11:53
  • I've already fixed it, but thank you for trying. – user3316633 Mar 12 '14 at 16:28

1 Answers1

8

In java, String's aren't mutable, so when you change them with something like the replace method, you have to reassign the variable to that changed String. So, you would have to change the replace code to this:

path = path.replace('/', '\\');
kabb
  • 2,474
  • 2
  • 17
  • 24