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

    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\";
    
    System.out.println(filepath);
    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 06:44 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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
    Posts
    2
    Rep Power
    0

    Default

    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, 11:10 PM
  2. Change to string
    By PhQ in forum New To Java
    Replies: 7
    Last Post: 04-14-2010, 04:26 PM
  3. Replies: 3
    Last Post: 03-26-2010, 08: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, 05:58 PM
  5. Regex - matching literal characters
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-08-2009, 12:25 AM

Posting Permissions

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