Results 1 to 3 of 3
  1. #1
    Jeremiah is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default Change Literal String to File Path with speratorChar

    I'm very very new to Java, and only been learning from reading things on the internet. So forgive me if this doesn't sound correct. I'll try my best. :)

    First thing my code does is look at an XML file to find the installation path of some software. The path could be anything. (\\servername\share\restofpath\ or C:\Program Files\etc, so it could have a couple "\" or many)

    At this point I've been able to open the file and create a variable that contains the path I'm looking for.

    For example:

    Java Code:
    String filepath = "C:\Program Files\Program Name\";
    My output is:
    C:\Program Files\Program Name\

    I'm trying to check to see if that folder is writable. When I try the following it doesn't work.

    Java Code:
    File checkfile = new File ("filepath");
    boolean isExecutable = checkfile.canWrite();
            if (isExecutable) {
                System.out.println("Folder is writable");
            } else {
                System.out.println("Folder is read only");
    This doesn't work. If I change the filepath string to just be just a filename/folder without any path in the working directory it checks it fine.

    I'm assuming I need to change my String filepath = "C:\Program Files\Program Name\" to something like

    filepath = "C:" + File.separatorChar + "Program Files" + File.separatorChar + "Program Name";

    What would be the best way to convert this? (keep in mind the length and number of seperatorChar's could be very long with lots of seperators)

    Thanks for any tips, and also please let me know if you need any additional information.
    Last edited by Jeremiah; 08-18-2011 at 05:44 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    File checkfile = new File ("filepath");
    Do you want the file named by the String "filepath" or do you want the file named by the contents of the filepath variable?
    If the later, remove the "s

  3. #3
    Jeremiah is offline Member
    Join Date
    Aug 2011
    Rep Power


    Thank you very much.... I was thinking that it wasn't working because I needed to change C:\program files\restofpath to a different format. Removing those quotes worked great. :)

Similar Threads

  1. Replies: 2
    Last Post: 02-04-2011, 10:10 PM
  2. Change to string
    By PhQ in forum New To Java
    Replies: 7
    Last Post: 04-14-2010, 03:26 PM
  3. Replies: 3
    Last Post: 03-26-2010, 07:09 AM
  4. Why doesnt the sound play when I change the path?
    By ProGenius in forum New To Java
    Replies: 10
    Last Post: 12-22-2009, 04:58 PM
  5. Regex - matching literal characters
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-07-2009, 11:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts