【发布时间】:2015-10-26 18:12:48
【问题描述】:
我希望能够使用可以在文件路径中更改的变量。与文件路径相关的用户名在构造函数中声明,然后我尝试将其分配给以下方法中的文件路径。
打电话时我想发生的事情:
System.out.format("%s%n", documentsPath.resolve(username +"\\Documents"));
文件路径是否会是:
C:\Users\ryanb\Documents
当我打电话给documentsPath.toString() 时,我只会得到回报:
C:\Users\
如何让documentsPath 变量分配有字符串用户名和最后的"\\Documents"。
这是我的代码:
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());
}
【问题讨论】:
标签: java file io path directory