Search code examples
javafiledriveletter

Can't put drive letter in file path in java


Hello I cannot put the drive letter in this:

File workingdir = new File(System.getenv("SystemDrive")+":/Users/"+System.getProperty("user.name")+"/AppData/Local/Google/Chrome/User Data/Default");

This is not working, however this is working:

File workingidr =new File("C:/Users/"+System.getProperty("user.name")+"/AppData/Local/Google/Chrome/User Data/Default");

I cannot hard code the drive letter, since it will run on multiple computers. Thank you for your help guys.


Solution

  • This is because System.getenv("SystemDrive") returns "C:" not "C", so change your code to this:

    File workingdir = new File(System.getenv("SystemDrive")+"/Users/"+System.getProperty("user.name")+"/AppData/Local/Google/Chrome/User Data/Default");
    

    Also you could replace

    System.getenv("SystemDrive")+"/Users/"+System.getProperty("user.name")
    

    with this:

    System.getProperty("user.home")
    

    Hope this helped.