1

I want to be able to use a variable that can be changed within a file path. The username relevant to the file path is declared in the constructor then I have tried to assign this to the file path in the below method.

What I wanted to happen when calling:

System.out.format("%s%n", documentsPath.resolve(username +"\\Documents"));

Was that the documents Path would then be:

C:\Users\ryanb\Documents

Instead when I call documentsPath.toString() I only get returned:

C:\Users\

How do I get the documentsPath variable to be assigned with the String username and the "\\Documents" at the end.

This is my code:

 class profileCopy{

  /*global variables */
  private Path documentsPath;
  private Path desktopPath;
  private Path favoritesPath;
  private Path networkFolder;
  private String username;
  private String foldername;


  public profileCopy(String username, String foldername)
  {
    this.username = username;
    this.foldername = foldername;
    documentsPath = Paths.get("C:\\Users");
    desktopPath = Paths.get("C:\\Users");
    favoritesPath = Paths.get("C:\\Users");
    networkFolder = (Paths.get("F:\\Data\\WP51"));

  }

  public void copyDocumentsFolder() throws IOException
  { 
    Path newDir = Paths.get("C:\\Users\\ryanb\\Documents\\TestCopy");
    System.out.format("%s%n", documentsPath.resolve(username +"\\Documents"));
    System.out.format("%s%n", networkFolder.resolve(foldername + "\\Backup"));
    System.out.println(networkFolder.getFileName());
    Files.move(documentsPath, networkFolder.resolve(documentsPath.getFileName()));
    System.out.println(newDir.toString());   
  }

2 Answers 2

3

The main point in order to your code doesn't work is that you don't re-assign the return value of resolve() method to your path variable, since the method returns a new object.

In order to build your Paths, you can use something like this:

documentsPath = Paths.get(string.format("C:\\Users\\%s\\%s", username, "Documents");

If you want to reuse some code, you can use an array of folders and create them:

List<Path> paths = new ArrayList();
String[] defaultFolders = {"Documents", "Desktop", "Music"};
foreach (folder : defaultFolders) {
paths.add(Path.get(string.format("C:\\Users\\%s\\%s", username, folder)));

PS: Since you're developing that in java, you should consider to make the Path's UNIX or Windows Compatible, since UNIX environments doensn't recognize the "C:/Users" path.

Sign up to request clarification or add additional context in comments.

Comments

1

The resolve methods returns a Path

public void copyDocumentsFolder() throws IOException
{
    Path newDir = Paths.get("C:\\Users\\ryanb\\Documents\\TestCopy");
    documentsPath = documentsPath.resolve(username + "\\Documents");
    networkFolder = networkFolder.resolve(foldername + "\\Backup");
    System.out.format("%s%n", documentsPath);
    System.out.format("%s%n", networkFolder);
    System.out.println(networkFolder.getFileName());
    Files.move(documentsPath, networkFolder.resolve(documentsPath.getFileName()));
    System.out.println(newDir.toString());
}

1 Comment

To clarify, documentsPath.resolve(...) returns a new Path object, leaving the original object untouched. It doesn't modify the object on which the method is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.